Matplotlib does not display interactive graph in Ipython - python

I am trying to plot a 3D scatter plot with matplotlib from IPython. I am able to make a plot when I use the inline magic command as follows
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
%matplotlib inline
y = np.arange(10)
x = np.arange(10)
z = np.arange(10)
plt.figure()
ax = plt.axes(projection='3d')
ax.scatter(x,y,z)
But because the plot is inline, it is not interactive and I can not rotate it to the viewing angle I want. When I replace the inline command with
%matplotlib
I get
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7fb80bf40358>
as output, but no window or graph appears. If I add
plt.show()
to the end of the script, nothing happens. How do I plot an interactive graph in IPython?

You may want to use pylab to get rid of most imports and all the namespaces:
%pylab
from mpl_toolkits.mplot3d import Axes3D
y = rand(100)
x = rand(100)
z = rand(100)
ax = subplot(projection='3d')
ax.scatter(x, y, z)
See https://plot.ly/ for interactive inline plots.

Related

VS Code jupyter notebook matplotlib plot directly in plot viewer?

Is it possible to have a matplotlib plot directly displayed in the plot viewer? I am currently working in VS Code on a juptyer notebook, both recently installed/updated. By default, the plot would open inline, like any other result of a cell, and I can open that in the plot viewer by clicking the icon in the top-left corner. Is it possible to have it displayed directly in the plot viewer, without the need to open it manually? I am just trying with something basic, like:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
ax.plot(x, y)
Thank you!
Add %matplotlib qt to your code before creating the figure/axes instances
import matplotlib.pyplot as plt
import numpy as np
%matplotlib qt
fig, ax = plt.subplots()
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
ax.plot(x, y)

Python Scatter plot not working with "None" points

Say I create three lists:
x=[1,2,3]
y=[4,5,6]
z=[1,None,4]
How can I scatter this and simply only include the points with numbers (i.e. exclude the "none" point). My code won't produce a scatter plot when I include these lists (however when I include a number instead of "None" it works):
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
%matplotlib notebook
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='r', marker='o')
plt.show()
You can do
import numpy as np
and replace your None with a np.nan. The points containing np.nan will not be plotted in your scatter plot. See this matplotlib doc for more information.
If you have long lists containing None, you can perform the conversion via
array_containing_nans = np.array(list_containing_nones, dtype=float)
you can use numpy.nan instead of None
import numpy as np
z=[1,None,4]
z_numpy = np.asarray(z, dtype=np.float32)
....
ax.scatter(x, y, z_numpy, c='r', marker='o')
You should use NaNs instead of None which is not the same thing. A NaN is a float.
Minimal example
import numpy as np
import matplotlib.pyplot as plt
x=[1,2,3]
y=[4,5,6]
z=[1,np.nan,4]
plt.scatter(x,y,z)
plt.show()

How to update mayavi interactively in ipython notebook?

I've been trying to create an interactive 3d plot in jupyter notebook.This worked great with matplotlib. Unfortunately matplotlib is very unfit to display intersecting planes. Therefore I wanted to switch to mayavi.
However mayavi shows some weird behaviour when I try to interactively change its values. Running the following Code:
%matplotlib notebook
#math
import numpy as np
#plotting
from mayavi import mlab
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
from IPython import display
import time
mlab.init_notebook()
X = np.arange(-5, 5, 0.3)
Y = np.arange(-5, 5, 0.3)
X,Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
mlab.clf()
#plot setup
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
def foo(x, y):
display.clear_output()
ax.cla()
#generate plots
ax.pcolor(X,Y,R,cmap='Blues')
ax.plot(x, y, 'ro')
mlab.mesh(X + x,Y,R, colormap='Blues')
#display plots
display.display(plt.gcf())
display.display(mlab.gcf())
def op_on_click(event):
foo(event.xdata, event.ydata)
foo(3,0)
time.sleep(10)
foo(10,0)
fig.canvas.mpl_connect('button_press_event', op_on_click)
It will immediately display the mayavi plot and update it after 10 seconds (as expected). But it does not redraw when the on_click event of the matplotlib figure is triggered.
Running it without mlab.clf() shows that the meshes actually get generated, the plot just doesnt get redrawn.
Any help on how to make it react to the on_click would be very appreciated.

Plots that varies over the time on Python Matplotlib with Jupyter

In the following lines I report a code that generates a plot changing over the time with Python on Anaconda Spyder
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-3, 3, 0.01)
N = 1
fig = plt.figure()
ax = fig.add_subplot(111)
for N in range(8):
y = np.sin(np.pi*x*N)
line, = ax.plot(x, y)
plt.draw()
plt.pause(0.5)
line.remove()
I would like to do the some with Jupyter, but it is not possible. Particularly it seems that the Matplotlib method .pause() does not exist on Jupyter.
Is there anyone who can explain me this difference and can help me building up a code for plots variating over the time with Python on Jupyter, please?
It works for me if I select an interactive backend using the magic command %matplotlib; it is likely that your Jupyter notebook settings are set to display plots inline.
import matplotlib.pyplot as plt
import numpy as np
%matplotlib
x = np.arange(-3, 3, 0.01)
N = 1
fig = plt.figure()
ax = fig.add_subplot(111)
for N in range(8):
y = np.sin(np.pi*x*N)
line, = ax.plot(x, y)
plt.draw()
plt.pause(0.5)
line.remove()
To restore your setings, use the magic %matplotlib inline

Adding different shades to a 3D plot in Python and Matplotlib

I'm looking for a VERY simple way of changing the colour of my 3D plot to make it look more interesting.
Here is my code (had to delete most of it because said i had too much code in my post):
from numpy import *
from matplotlib.pyplot import *
from mpl_toolkits.mplot3d import Axes3D
from numpy import array
from matplotlib import pyplot
from math import sqrt
X, Y = np.mgrid[:9, :21]
fig = figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax.set_xlabel('Time')
ax.set_ylabel('Step')
ax.set_zlabel('Probability')
ax.plot_surface(X, Y, probs)
show()
Here is my graph:
Any help would really be appreciated!!
Ive tried using the http://matplotlib.org/mpl_toolkits/mplot3d to no avail

Categories

Resources