I am trying to put different functions plots on the same figure. Is that possible?
Overall, this is what I have tried:
fig1 = plt.figure()
fig1, (ax1, ax2) = plt.subplots(2)
x = [1,2,3,4]
y = [5,6,7,8]
z = [1,2,5,6]
def plot1(xcoord,ycoord,ax=None):
ax=ax
ax.plot(xcoord,ycoord)
return plt.show()
def plot2(xcoord,ycoord,ax=None):
ax=ax
ax.plot(xcoord,ycoord)
ax.grid('on')
return plt.show()
doplot1(x,y,ax1)
doplot2(x,z,ax2)
I would like it to return with axis doplot1 on ax1 and doplot2 on ax2.
Thanks for helping!!
The standard way to do what you want, would be:
import matplotlib.pyplot as plt
def doplot1(xcoord, ycoord, ax=None):
ax.plot(xcoord, ycoord)
def doplot2(xcoord, ycoord, ax=None):
ax = ax or plt.gca()
ax.plot(xcoord, ycoord)
ax.grid('on')
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
z = [1, 2, 5, 6]
fig1, (ax1, ax2) = plt.subplots(2)
doplot1(x, y, ax1)
doplot2(x, z, ax2)
plt.show()
Some remarks:
When using plt.subplots() (not to be confounded by plt.subplot() without the 's'), there is no need to call plt.figure() as that is now done automatically.
plt.show() should only be called after all the subplots have been created.
ax = ax or plt.gca() can be used to check that when no ax is given (so being None) it gets the value of the current ax (gca means "get current axis").
Related
Let's see a contrived example of 2 matplotlib.pyplot functions returning a plot, which then may be accessed through plt.gcf() or plt.gca() methods:
x = np.linspace(-5, 5, 11)
y = x
y_2 = x * x
plt.plot(x,y)
fig_1 = plt.gcf()
ax_1 = plt.gca()
#plt.close()
plt.plot(x,y_2)
fig_2 = plt.gcf()
ax_2 = plt.gca()
#plt.close()
How would I nicely draw fig_1 and fig_2 objects alongside through e.g. plt.subplots(1,2) (given x, y, y_2 are not accessable)
Use the add_subplot method of the Figure object to draw the two plots alongside.
Try this:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-5, 5, 11)
y = x
y_2 = x * x
fig, axs = plt.subplots(1, 2) # create a figure with subplots
axs[0].plot(x, y) # first figure
axs[1].plot(x, y_2) # second figure
plt.show()
if no given given x, y, y_2:
fig, axs = plt.subplots(1, 2)
axs[0].plot(x, y)
axs[1].plot(x, y_2)
plt.show()
Looking at the matplotlib documentation, it seems the standard way to add an AxesSubplot to a Figure is to use Figure.add_subplot:
from matplotlib import pyplot
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.hist( some params .... )
I would like to be able to create AxesSubPlot-like objects independently of the figure, so I can use them in different figures. Something like
fig = pyplot.figure()
histoA = some_axes_subplot_maker.hist( some params ..... )
histoA = some_axes_subplot_maker.hist( some other params ..... )
# make one figure with both plots
fig.add_subaxes(histo1, 211)
fig.add_subaxes(histo1, 212)
fig2 = pyplot.figure()
# make a figure with the first plot only
fig2.add_subaxes(histo1, 111)
Is this possible in matplotlib and if so, how can I do this?
Update: I have not managed to decouple creation of Axes and Figures, but following examples in the answers below, can easily re-use previously created axes in new or olf Figure instances. This can be illustrated with a simple function:
def plot_axes(ax, fig=None, geometry=(1,1,1)):
if fig is None:
fig = plt.figure()
if ax.get_geometry() != geometry :
ax.change_geometry(*geometry)
ax = fig.axes.append(ax)
return fig
Typically, you just pass the axes instance to a function.
For example:
import matplotlib.pyplot as plt
import numpy as np
def main():
x = np.linspace(0, 6 * np.pi, 100)
fig1, (ax1, ax2) = plt.subplots(nrows=2)
plot(x, np.sin(x), ax1)
plot(x, np.random.random(100), ax2)
fig2 = plt.figure()
plot(x, np.cos(x))
plt.show()
def plot(x, y, ax=None):
if ax is None:
ax = plt.gca()
line, = ax.plot(x, y, 'go')
ax.set_ylabel('Yabba dabba do!')
return line
if __name__ == '__main__':
main()
To respond to your question, you could always do something like this:
def subplot(data, fig=None, index=111):
if fig is None:
fig = plt.figure()
ax = fig.add_subplot(index)
ax.plot(data)
Also, you can simply add an axes instance to another figure:
import matplotlib.pyplot as plt
fig1, ax = plt.subplots()
ax.plot(range(10))
fig2 = plt.figure()
fig2.axes.append(ax)
plt.show()
Resizing it to match other subplot "shapes" is also possible, but it's going to quickly become more trouble than it's worth. The approach of just passing around a figure or axes instance (or list of instances) is much simpler for complex cases, in my experience...
The following shows how to "move" an axes from one figure to another. This is the intended functionality of #JoeKington's last example, which in newer matplotlib versions is not working anymore, because axes cannot live in several figures at once.
You would first need to remove the axes from the first figure, then append it to the next figure and give it some position to live in.
import matplotlib.pyplot as plt
fig1, ax = plt.subplots()
ax.plot(range(10))
ax.remove()
fig2 = plt.figure()
ax.figure=fig2
fig2.axes.append(ax)
fig2.add_axes(ax)
dummy = fig2.add_subplot(111)
ax.set_position(dummy.get_position())
dummy.remove()
plt.close(fig1)
plt.show()
For line plots, you can deal with the Line2D objects themselves:
fig1 = pylab.figure()
ax1 = fig1.add_subplot(111)
lines = ax1.plot(scipy.randn(10))
fig2 = pylab.figure()
ax2 = fig2.add_subplot(111)
ax2.add_line(lines[0])
TL;DR based partly on Joe nice answer.
Opt.1: fig.add_subplot()
def fcn_return_plot():
return plt.plot(np.random.random((10,)))
n = 4
fig = plt.figure(figsize=(n*3,2))
#fig, ax = plt.subplots(1, n, sharey=True, figsize=(n*3,2)) # also works
for index in list(range(n)):
fig.add_subplot(1, n, index + 1)
fcn_return_plot()
plt.title(f"plot: {index}", fontsize=20)
Opt.2: pass ax[index] to a function that returns ax[index].plot()
def fcn_return_plot_input_ax(ax=None):
if ax is None:
ax = plt.gca()
return ax.plot(np.random.random((10,)))
n = 4
fig, ax = plt.subplots(1, n, sharey=True, figsize=(n*3,2))
for index in list(range(n)):
fcn_return_plot_input_ax(ax[index])
ax[index].set_title(f"plot: {index}", fontsize=20)
Outputs respect.
Note: Opt.1 plt.title() changed in opt.2 to ax[index].set_title(). Find more Matplotlib Gotchas in Van der Plas book.
To go deeper in the rabbit hole. Extending my previous answer, one could return a whole ax, and not ax.plot() only. E.g.
If dataframe had 100 tests of 20 types (here id):
dfA = pd.DataFrame(np.random.random((100,3)), columns = ['y1', 'y2', 'y3'])
dfB = pd.DataFrame(np.repeat(list(range(20)),5), columns = ['id'])
dfC = dfA.join(dfB)
And the plot function (this is the key of this whole answer):
def plot_feature_each_id(df, feature, id_range=[], ax=None, legend_bool=False):
feature = df[feature]
if not len(id_range): id_range=set(df['id'])
legend_arr = []
for k in id_range:
pass
mask = (df['id'] == k)
ax.plot(feature[mask])
legend_arr.append(f"id: {k}")
if legend_bool: ax.legend(legend_arr)
return ax
We can achieve:
feature_arr = dfC.drop('id',1).columns
id_range= np.random.randint(len(set(dfC.id)), size=(10,))
n = len(feature_arr)
fig, ax = plt.subplots(1, n, figsize=(n*6,4));
for i,k in enumerate(feature_arr):
plot_feature_each_id(dfC, k, np.sort(id_range), ax[i], legend_bool=(i+1==n))
ax[i].set_title(k, fontsize=20)
ax[i].set_xlabel("test nr. (id)", fontsize=20)
I am trying to plot 2 way bar charts. I want to invert the x-axis of x1 so that 0 is in the middle of both. I keep getting the error:
AttributeError: 'BarContainer' object has no attribute 'invert_xaxis'
Here is my code:
import matplotlib.pyplot as plt
y = ['F','M','H']
x1 = [8, 4, 3]
x2 = [2, 4, 7]
fig, axes = plt.subplots(ncols=2, sharey=True)
axes[0] = plt.barh(y, x1, align='center', color='b')
axes[1] = plt.barh(y, x2, align='center', color='r')
axes[0].invert_xaxis()
plt.show()
The problem is that you are assigning the plots to the two axis objects instead of using them to plot. The correct way is to directly use the axis objects to plot the barh. Then the things will work as expected.
import matplotlib.pyplot as plt
y = ['F','M','H']
x1 = [8, 4, 3]
x2 = [2, 4, 7]
fig, axes = plt.subplots(ncols=2, sharey=True)
axes[0].barh(y, x1, align='center', color='b') # <---- Changed here
axes[1].barh(y, x2, align='center', color='r') # <---- Changed here
axes[0].invert_xaxis()
plt.show()
Looking at the matplotlib documentation, it seems the standard way to add an AxesSubplot to a Figure is to use Figure.add_subplot:
from matplotlib import pyplot
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.hist( some params .... )
I would like to be able to create AxesSubPlot-like objects independently of the figure, so I can use them in different figures. Something like
fig = pyplot.figure()
histoA = some_axes_subplot_maker.hist( some params ..... )
histoA = some_axes_subplot_maker.hist( some other params ..... )
# make one figure with both plots
fig.add_subaxes(histo1, 211)
fig.add_subaxes(histo1, 212)
fig2 = pyplot.figure()
# make a figure with the first plot only
fig2.add_subaxes(histo1, 111)
Is this possible in matplotlib and if so, how can I do this?
Update: I have not managed to decouple creation of Axes and Figures, but following examples in the answers below, can easily re-use previously created axes in new or olf Figure instances. This can be illustrated with a simple function:
def plot_axes(ax, fig=None, geometry=(1,1,1)):
if fig is None:
fig = plt.figure()
if ax.get_geometry() != geometry :
ax.change_geometry(*geometry)
ax = fig.axes.append(ax)
return fig
Typically, you just pass the axes instance to a function.
For example:
import matplotlib.pyplot as plt
import numpy as np
def main():
x = np.linspace(0, 6 * np.pi, 100)
fig1, (ax1, ax2) = plt.subplots(nrows=2)
plot(x, np.sin(x), ax1)
plot(x, np.random.random(100), ax2)
fig2 = plt.figure()
plot(x, np.cos(x))
plt.show()
def plot(x, y, ax=None):
if ax is None:
ax = plt.gca()
line, = ax.plot(x, y, 'go')
ax.set_ylabel('Yabba dabba do!')
return line
if __name__ == '__main__':
main()
To respond to your question, you could always do something like this:
def subplot(data, fig=None, index=111):
if fig is None:
fig = plt.figure()
ax = fig.add_subplot(index)
ax.plot(data)
Also, you can simply add an axes instance to another figure:
import matplotlib.pyplot as plt
fig1, ax = plt.subplots()
ax.plot(range(10))
fig2 = plt.figure()
fig2.axes.append(ax)
plt.show()
Resizing it to match other subplot "shapes" is also possible, but it's going to quickly become more trouble than it's worth. The approach of just passing around a figure or axes instance (or list of instances) is much simpler for complex cases, in my experience...
The following shows how to "move" an axes from one figure to another. This is the intended functionality of #JoeKington's last example, which in newer matplotlib versions is not working anymore, because axes cannot live in several figures at once.
You would first need to remove the axes from the first figure, then append it to the next figure and give it some position to live in.
import matplotlib.pyplot as plt
fig1, ax = plt.subplots()
ax.plot(range(10))
ax.remove()
fig2 = plt.figure()
ax.figure=fig2
fig2.axes.append(ax)
fig2.add_axes(ax)
dummy = fig2.add_subplot(111)
ax.set_position(dummy.get_position())
dummy.remove()
plt.close(fig1)
plt.show()
For line plots, you can deal with the Line2D objects themselves:
fig1 = pylab.figure()
ax1 = fig1.add_subplot(111)
lines = ax1.plot(scipy.randn(10))
fig2 = pylab.figure()
ax2 = fig2.add_subplot(111)
ax2.add_line(lines[0])
TL;DR based partly on Joe nice answer.
Opt.1: fig.add_subplot()
def fcn_return_plot():
return plt.plot(np.random.random((10,)))
n = 4
fig = plt.figure(figsize=(n*3,2))
#fig, ax = plt.subplots(1, n, sharey=True, figsize=(n*3,2)) # also works
for index in list(range(n)):
fig.add_subplot(1, n, index + 1)
fcn_return_plot()
plt.title(f"plot: {index}", fontsize=20)
Opt.2: pass ax[index] to a function that returns ax[index].plot()
def fcn_return_plot_input_ax(ax=None):
if ax is None:
ax = plt.gca()
return ax.plot(np.random.random((10,)))
n = 4
fig, ax = plt.subplots(1, n, sharey=True, figsize=(n*3,2))
for index in list(range(n)):
fcn_return_plot_input_ax(ax[index])
ax[index].set_title(f"plot: {index}", fontsize=20)
Outputs respect.
Note: Opt.1 plt.title() changed in opt.2 to ax[index].set_title(). Find more Matplotlib Gotchas in Van der Plas book.
To go deeper in the rabbit hole. Extending my previous answer, one could return a whole ax, and not ax.plot() only. E.g.
If dataframe had 100 tests of 20 types (here id):
dfA = pd.DataFrame(np.random.random((100,3)), columns = ['y1', 'y2', 'y3'])
dfB = pd.DataFrame(np.repeat(list(range(20)),5), columns = ['id'])
dfC = dfA.join(dfB)
And the plot function (this is the key of this whole answer):
def plot_feature_each_id(df, feature, id_range=[], ax=None, legend_bool=False):
feature = df[feature]
if not len(id_range): id_range=set(df['id'])
legend_arr = []
for k in id_range:
pass
mask = (df['id'] == k)
ax.plot(feature[mask])
legend_arr.append(f"id: {k}")
if legend_bool: ax.legend(legend_arr)
return ax
We can achieve:
feature_arr = dfC.drop('id',1).columns
id_range= np.random.randint(len(set(dfC.id)), size=(10,))
n = len(feature_arr)
fig, ax = plt.subplots(1, n, figsize=(n*6,4));
for i,k in enumerate(feature_arr):
plot_feature_each_id(dfC, k, np.sort(id_range), ax[i], legend_bool=(i+1==n))
ax[i].set_title(k, fontsize=20)
ax[i].set_xlabel("test nr. (id)", fontsize=20)
I'm somewhat new to matplotlib. What I'm trying to do is write code that saves several figures to eps files, and then generates a composite figure. Basically what I'd like to do is have something like
import matplotlib.pyplot as plt
from matplotlib import gridspec
def my_plot_1():
fig = plt.figure()
...
return fig
def my_plot_2():
fig = plt.figure()
...
return fig
def my_combo_plot(fig1,fig2):
fig = plt.figure()
gs = gridspec.GridSpec(2,2)
ax1 = plt.subplot(gs[0,0])
ax2 = plt.subplot(gs[0,1])
ax1 COPY fig1
ax2 COPY fig2
...
where then later I could do something like
my_combo_plot( my_plot_1() , my_plot_2() )
and have all the data and settings get copied from the plots returned by the first two functions, but I can't figure out how this would be done with matplotlib.
Since pyplot kind of works like a state machine, I'm not sure if what you are asking for is possible. I would instead factor out the drawing code, something like this:
import matplotlib.pyplot as plt
def my_plot_1(ax=None):
if ax is None:
ax = plt.gca()
ax.plot([1, 2, 3], 'b-')
def my_plot_2(ax=None):
if ax is None:
ax = plt.gca()
ax.plot([3, 2, 1], 'ro')
def my_combo_plot():
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
my_plot_1(ax1)
my_plot_2(ax2)
Using the answer https://stackoverflow.com/a/46906599/5267751 it's possible to move the axes from one figure to other (using pickle it's also possible to keep the old figure).
Add set_subplotspec to position the resulting axes:
import matplotlib.pyplot as plt
from matplotlib import gridspec
def my_plot_1():
fig = plt.figure()
plt.plot([1, 2, 3], 'b-')
return fig
def my_plot_2():
fig = plt.figure()
plt.plot([3, 2, 1], 'ro')
return fig
fig1 = my_plot_1()
fig2 = my_plot_2()
def my_combo_plot(fig1,fig2):
fig = plt.figure()
gs = gridspec.GridSpec(2,2)
ax1 = fig1.axes[0]
ax1.remove()
ax1.figure = fig
fig.add_axes(ax1)
ax1.set_subplotspec(gs[0, 0])
ax2 = fig2.axes[0]
ax2.remove()
ax2.figure = fig
fig.add_axes(ax2)
ax2.set_subplotspec(gs[0, 1])
plt.close(fig1)
plt.close(fig2)
my_combo_plot( my_plot_1() , my_plot_2() )
plt.show()
The code assumes that each figure contains exactly one axes, however.