Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Matplotlib.pyplot.subplot2grid() in python
Next article icon

Prerequisites: matplotlib

subplot() function adds subplot to a current figure at the specified grid position.  It is similar to the subplots() function however unlike subplots() it adds one subplot at a time. So to create multiple plots you will need several lines of code with the subplot() function. Another drawback of the subplot function is that it deletes the preexisting plot on your figure. Refer to example 1.

It is a wrapper of Figure.add_subplot.

Syntax:

subplot(nrows, ncols, index, **kwargs)

subplot(pos, **kwargs) 

subplot(ax)

Parameters : 

  • args:  Either a 3-digit integer or three separate integers describing the position of the subplot.
  • pos is a three-digit integer where the first, second, and third integer are nrows,ncols, index.
  • projection : [{None, ’aitoff’, ’hammer’, ’lambert’, ’mollweide’, ’polar’, ’rectilinear’, str}, optional]. The projection-type of the subplot (Axes). The default None results in a ’rectilinear’ projection.
  • label : [str] A label for the returned axes.
  • **kwargs:This method also takes the keyword arguments for the returned axes base class;
    except for the figure argument, for e.g facecolor.

Returns :  An axes.SubplotBase subclass of Axes or a subclass of Axes. The returned axes base class depends on the projection used.

Implementation of the function is given below:

Example 1: subplot() will delete the pre-existing plot.

Python3
# importing the moduleimportmatplotlib.pyplotasplt# Data to display on plotx=[1,2,3,4,5]y=[1,2,1,2,1]# plot() will create new figure and will add axes object (plot) of above dataplt.plot(x,y,marker="x",color="green")# subplot() will add plot to current figure deleting existing plotplt.subplot(121)

Output:We can see that the first plot got set aside by the subplot() function. 

subplot_gfg

If you want to see the first plot comment out plt.subplot() line and you will see the following plot

plot_gfg

Example 2:

Python3
importmatplotlib.pyplotasplt# data to display on plotsx=[3,1,3]y=[3,2,1]z=[1,3,1]# Creating figure objectplt.figure()# adding first subplotplt.subplot(121)plt.plot(x,y,color="orange",marker="*")# adding second subplotplt.subplot(122)plt.plot(z,y,color="yellow",marker="*")

Output :

multiple_subplots

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