Matplotlib: can I create AxesSubplot objects and then add them to the Figure instance?

Looking at the matplotlib documentation, it seems that the standard way to add AxesSubplot to 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 regardless of the shape, so I can use them in different drawings. 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 was unable to separate the creation of axes and shapes, but by following the examples below, you can easily reuse previously created axes in new instances or olf Figure. 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 
+38
python matplotlib
Jun 10 2018-11-11T00:
source share
3 answers

Typically, you simply pass an instance of the axes of the 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 answer your question, you can 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) 

Alternatively, you can simply add an instance of the axes to another drawing:

 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 fit the other β€œshapes” of the subtitle is also possible, but it will quickly become more problematic than worth it. The approach of simply passing an instance of a figure or axes (or a list of instances) is much simpler for complex cases, in my experience ...

+20
Jun 10 2018-11-11T00:
source share

For line graphs, you can deal with Line2D objects:

 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]) 
+5
Jun 10 '11 at 17:58
source share

The following shows how to β€œmove” an axis from one shape to another. This is the alleged functionality of @JoeKington of the last example , which in the new versions of matplotlib no longer works, because the axes cannot live by several figures at once.

First you need to remove the axis from the first shape, and then add it to the next shape and give it some place to live.

 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() 
+1
Oct 24 '17 at 9:15
source share



All Articles