Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use `GridSpec()` with `subplots()`

There are several (example) SO posts dealing with how to use GridSpec with subplotting in general. What i am trying to achieve to no avail, is to allow the use of GridSpec with subplots, something like this, Where i can replace the actual Array and list indexes with some loop controlled index :

gs = gridspec.GridSpec(4, 1, height_ratios=[2, 2, 1, 1])         tPlot, axes = plt.subplots(4, sharex=True, sharey=False) tPlot.suptitle(node, fontsize=20) axes[0].plot(targetDay[0], gs[0]) axes[1].plot(targetDay[1], gs[1]) axes[2].scatter(targetDay[2], gs[2]) axes[3].plot(targetDay[3], gs[3]) 

Needless to say that this code does not work, it just an example.

like image 862
InsaneBot Avatar asked Dec 14 '15 13:12

InsaneBot


People also ask

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 ).

How do you combine subplots in Python?

To combine several matplotlib axes subplots into one figure, we can use subplots() method with nrow=2.

How do I increase the gap 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. They are the fractions of axis width and height, respectively.


1 Answers

Rather than calling gridspec.GridSpec before subplots, you can send the kwargs to GridSpec from within your subplots call, using the gridspec_kw argument. From the docs:

gridspec_kw : dict

Dict with keywords passed to the GridSpec constructor used to create the grid the subplots are placed on.

So, for example:

import matplotlib.pyplot as plt  tPlot, axes = plt.subplots(         nrows=4, ncols=1, sharex=True, sharey=False,          gridspec_kw={'height_ratios':[2,2,1,1]}         )  tPlot.suptitle('node', fontsize=20)  axes[0].plot(range(10),'ro-')  axes[1].plot(range(10),'bo-')  axes[2].plot(range(10),'go-')  axes[3].plot(range(10),'mo-')   plt.show() 

enter image description here

like image 198
tmdavison Avatar answered Sep 30 '22 17:09

tmdavison