[Matplotlib-users] First Time asking a question

Eric Firing efiring at hawaii.edu
Thu Jun 7 21:45:48 EDT 2018


Matthew,

If you want to manually specify the position of each Axes rectangle in 
inches--so you can explicitly lay out the figure exactly as you want-- 
you can do it by creating each Axes with a helper like this:

def axes_inches(fig, rect, **kw):
     """
     Wrapper for Figure.add_axes in which *rect* is given in inches.
     The translation to normalized coordinates is done immediately
     based on the present figsize.

     *rect* is left, bottom, width, height in inches
     *kw* are passed to Figure.add_axes

     """

     fw = fig.get_figwidth()
     fh = fig.get_figheight()
     l, b, w, h = rect
     relrect = [l / fw, b / fh, w / fw, h / fh]
     ax = fig.add_axes(relrect, **kw)
     return ax

This only works if you are not using an interactive backend, or if you 
are using an interactive backend and you specify a dpi small enough so 
that your specified figure size fits on your screen with room for 
boundaries and the toolbar.  If it doesn't, unfortunately, it will be 
resized to fit, and then it won't scale properly when you save the 
figure to a file.  For example, if you are displaying on the screen with 
a backend like qt5agg and you want the figure size to be (25, 21) when 
you write it (e.g., to a pdf file), then you will probably need to 
create the figure with dpi=20 or something like that.  When you save to 
a file, you can of course do so with a different dpi.

Eric

On 2018/06/07 3:31 AM, Matthew Bradley wrote:
> Hi all,
> 
> This is my first time asking a question on this channel so everyone's 
> help is appreciated. I'm trying to recreate a figure that my research 
> lab first created in mathematica (attached in the email). I've tried to 
> recreate the figure and plot area using the gridspec feature, and it's 
> come close (my closest attempt is also attached). There are still a few 
> things that need tweaking like making the lower right plot a square 
> shape, decreasing the spacing between it and the plot to the left and 
> above it.
> 
> Here's my code as a starting point if you wanted to use it. The figure 
> size doesn't have to be 25:20.
> 
> fig = plt.figure(figsize=(25,20))
> gs1 = gridspec.GridSpec(7,10)
> 
> ax1 = plt.subplot(gs1[1:,0])
> ax2 = plt.subplot(gs1[0:6,1])
> ax3 = plt.subplot(gs1[1:,3])
> ax4 = plt.subplot(gs1[0:6,4])
> ax5 = plt.subplot(gs1[1,6])
> ax6 = plt.subplot(gs1[0,7])
> ax7 = plt.subplot(gs1[0,9])
> ax8 = plt.subplot(gs1[4:,6:])
> gs1.update(wspace=0.02,hspace=0)
> 
> 
> My question is how to do this most efficiently using gridspec or if 
> there is another feature that makes such a setup a much easier task. I 
> think I can add more columns and rows to the gridspec and custom define 
> row height and column width ratios to recreate the plot but that seems 
> like it would take a lot of trial and error on my part. Likewise if I 
> were to define multiple gridspecs and define their layouts with the 
> gridspec.update command would take some guessing and checking. A 
> gridspec within a gridspec using GridSpecFromSubPlot command might be 
> able to work but it doesn't have an attribute .update() to customize the 
> wspace between plots in the gridspec that I know of. Is there a way to 
> define a plots origin (in pixel or some other coordinates) and its extent?
> 
> ​Thanks in advance!​
> 
> 
> -- 
> Matthew Bradley
> 
> 
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
> 



More information about the Matplotlib-users mailing list