Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to set the spacing between subplots in Matplotlib in Python?
Next article icon

In this article, we will learn different ways to create subplots of different sizes usingMatplotlib. It provides 3 different methods using which we can create different subplots of different sizes.

Methods available to create subplot: 

  • Gridspec
  • gridspec_kw
  • subplot2grid

Create Different Subplot Sizes in Matplotlib using Gridspec

The GridSpec from the gridspec module is used to adjust the geometry of the Subplot grid. We can use different parameters to adjust the shape, size, and number of columns and rows.

gridspec.GridSpec(ncols=2, nrows=2, width_ratios=[2, 1],wspace=0.5,hspace=0.5,height_ratios=[1,2])

Python3
# importing required librariesimportmatplotlib.pyplotaspltfrommatplotlibimportgridspecimportnumpyasnp# create a figurefig=plt.figure()# to change size of subplot's# set height of each subplot as 8fig.set_figheight(8)# set width of each subplot as 8fig.set_figwidth(8)# create grid for different subplotsspec=gridspec.GridSpec(ncols=2,nrows=2,width_ratios=[2,1],wspace=0.5,hspace=0.5,height_ratios=[1,2])# initializing x,y axis valuex=np.arange(0,10,0.1)y=np.cos(x)# ax0 will take 0th position in# geometry(Grid we created for subplots),# as we defined the position as "spec[0]"ax0=fig.add_subplot(spec[0])ax0.plot(x,y)# ax1 will take 0th position in# geometry(Grid we created for subplots),# as we defined the position as "spec[1]"ax1=fig.add_subplot(spec[1])ax1.plot(x,y)# ax2 will take 0th position in# geometry(Grid we created for subplots),# as we defined the position as "spec[2]"ax2=fig.add_subplot(spec[2])ax2.plot(x,y)# ax3 will take 0th position in# geometry(Grid we created for subplots),# as we defined the position as "spec[3]"ax3=fig.add_subplot(spec[3])ax3.plot(x,y)# display the plotsplt.show()

Output: 

Create Different Subplot Sizes in Matplotlib?
 

Create Different Subplot Sizes in Matplotlib gridspec_kw

It is a dictionary available inside"plt.subplots()" method in Matplotlib. By passing different parameters to the dictionary we can adjust the shape and size of each subplot. 

plt.subplots(nrows=2, ncols=2,figsize=(7,7), gridspec_kw={'width_ratios': [3, 3], 'height_ratios': [3, 3], 'wspace' : 0.4, 'hspace' : 0.4})

Code : 

Python3
# importing required librariesimportmatplotlib.pyplotaspltimportnumpyasnp# setting different parameters to adjust each gridfig,ax=plt.subplots(nrows=2,ncols=2,figsize=(7,7),gridspec_kw={'width_ratios':[3,3],'height_ratios':[3,3],'wspace':0.4,'hspace':0.4})# initializing x,y axis valuex=np.arange(0,10,0.1)y=np.tan(x)# ax[0][0] will take 0th position in# geometry(Grid we created for subplots)ax[0][0].plot(x,y)# ax[0][0] will take 0th position in# geometry(Grid we created for subplots)ax[0][1].plot(x,y)# ax[0][0] will take 0th position in# geometry(Grid we created for subplots)ax[1][0].plot(x,y)# ax[0][0] will take 0th position in# geometry(Grid we created for subplots)ax[1][1].plot(x,y)plt.show()

Output : 

Create Different Subplot Sizes in Matplotlib?
 

Create Different Subplot Sizes in Matplotlib subplot2grid

It provides more flexibility to create a grid at any location. We can extend the grid horizontally as well as vertically very easily. Here shape is denoting to no. of rows and columns and loc denotes the geometric location of the grid. Whereas, colspan = 3 in ax1, it means this subplot will cover 3 of all columns of that row. If we give irregular input then it will give an error so we have given the proper value to colspan and rowspan.

plt.subplot2grid(shape=(3, 3), loc=(0, 0), colspan=3)

Python3
# importing required libraryimportmatplotlib.pyplotaspltimportnumpyasnp# creating grid for subplotsfig=plt.figure()fig.set_figheight(6)fig.set_figwidth(6)ax1=plt.subplot2grid(shape=(3,3),loc=(0,0),colspan=3)ax2=plt.subplot2grid(shape=(3,3),loc=(1,0),colspan=1)ax3=plt.subplot2grid(shape=(3,3),loc=(1,2),rowspan=2)ax4=plt.subplot2grid((3,3),(2,0))ax5=plt.subplot2grid((3,3),(2,1),colspan=1)# initializing x,y axis valuex=np.arange(0,10,0.1)y=np.cos(x)# plotting subplotsax1.plot(x,y)ax1.set_title('ax1')ax2.plot(x,y)ax2.set_title('ax2')ax3.plot(x,y)ax3.set_title('ax3')ax4.plot(x,y)ax4.set_title('ax4')ax5.plot(x,y)ax5.set_title('ax5')# automatically adjust padding horizontally# as well as vertically.plt.tight_layout()# display plotplt.show()

Output : 

Create Different Subplot Sizes in Matplotlib?
 

Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp