Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to Add Title to Subplots in Matplotlib?
Next article icon

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

By default, it returns a figure with a single plot. For each axes object i.e. plot we can set a title (set viaset_title()), an x-label (set viaset_xlabel()), and a y-label set viaset_ylabel()).

Let's see how this works:

  1. When we call the subplots() method by stacking only in one direction it returns a 1D array of axes objects i.e. subplots.
  2. We can access these axes objects using indices just like we access elements of the array. To create specific subplots, call matplotlib.pyplot.plot() on the corresponding index of the axes. Refer to the following figure for a better understanding

Create Multiple Subplots in Matplotlib in Python

Here, we have various Ways toMatplotlib create subplots inPython. Here, we are using some generally used Ways to Matplotlib create subplots in Python those are following.

  • 1-D Array of Subplots
  • Using Stacking in Two Directions
  • Usingplt.subplots with 2D Array of Subplots

Subplots in Matplotlib Using 1-D Array of Subplots

In this example code utilizes Matplotlib to generate a figure with two subplots. The data, represented by arrays 'x,' 'y,' and 'z,' is plotted on separate axes within the figure. The resulting visualization displays distinct plots for the datasets 'y' and 'z' in the designated subplots.

Python3
# importing libraryimportmatplotlib.pyplotasplt# Some data to displayx=[1,2,3]y=[0,1,0]z=[1,0,1]# Creating 2 subplotsfig,ax=plt.subplots(2)# Accessing each axes object to plot the data through returned arrayax[0].plot(x,y)ax[1].plot(x,z)

Output

subplot1D-300x200

Subplots in Matplotlib Using Stacking in Two Directions

In this Python script, the Matplotlib library is imported, andNumPy is used for numerical operations. Data for plotting sine waves is generated. The script then creates a 3x2 grid of subplots, each plotting the same sine wave with different colors (orange, green, blue, magenta, black, and red).

Python3
# importing libraryimportmatplotlib.pyplotaspltimportnumpyasnp# Data for plottingx=np.arange(0.0,2.0,0.01)y=1+np.sin(2*np.pi*x)# Creating 6 subplots and unpacking the output array immediatelyfig,((ax1,ax2),(ax3,ax4),(ax5,ax6))=plt.subplots(3,2)ax1.plot(x,y,color="orange")ax2.plot(x,y,color="green")ax3.plot(x,y,color="blue")ax4.plot(x,y,color="magenta")ax5.plot(x,y,color="black")ax6.plot(x,y,color="red")

Output

Subplots_fig2

Matplotlib Multiple Plots Same Figure

In this example Python script utilizes Matplotlib to create a 2x2 grid of subplots. Each subplot showcases a different type of plot: line plot, scatter plot, bar plot, and histogram. The Axes objects are accessed through the 2D array 'axs,' and specific data is visualized in each subplot, demonstrating the flexibility of Matplotlib for diverse plotting needs.

Python3
importmatplotlib.pyplotasplt# Create a 2x2 grid of subplotsfig,axs=plt.subplots(2,2)# Now axs is a 2D array of Axes objectsaxs[0,0].plot([1,2,3],[4,5,6])axs[0,1].scatter([1,2,3],[4,5,6])axs[1,0].bar([1,2,3],[4,5,6])axs[1,1].hist([1,2,2,3,3,3,3,3,3,4,4,5])plt.show()

Output

first


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