Is python matplotlib.pyplot globally scoped? - python

Most of the times, we import matplotlib.pyplot as follows, and use plt to plot graphs/diagrams.
import matplotlib.pyplot as plt
x = ...
plt.plot(x, sin(x))
...
plt.savefig('/path/to/sin_x.png')
plt.plot(x, cos(x))
...
plt.savefig('/path/to/cos_x.png')
My question is how to use plt as a local variable, like this?
plt1 = get_plot()
plt1.title('y = sin(x)')
...
plt1.plot(x, sin(x))
plt1.savefig('/path/to/sin_x.png')
plt2 = get_plot()
plt2.title('y = cos(x)')
...
plt2.plot(x, cos(x))
plt2.savefig('/path/to/cos_x.png')
plt1 == plt2 # false
Or, customizations on the first figure won't affect the second one without explicitly calling plt.clf()?

This is a good example of the benefits of the matplotlib object-oriented interface over the "state-machine" interface (see, for example, here and here).
The differences:
State-machine
import matplotlib.pyplot as plt
plt.plot(x, sin(x))
plt.savefig('plot.png')
OO interface
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, sin(x)
fig.savefig('plot.png')
When using the OO interface, you can create as many figure and Axes instances, and refer back to them without interfering with other instances.
For example:
import matplotlib.pyplot as plt
# Create first figure instance
fig1 = plt.figure()
# Add an Axes instance to the figure
ax1 = fig1.add_subplot(111)
# Set the title
ax1.set_title('y = sin(x)')
# Plot your data
ax1.plot(x, sin(x))
# Save this figure
fig1.savefig('/path/to/sin_x.png')
# Now create a second figure and axes. Here I use an altenative method,
# plt.subplots(1), to show how to create the figure and axes in one step.
fig2, ax2 = plt.subplots(1)
ax2.set_title('y = cos(x)')
ax2.plot(x, cos(x))
fig2.savefig('/path/to/cos_x.png')
# If you want to, you could modify `ax1` or `fig1` here, without affecting `fig2` and `ax2`

Related

Showing subplots at each pass of a loop

I would essentially like to do the following:
import matplotlib.pyplot as plt
import numpy as np
fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
for i in range(10):
ax1.scatter(i, np.sqrt(i))
ax1.show() # something equivalent to this
ax2.scatter(i, i**2)
That is, each time a point is plotted on ax1, it is shown - ax2 being shown once.
You cannot show an axes alone. An axes is always part of a figure. For animations you would want to use an interactive backend. Then the code in a jupyter notebook could look like
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
frames = 10
x = np.arange(frames)
line1, = ax1.plot([],[], ls="", marker="o")
line2, = ax2.plot(x, x**2, ls="", marker="o")
ax2.set_visible(False)
def animate(i):
line1.set_data(x[:i], np.sqrt(x[:i]))
ax1.set_title(f"{i}")
ax1.relim()
ax1.autoscale_view()
if i==frames-1:
ax2.set_visible(True)
fig2.canvas.draw_idle()
ani = FuncAnimation(fig1, animate, frames=frames, repeat=False)
plt.show()
If you want to change plots dynamically I'd suggest you don't redraw the whole plot every time, this will result in very laggy behavior. Instead you could use Blit to do this. I used it in a previous project. Maybe it can help you too if you just take the parts from this you need:
Python project dynamically updating plot

Matplotlib copy/duplicate a 3D figure?

I've tried to find a way to copy a 3D figure in matplotlib but I didn't find a solution which is appropriate in my case.
From these posts
How do I reuse plots in matplotlib?
and
How to combine several matplotlib figures into one figure?
Using fig2._axstack.add(fig2._make_key(ax),ax) as in the code below gives quite the good result but figure 2 is not interactive I can't rotate the figure etc :
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(1)
ax = fig.gca(projection = '3d')
ax.plot([0,1],[0,1],[0,1])
fig2 = plt.figure(2)
fig2._axstack.add(fig2._make_key(ax),ax)
plt.show()
An alternative would be to copy objects from ax to ax2 using a copy method proposed in this post How do I reuse plots in matplotlib? but executing the code below returns RuntimeError: Can not put single artist in more than one figure :
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np, copy
fig = plt.figure(1)
ax = fig.gca(projection = '3d')
ax.plot([0,1],[0,1],[0,1])
fig2 = plt.figure(2)
ax2 = fig2.gca(projection = '3d')
for n in range(len(ax.lines)) :
ax2.add_line(copy.copy(ax.lines[n]))
plt.show()
Those codes are pretty simple but I don't want to copy/paste part of my code for drawing similar figures
Thanks in advance for your reply !

Python: matplotlib - loop, clear and show different plots over the same figure

