简体   繁体   中英

Creating two completely independent plots in matplotlib and going back and forth between them

I'd like to create two independent matplotlib plots within a python script, and potentially jump back and forth between them as I add lines, annotations, etc. to the various plots (for example, perhaps I call a function which adds lines to both plots, and then another function which adds the annotations).

I expect that by working off matplotlib examples I'd be able to figure out some solution that works, but I'd like to know what the preferred and cleanest way of doing this is. I tend to get confused about when I should be doing things like

fig,ax=plt.subplots()

and when I should be doing things like:

fig=plt.figure()

Furthermore, how should I be switching back and forth between plots. If I did something like

fig1,ax1=plt.subplots()
fig2,ax2=plt.subplots()

can I then just refer to these plots by doing something like:

ax1.plt.plot([some stuff])
ax2.plt.plot([otherstuff]

? I ask this because often in the matplotlib examples they don't refer to the plot like this after calling plt.subplot() but instead call commands like

plt.plot([stuff])

where presumably it doesn't matter that they didn't specify ax1 or ax2 because there's only one plot in the example. At the end I'd like to save both plots to file using something like

plt.savefig(....)

although I need, again, to be able to refer to both plots independently. So what's the proper way of implementing this?

If you want to be able to write code that clearly applies commands to distinct axes, you want to use the object oriented interface .

Actually, both of your first two examples are using this interface. The differences is that plt.subplots() will create both a figure object and a grid of axes, while plt.figure() just creates the figure.

The figure object has methods to create axes within it. So, these two blocks of code are equivalent:

fig, ax = plt.subplots()

and

fig = plt.figure()
ax = fig.add_subplot(111)

Generally, the latter approach is only going to be more useful when you want multiple axes within the figure that don't follow a regular grid. So, you could do:

fig = plt.figure()
ax = fig.add_axes([.1, .1, .2, .8])

which will add a tall axes object on the left side of the figure.


Next, how do you get multiple axes to plot on?

The subplots function takes two positional arguments specifying the number of rows and columns in the grid (these default to (1, 1) . So if you want two axes side by side, you would do

fig, axes = plt.subplots(1, 2)

Now axes is going to be a (1, 2) object array that is filled with Axes objects. It's often more convenient, for a small grid, to use Python's tuple unpacking and get direct references to the objects:

fig, (ax1, ax2) = plt.subplots(1, 2)

Now, what do you do with these objects, and what's the relationship between them and the MATLAB-style procedure interface?

Most functions in the pyplot namespace also exist as methods on either Figure or Axes objects. Matplotlib (and MATLAB) has a concept of the "current" figure and axes. When you call a function like plt.plot , it draws on the current axis (usually the most recently created one). When you call a function like plt.savefig , it saves the current figure.

For simple task, this is a bit more direct and usually easier than using the object-oriented interface. However, when you start making more complex plots, eg a grid of axes where each grid has multiple layers (maybe a scatterplot and a regression line), being able to structure the code around what you are doing rather than where you are doing has substantial advantages. Generally, plotting code that is written in an object-oriented fashion will scale much better than code written in a procedural fashion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM