Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I arrange multiple pyplot figures in a kind of layout?

I created a software to do signal analyses. There are multiple functions, and each finally display a complex figure containing labels, plot, axhspan, axvspan etc... Usually, these functions are called individually. Everyone of my functions returns a figure object, that I can save in pdf for example.

def Myfunction1(self):
    fig = pyplot.figure()
    ...do somestuff, create my figure
    pyplot.show()
    fig.savefig('C:\MyFigurefolder\figure1.pdf', dpi=300)
    return fig

def Myfunction2(self):
    fig = pyplot.figure()
    ...do some other stuff, create my 2nd figure
    pyplot.show()
    fig.savefig('C:\MyFigurefolder\figure2.pdf', dpi=300)
    return fig

Now, I would like to create a kind of "summary figure", by doing a metaanalyses, and pooling multiple figures together, and saving them in a final pdf. I don't really know how I should do that. Is there a way to use whole figure objects, (or maybe the multiple individual pdf) to do my figure?

Something like:

def FinalFigure(self):

    final = A_Kind_Of_Layout_Or_A_Figure_or_something

    a=self.Myfunction1()
    b=self.Myfunction2()

    Action_to_arrange_a_and_b_like_gridspec

    final.savefig('C:\MyFigurefolder\FinalFigure.pdf', dpi=300)
like image 524
Vantoine Avatar asked Jan 18 '13 18:01

Vantoine


People also ask

How do you plot multiple figures in Python?

In Matplotlib, we can draw multiple graphs in a single plot in two ways. One is by using subplot() function and other by superimposition of second graph on the first i.e, all graphs will appear on the same plot.

When creating a subplot using Matplotlib we can ensure the plot boxes are arranged in the space available using which function?

plt. subplots() , created a grid (2 rows and 3 columns). The function returned the figure object (stored in fig ) that will hold all the subplots, as well as all the individual axes (stored in axes ).


1 Answers

You may combine several plots with matplotlib.pyplot.subplot. For more control on the layout, check out the GridSpec.

Edit: As requested, a short example from the linked tutorial:

A gridspec instance provides array-like (2d or 1d) indexing that returns the SubplotSpec instance. For, SubplotSpec that spans multiple cells, use slice.

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1,:-1])
ax3 = plt.subplot(gs[1:, -1])
ax4 = plt.subplot(gs[-1,0])
ax5 = plt.subplot(gs[-1,-2])

enter image description here

like image 190
HenriV Avatar answered Oct 04 '22 11:10

HenriV