I want to see how a plot varies with different values using a loop. I want to see it on the same plot. But i do not want to remains of the previous plot in the figure. In MATLAB this is possible by creating a figure and just plotting over the same figure. Closing it when the loop ends.
Like,
fh = figure();
%for loop here
%do something with x and y
subplot(211), plot(x);
subplot(212), plot(y);
pause(1)
%loop done
close(fh);
I am not able to find the equivalent of this in matplotlib. Usually all the questions are related to plotting different series on the same plot, which seems to come naturally on matplotlib, by plotting several series using plt.plot() and then showing them all finally using plt.show(). But I want to refresh the plot.
There are essentially two different ways to create animations in matplotlib
interactive mode
Turning on interactive more is done using plt.ion(). This will create a plot even though show has not yet been called. The plot can be updated by calling plt.draw() or for an animation, plt.pause().
import matplotlib.pyplot as plt
x = [1,1]
y = [1,2]
fig, (ax1,ax2) = plt.subplots(nrows=2, sharex=True, sharey=True)
line1, = ax1.plot(x)
line2, = ax2.plot(y)
ax1.set_xlim(-1,17)
ax1.set_ylim(-400,3000)
plt.ion()
for i in range(15):
x.append(x[-1]+x[-2])
line1.set_data(range(len(x)), x)
y.append(y[-1]+y[-2])
line2.set_data(range(len(y)), y)
plt.pause(0.1)
plt.ioff()
plt.show()
FuncAnimation
Matplotlib provides an animation submodule, which simplifies creating animations and also allows to easily save them. The same as above, using FuncAnimation would look like:
import matplotlib.pyplot as plt
import matplotlib.animation
x = [1,1]
y = [1,2]
fig, (ax1,ax2) = plt.subplots(nrows=2, sharex=True, sharey=True)
line1, = ax1.plot(x)
line2, = ax2.plot(y)
ax1.set_xlim(-1,18)
ax1.set_ylim(-400,3000)
def update(i):
x.append(x[-1]+x[-2])
line1.set_data(range(len(x)), x)
y.append(y[-1]+y[-2])
line2.set_data(range(len(y)), y)
ani = matplotlib.animation.FuncAnimation(fig, update, frames=14, repeat=False)
plt.show()
An example to animate a sine wave with changing frequency and its power spectrum would be the following:
import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np
x = np.linspace(0,24*np.pi,512)
y = np.sin(x)
def fft(x):
fft = np.abs(np.fft.rfft(x))
return fft**2/(fft**2).max()
fig, (ax1,ax2) = plt.subplots(nrows=2)
line1, = ax1.plot(x,y)
line2, = ax2.plot(fft(y))
ax2.set_xlim(0,50)
ax2.set_ylim(0,1)
def update(i):
y = np.sin((i+1)/30.*x)
line1.set_data(x,y)
y2 = fft(y)
line2.set_data(range(len(y2)), y2)
ani = matplotlib.animation.FuncAnimation(fig, update, frames=60, repeat=True)
plt.show()
If you call plt.show() inside the loop you will see the plot for each element on the loop as long as you close the window containing the figure. The process, will be plot for the first element, then if you close the window you will see the plot for the second element in the loop, etc

How do I change matplotlib's subplot projection of an existing axis?

I'm trying to construct a simple function that takes a subplot instance (matplotlib.axes._subplots.AxesSubplot) and transforms its projection to another projection, for example, to one of the cartopy.crs.CRS projections.
The idea looks something like this
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
def make_ax_map(ax, projection=ccrs.PlateCarree()):
# set ax projection to the specified projection
...
# other fancy formatting
ax2.coastlines()
...
# Create a grid of plots
fig, (ax1, ax2) = plt.subplots(ncols=2)
# the first subplot remains unchanged
ax1.plot(np.random.rand(10))
# the second one gets another projection
make_ax_map(ax2)
Of course, I can just use fig.add_subplot() function:
fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax1.plot(np.random.rand(10))
ax2 = fig.add_subplot(122,projection=ccrs.PlateCarree())
ax2.coastlines()
but I was wondering if there is a proper matplotlib method to change a subplot axis projection after it was defined. Reading matplotlib API didn't help unfortunately.
You can't change the projection of an existing axes, the reason is given below. However the solution to your underlying problem is simply to use the subplot_kw argument to plt.subplots() described in the matplotlib documentation here. For example, if you wanted all your subplots to have the cartopy.crs.PlateCarree projection you could do
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
# Create a grid of plots
fig, (ax1, ax2) = plt.subplots(ncols=2, subplot_kw={'projection': ccrs.PlateCarree()})
Regarding the actual question, specifying a projection when you create an axes set determines the axes class you get, which is different for each projection type. For example
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
ax1 = plt.subplot(311)
ax2 = plt.subplot(312, projection='polar')
ax3 = plt.subplot(313, projection=ccrs.PlateCarree())
print(type(ax1))
print(type(ax2))
print(type(ax3))
This code will print the following
<class 'matplotlib.axes._subplots.AxesSubplot'>
<class 'matplotlib.axes._subplots.PolarAxesSubplot'>
<class 'cartopy.mpl.geoaxes.GeoAxesSubplot'>
Notice how each axes is actually an instance of a different class.
Assuming there are multiple axes being used for 2D plotting, like...
fig = matplotlib.pyplot.Figure()
axs = fig.subplots(3, 4) # prepare for multiple subplots
# (some plotting here)
axs[0,0].plot([1,2,3])
... one can simply destroy one of them and replace it with a new one having the 3D projection:
axs[2,3].remove()
ax = fig.add_subplot(3, 4, 12, projection='3d')
ax.plot_surface(...)
Just note that unlike rest of Python, the add_subplot uses row-column indexing starting from 1 (not from 0).
EDIT: Changed my typo about indexing.
following the answer to this question :
In python, how can I inherit and override a method on a class instance, assigning this new version to the same name as the old one?
I found a hack to change the projection of an axe after creating it which seems to work at least in the simple example below, but I have no idea if this solution is the best way
from matplotlib.axes import Axes
from matplotlib.projections import register_projection
class CustomAxe(Axes):
name = 'customaxe'
def plotko(self, x):
self.plot(x, 'ko')
self.set_title('CustomAxe')
register_projection(CustomAxe)
if __name__ == '__main__':
import matplotlib.pyplot as plt
fig = plt.figure()
## use this syntax to create a customaxe directly
# ax = fig.add_subplot(111, projection="customaxe")
## change the projection after creation
ax = plt.gca()
ax.__class__ = CustomAxe
ax.plotko(range(10))
plt.show()
You can use the following function, which removes the axis and generates the axis in the specified projection, similar to dominecf answer, with the advantage that the specific subplot parameters (row, col, and index) are retrieved automatically.
import matplotlib.pyplot as plt
def update_projection(ax, axi, projection='3d', fig=None):
if fig is None:
fig = plt.gcf()
rows, cols, start, stop = axi.get_subplotspec().get_geometry()
ax.flat[start].remove()
ax.flat[start] = fig.add_subplot(rows, cols, start+1, projection=projection)
and generate a plot with all available projections
import matplotlib.projections
import numpy as np
# test data
x = np.linspace(-np.pi, np.pi, 10)
# plot all projections available
projections = matplotlib.projections.get_projection_names()
fig, ax = plt.subplots(nrows=1, ncols=len(projections), figsize=[3.5*len(projections), 4], squeeze=False)
for i, pro_i in enumerate(projections):
update_projection(ax, ax.flat[i], pro_i)
ax.flat[i].set_title(pro_i)
try:
ax.flat[i].grid(True)
ax.flat[i].plot(x, x)
except Exception as a:
print(pro_i, a)
plt.tight_layout(pad=.5)

Python: 3D scatter losing colormap

I'm creating a 3D scatter plot with multiple sets of data and using a colormap for the whole figure. The code looks like this:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for R in [range(0,10), range(5,15), range(10,20)]:
data = [np.array(R), np.array(range(10)), np.array(range(10))]
AX = ax.scatter(*data, c=data[0], vmin=0, vmax=20, cmap=plt.cm.jet)
def forceUpdate(event): AX.changed()
fig.canvas.mpl_connect('draw_event', forceUpdate)
plt.colorbar(AX)
This works fine but as soon as I save it or rotate the plot, the colors on the first and second scatters turn blue.
The force update is working by keeping the colors but only on the last scatter plot drawn. I tried making a loop that updates all the scatter plots but I get the same result as above:
AX = []
for R in [range(0,10), range(5,15), range(10,20)]:
data = [np.array(R), np.array(range(10)), np.array(range(10))]
AX.append(ax.scatter(*data, c=data[0], vmin=0, vmax=20, cmap=plt.cm.jet))
for i in AX:
def forceUpdate(event): i.changed()
fig.canvas.mpl_connect('draw_event', forceUpdate)
Any idea how I can make sure all scatters are being updated so the colors don't disappear?
Thanks!
Having modified your code so that it does anything:
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from mpl_toolkits.mplot3d import Axes3D
>>> AX = \[\]
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111, projection='3d')
>>> for R in \[range(0,10), range(5,15), range(10,20)\]:
... data = \[np.array(R), np.array(range(10)), np.array(range(10))\]
... AX = ax.scatter(*data, c=data\[0\], vmin=0, vmax=20, cmap=plt.cm.jet)
... def forceUpdate(event): AX.changed()
... fig.canvas.mpl_connect('draw_event', forceUpdate)
...
9
10
11
>>> plt.colorbar(AX)
<matplotlib.colorbar.Colorbar instance at 0x36265a8>
>>> plt.show()
then I get:
So the above code is working. If your existing code isn't then I suggest that you try the exact code above and if that doesn't work look into the versions of code that you are using if it does work then you will have to investigate the differences between it and your actual code, (rather than your example code).

Categories