Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating subplots with differing shapes in matplotlib

meteogram

I'm looking to create a figure with multiple subplots much like this meteogram in matplotlib. I will need multiple subplots of varying heights (all the same width, but some will be "skinnier" and some will be "fatter"). Just like the image above, some subplots need to be big and some need to be small, but they need to be large enough to convey useful information no matter how many subplots there are.

When I create a subplot as pyplot.subplot(2,1,1) it has the aspect ratio I need, but if I increase the number of rows (e.g. subplot(3,1,1)) the subplot becomes "too skinny". I'm going to need to fit a half dozen of these plots on one figure but they become too "small" if I start to fit more subplots on the figure.

Put simply, is a figure like this meteogram possible in matplotlib without stitching together multiple images? These images will be returned, by url request, from a server so technically it's possible but if I can fix this problem in the library that would be preferred.

The documentation of matplotlib is fantastic, but it seems matplotlib is tuned to give you subplots with the same aspect ratio rather than letting you give subplots with arbitrary aspect ratios as the number of subplots increases. I can't find a single example on the net of differing subplot sizes.

like image 373
user1844160 Avatar asked Nov 22 '12 06:11

user1844160


People also ask

How do I make multiple subplots in Matplotlib?

To create multiple plots use matplotlib. pyplot. subplots method which returns the figure along with Axes object or array of Axes object. nrows, ncols attributes of subplots() method determine the number of rows and columns of the subplot grid.

How do you make a 2x2 subplot in Python?

subplots(2, 2) for a 2 x 2 array. This option is most useful for two subplots (e.g.: fig, (ax1, ax2) = plt. subplots(1, 2) or fig, (ax1, ax2) = plt. subplots(2, 1) ).

How do you separate subplots in Python?

Using subplots_adjust() method to set the spacing between subplots. We can use the plt. subplots_adjust() method to change the space between Matplotlib subplots. The parameters wspace and hspace specify the space reserved between Matplotlib subplots.


1 Answers

You can have full control of axes positioning by giving it a bounding box in normalised coordinates (on construction). The format is [left, top, width, height].

An example of how I would go about setting up the figure:

import matplotlib.pyplot as plt


left = 0.1
width = 0.8

ax1 = plt.axes([left, 0.5, width, 0.45])
ax2 = plt.axes([left, 0.3, width, 0.19])
ax3 = plt.axes([left, 0.2, width, 0.09], sharex=ax2)
ax4 = plt.axes([left, 0.1, width, 0.09], sharex=ax2)

# ticks at the top of the top plot
ax1.xaxis.tick_top()

# remove ticks for ax2 and ax3
ax2.xaxis.set_visible(False)
ax3.xaxis.set_visible(False)

# only put ticks on the bottom of ax4
ax4.xaxis.tick_bottom()

plt.show()

I've added some functionality which you might find useful (removing the top ticks, sharing the xaxis, moving the ticks to the top of an Axes).

The result:

Example output

HTH

like image 196
pelson Avatar answered Sep 22 '22 16:09

pelson