Matplotlib's Figure and Axes explanation
Asked Answered
M

1

6

I am really pretty new to matplotlib, though I know that it can be very powerful. I've been reading number of tutorials and examples and it's a real hassle to understand how does matplotlib's Figure and Axes work. I am illustrating, what I am trying to understand, with the attached figure.enter image description here

I know how to create a figure instance of certain size in inches. However, what bothers me is how can I create subplots and then axes, within each subplot, with relative coordinates (bottom=0,left=0,top=1,right=1) as illustrated. So, for example I want to create a "parent" plot area (say (6in,10in)). Then, I want to create two subplot areas, each with size (3in,3in), with 1in space from the top, 2in space between the two vertical subplot areas and 1in from bottom. Then, 1in space on the left and 2in space on the write. In the same time, I would like to be able to get the coordinates of the subplot areas with respect to the main plot area. Then, inside the first subplot area, I'd like to create 2 axis instances, with Axis 1, having coordinates with respect to Subplot Area1 (0.1,0.7,0.7,0.2) and Axes 2 (0.1,0.2,0.7,0.5). And then of course I'd like to be able to plot on these axes e.g., ax1.plot()....
If you could provide a sample code to achieve that, then I can study it. Your help will be very much appreciated!

Mra answered 16/3, 2016 at 15:48 Comment(5)
a subplot and an Axes object are really the same thing. There is not really a "subplot" as you describe it in matplotlib. You can just create your three Axes objects using gridspec without the need to put them in your "subplots"Nelsonnema
Then, why does matplotlib have two separate clasess Axes and subplot, when they are the same thing?Mra
fig.add_subplot returns an Axes instance. subplot is not a class, it is a function that returns an Axes.Nelsonnema
Hi @tom, then what is the difference between fig.add_subplot and fig.add_axes?Mra
Too much to write in a comment, so I've added an answer with some detailsNelsonnema
N
6

a subplot and an Axes object are really the same thing. There is not really a "subplot" as you describe it in matplotlib. You can just create your three Axes objects using gridspec without the need to put them in your "subplots".

There are a few different ways to create Axes instances within your figure.

fig.add_axes will create an Axes instance at the position given to it (you give it [left,bottom,width,height] in figure coordinates (i.e. 0,0 is bottom left, 1,1 is top right).

fig.add_subplot will also create an Axes instance. In this case, rather than giving it a rectangle to be created in, you give it the number of rows and columns of subplots you would like, and then the plot_number, where plot_number starts at 1, increments across rows first and has a maximum of nrows * ncols.

For example, to create the top-left Axes in a grid of 2 row and 2 columns, you could do the following:

fig.add_subplot(2,2,1)

or the shorthand

fig.add_subplot(221)

There are some more customisable ways to create Axes as well, for example gridspec and subplot2grid which allow for easy creation of many subplots of different shapes and sizes.

Nelsonnema answered 16/3, 2016 at 16:49 Comment(3)
@ tom, thank you so much. Your help has really made a difference, as I was not able to understand all that from the matplotlib documentation. I appreciate that!Mra
@tom Suppose one has generated multiple axes that one would like to add to the figure instance. Is there a way to do this?Wirra
@MPath I guess that would depend on how you created them, etc. Probably best to ask that in a new question rather than me try to explain in a comment to a different questionNelsonnema

© 2022 - 2024 — McMap. All rights reserved.