I have two plots
import matplotlib.pyplot as plt plt.subplot(121) plt.subplot(122)
I want plt.subplot(122)
to be half as wide as plt.subplot(121)
. Is there a straightforward way to set the height and width parameters for a subplot?
To change the size of subplots in Matplotlib, use the plt. subplots() method with the figsize parameter (e.g., figsize=(8,6) ) to specify one size for all subplots — unit in inches — and the gridspec_kw parameter (e.g., gridspec_kw={'width_ratios': [2, 1]} ) to specify individual sizes.
To change figure size of more subplots you can use plt. subplots(2,2,figsize=(10,10)) when creating subplots.
Import matplotlib. To change the figure size, use figsize argument and set the width and the height of the plot. Next, we define the data coordinates. To plot a bar chart, use the bar() function. To display the chart, use the show() function.
See the grid-spec tutorial:
http://matplotlib.sourceforge.net/users/gridspec.html
Example code:
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec f = plt.figure() gs = gridspec.GridSpec(1, 2,width_ratios=[2,1]) ax1 = plt.subplot(gs[0]) ax2 = plt.subplot(gs[1]) plt.show()
You can also adjust the height ratio using a similar option in GridSpec
By simply specifying the geometry with “122
”, you're implicitly getting the automatic, equal-sized columns-and-rows layout.
To customise the layout grid, you need to get a little more specific. See “Customizing Location of Subplot Using GridSpec” in the Matplotlib docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With