Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Matplotlib.pyplot.figimage() function in Python
Next article icon

Thematplotlib.pyplot.plot() is used to create 2D plots such as line graphs and scatter plots. Theplot()function allows us to plot data points, customize line styles, markers and colors making it useful for various types of visualizations. In this article, we'll see how to use this function to plot data in Python.

Syntax: matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)

Parameters:

Returns:A list ofLine2D objects each representing a segment of the plotted data.

There are many ways of creating plot usingMatplotlib.pyplot.plot() function some of their examples are:

Example 1: Line Plots in Matplotlib

Here we useMatplotlib'splot() function to create a simple line plot with the data [1, 2, 3, 4, 5].

Python
importmatplotlib.pyplotaspltplt.plot([1,2,3,4,5])plt.title('Basic Line Plot')plt.show()

Output:

matplot1
Basic Line Plot

Example 2: Multiple Lines Using Matplotlib

We will plot sine and cosine functions on the same graph.

  • plt.plot(x, y1, label='Sin(x)', color='blue'):Plots sine curve with x values and y1 values labeling it as "Sin(x)" and using a blue color for the line.
  • plt.plot(x, y2, label='Cos(x)', color='red', linestyle='--'): Plots cosine curve with x values and y2 values labeling it as "Cos(x)" using a red color and a dashed line style.
Python
importmatplotlib.pyplotaspltimportnumpyasnpx=np.linspace(0,2*np.pi,100)y1=np.sin(x)y2=np.cos(x)plt.plot(x,y1,label='Sin(x)',color='blue')plt.plot(x,y2,label='Cos(x)',color='red',linestyle='--')plt.xlabel('X-axis')plt.ylabel('Y-axis')plt.title('Multiple Lines Plot')plt.legend()plt.show()

Output:

matplot2
Multiple Lines Plot

Example 3:Scatter Plot with Custom Markers

We will generate and customize scatter plot with 50 random data points featuring red circular markers.

  • plt.plot(x, y, marker='o', linestyle='', color='red', label='Scatter Plot'): Plots a scatter plot with x and y values and using red circular markers (marker='o') with no connecting lines (linestyle='') and labeling it as "Scatter Plot".
Python
importmatplotlib.pyplotaspltimportnumpyasnpnp.random.seed(42)x=np.random.rand(50)y=np.random.rand(50)plt.plot(x,y,marker='o',linestyle='',color='red',label='Scatter Plot')plt.xlabel('X-axis')plt.ylabel('Y-axis')plt.title('Scatter Plot Example')plt.legend()plt.show()

Output:

matplot3
Scatter Plot with Multiple Markers

Example 4: Plotting Multiple Curves

We are creating a line plot with two curves: a blue curvey = x^2and an orange curvey = 1 - x^3 generating data randomly. The plot is limited to the range [0, 1] on both axes showcasing a visual representation of mathematical functions.

Python
importmatplotlib.pyplotaspltimportnumpyasnpnp.random.seed(19680801)xdata=np.random.random([2,10])xdata1=xdata[0,:]xdata2=xdata[1,:]xdata1.sort()xdata2.sort()ydata1=xdata1**2ydata2=1-xdata2**3plt.plot(xdata1,ydata1,color='tab:blue')plt.plot(xdata2,ydata2,color='tab:orange')plt.xlim([0,1])plt.ylim([0,1])plt.title('matplotlib.pyplot.plot() example 2')plt.show()

Output:

matplot4
Two Curves Plot

With the flexibility to customize line styles, markers and colors Matplotlib's plot() function provides various possibilities for visualizing our data in Python.


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