It seems easy but I could not find any solution for opening multiple figures and save them by their name. I look for something like this:
fig1, ax1 = pl.subplots(1)
fig2, ax2 = pl.subplots(1)
...
pl.savefig('f1.png', fig1)
pl.savefig('f2.png', fig2)
usually pl.savefig acts on the last active figure. So how one can activate a figure and save it, then repeat the process for the rest of the figures?
You can save an image using the figure object itself:
fig1.savefig(...)
Alternatively, you can change the current figure by calling plt.figure(1) to select the first figure that was create and then use plt.savefig(). Or, you can use plt.figure(fig1.number) to switch focus to fig1
import matplotlib.pyplot as plt
fig1, ax1 = plt.subplots(1)
fig2, ax2 = plt.subplots(1)
# Can choose one of the below to change the current figure
plt.figure(1)
# plt.figure(fig1.number)
plt.savefig(...) # will save fig1
Related
I have already plotted two figures separately in a single jupyter notebook file, and exported them.
What I want is to show them side by side, but not plot them again by using matplotlib.pyplot.subplots.
For example, in Mathematica, it's easier to do this by just saving the figures into a Variable, and displaying them afterwards.
What I tried was saving the figures, using
fig1, ax1 = plt.subplots(1,1)
... #plotting using ax1.plot()
fig2, ax2 = plt.subplots(1,1)
... #plotting using ax2.plot()
Now, those fig1 or fig2 are of type Matplotlib.figure.figure which stores the figure as an 'image-type' instance. I can even see them separately by calling just fig1 or fig2 in my notebook.
But, I can not show them together as by doing something like
plt.show(fig1, fig2)
It returns nothing since, there wasn't any figures currently being plotted.
You may look at this link or this, which is a Mathematica version of what I was talking about.
assuming u want to merge those subplots in the end.
Here is the code
import numpy as np
import matplotlib.pyplot as plt
#e.x function to plot
x = np.linspace(0, 10)
y = np.exp(x)
#almost your code
figure, axes = plt.subplots(1,1)
res_1, = axes.plot(x,y) #saving the results in a tuple
plt.show()
plt.close(figure)
figure, axes = plt.subplots(1,1)
res_2, = axes.plot(x,-y) #same before
plt.show()
#restructure to merge
figure_2, (axe_1,axe_2) = plt.subplots(1,2) #defining rows and columns
axe_1.plot(res_1.get_data()[0], res_1.get_data()[1]) #using the already generated data
axe_2.plot(res_2.get_data()[0], res_2.get_data()[1])
#if you want show them in one
plt.show()
Not quite sure what you mean with:
but not plot them again by using matplotlib.pyplot.subplots.
But you can display two figures next to each other in a jupyter notebook by using:
fig, ax = plt.subplots(nrows=1, ncols=2)
ax[0] = ... # Code for first figure
ax[1] = ... # Code for second figure
plt.show()
Or above each other:
fig, ax = plt.subplots(nrows=2, ncols=1)
ax[0] = ... # Top figure
ax[1] = ... # Bottom figure
plt.show()
What i want
I want to combine two matplotlib figures in one new subplot.
The two figures are returned from visualization functions of libraries i don't want to or can't change myself(rebuild from source etc.). Also it should be not a hack-around but rather be a nice generic matplotlib solution.
The pseudo code looks like the following.
Pseudo code
import matplotlib.pyplot as plt
from library1 import magic_visualization_1
from library2 import magic_visualization_2
# Data of type some_crazy_data_type_of_the_library e.g. no simple x,y coords
data = ...
fig1 = magic_visualization_1(data) # type is: <class 'matplotlib.figure.Figure'>
fig2 = magic_visualization_2(data) # type is: <class 'matplotlib.figure.Figure'>
fig, axs = plt.subplots(2, 1, figsize=(10, 5))
# Somehow add fig1
# Somehow add fig2
plt.show()
# or like
fig = plt.figure(figsize=(10, 5))
gridspec = fig.add_gridspec(2, 1, left=0.05, right=0.95, wspace=0.1, hspace=0.15)
# Somehow add fig1
# Somehow add fig2
plt.show()
Example images
The two example figures:
fig1,
fig2
Photoshoped result
I should look like this(i made this by hand with gimp/photoshop)
fig1 on top of fig2
What i tried
The best idea i found was deepcopying every figure into the new subfigures but that feels to much like a hack-around.
Also i tried this solution with copying the two figures content.
Result:
vertical concatenation of the two figures
So I have a following simple code saved in a .py file, and executing in shell:
import matplotlib.pyplot as plt
myfig = plt.figure(figsize=(5, 5))
ax1 = myfig.add_subplot(1, 1, 1)
myfig.show()
However it does nothing upon execution, no errors nothing.
Then when I start Ipython in shell, and type exact same code, it does pop up an empty window. Why is that?
of course I can use plt.show() and everything is fine. But lets say I have two figures, fig1 and fig2, and there is stuff in both figs, and I want to only display one of them, how can I do that? plt.show() plots both of them.
Sorry if this is stupid I'm just curious why when working interactively in ipython, window pops up upon calling fig1.show() but nothing happens when I execute same script in shell but doing: python myfile.py
Thank you!
plt.show starts an event loop, creates interactive windows and shows all current figures in them. If you have more figures than you actually want to show in your current pyplot state, you may close all unneeded figures prior to calling plt.show().
fig1 = plt.figure()
ax1 = fig1.add_subplot(1, 1, 1)
ax1.plot([1,3,4])
fig2 = plt.figure()
ax2 = fig2.add_subplot(1, 1, 1)
ax2.plot([1,2,5])
# close first figure
plt.close(fig1)
# show all active figures (which is now only fig2)
plt.show()
In contrast fig.show() will not start an event loop. It will hence only make sense in case an event loop already has been started, e.g. after plt.show() has been called. In non-interactive mode that may happen upon events in the event loop. To give an example, the following would show fig2 once a key on the keyboard is pressed when fig1 is active.
import matplotlib.pyplot as plt
fig1 = plt.figure()
ax1 = fig1.add_subplot(1, 1, 1)
ax1.plot([1,3,4])
def show_new_figure(evt=None):
fig2 = plt.figure()
ax2 = fig2.add_subplot(1, 1, 1)
ax2.plot([1,2,5])
fig2.show()
# Upon pressing any key in fig1, show fig2.
fig1.canvas.mpl_connect("key_press_event", show_new_figure)
plt.show()
You need to modify your code like this:
import matplotlib.pyplot as plt
myfig = plt.figure(figsize=(5, 5))
ax1 = myfig.add_subplot(1, 1, 1)
plt.plot((1, 2, 3)) # <- plot something
plt.show() # <- show the plot
more info in matplotlib docs here.
you need add an extra line
%matplotlib inline
To get the plot in jupyter notebook.
for more you can refer http://ipython.readthedocs.io/en/stable/interactive/tutorial.html#magics-explained
I have created a figure in one part of the code as follows:
n = arange(51)
fig3 = plt.figure()
plt.semilogy(n,a1mag,'ro')
Now, i want to add another plot to this figure at a later part of the code. Is there some way to access fig3 while plotting?
It would be recommendable to either stay completely in the pyplot state-machine or comlpetely in the object oriented API; mixing the two causes just headaches.
pyplot
plt.figure(3)
plt.semilogy(x,y,'ro')
# .. do other stuff
# reactivate figure 3
plt.figure(3)
plt.plot(x,z)
object-oriented API
fig3, ax3 = plt.subplots()
ax3.semilogy(x,y)
# .. do other stuff
# plot to ax3
ax3.plot(x,z)
I would like to create a small function for my own work. However I would like to create something like porting existing plots into figures. Which goes like this:
import matplotlib.pyplot as PLT
ax1 = PLT.plot(array1)
ax2 ...
def multi_ax(array_of_ax ):
fig = PLT.figure()
for n in range(some_number):
ax = fig.add_subplot(x,y,n+1)
ax.replacing(array_of_ax[n], postions_of_array)
Is there way to fit this way? Thanks in advance.
It isn't possible to move an axes from one figure to another; the axes is linked to the figure upon creation.
Instead, you'll have to first generate the figure, and then the axeses within that figure.