Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Plot Multiple lines in Matplotlib
Next article icon

Line plots are important data visualization elements that can be used to identify relationships within the data. Usingmatplotlib.pyplot.plot() function we can plot line plots. Styling tools in this helps us customize line plots according to our requirements which helps in better representations.

Line Styles in Matplotlib

Below are the available line styles present inMatplotlib.

Character

Definition

-

Solid line

--

Dashed line

-.

dash-dot line

Dotted line

.

Point marker

Marker represent various points and shape markers in Matplotlib.

Character

Definition

o

Circle marker

,

Pixel marker

v

triangle_down marker

^

triangle_up marker

<

triangle_left marker

>

triangle_right marker

1

tri_down marker

2

tri_up marker

3

tri_left marker

4

tri_right marker

s

square marker

p

pentagon marker

*

star marker

h

hexagon1 marker

H

hexagon2 marker

+

Plus marker

x

X marker

D

Diamond marker

d

thin_diamond marker

|

vline marker

_

hline marker

Color code abbreviations that can be used with line styles.

Codes

Description

b

blue

g

green

r

red

c

cyan

m

magenta

y

yellow

k

black

w

white

Below are some examples by which we line plot styles in Matplotlib in Python:

Example 1: Plotting a Simple Line Plot Style

In this example, we will visualize marks of 20 students in a class. Each student's name is paired with a randomly generated mark and a dashed magenta line graph representing the distribution of these marks.

  • plt.figure(figsize=(12, 6)):Creates a new figure for the plot with a specified size of 12 inches wide by 6 inches high.
  • plt.plot(students, marks, 'm--'): Plots a line graph with the student list on x-axis and marks list on y-axis using a magenta dashed line style ('m--').
Python
importmatplotlib.pyplotaspltimportrandomasrandomstudents=["Jane","Joe","Beck","Tom","Sam","Eva","Samuel","Jack","Dana","Ester","Carla","Steve","Fallon","Liam","Culhane","Candance","Ana","Mari","Steffi","Adam"]marks=[]foriinrange(0,len(students)):marks.append(random.randint(0,101))plt.figure(figsize=(12,6))plt.xlabel("Students")plt.ylabel("Marks")plt.title("CLASS RECORDS")plt.plot(students,marks,'m--')

Output:

LINE-PLOT1
Simple Line Plot

Example 2:Adding Markers to Line Plots

In same above example where each student's name is plotted against their corresponding mark and the line is solid green and data points are marked with red circles. By this example we can understand how to change line styles and markers in Matplotlib.

  • plt.plot(students, marks, color = 'green', linestyle = 'solid', marker = 'o', markerfacecolor = 'red', markersize = 12):It customizes the line with a green color, solid line style and markers ('o') with red fill and a size of 12 for the markers.
Python
importmatplotlib.pyplotaspltimportrandomasrandomstudents=["Jane","Joe","Beck","Tom","Sam","Eva","Samuel","Jack","Dana","Ester","Carla","Steve","Fallon","Liam","Culhane","Candance","Ana","Mari","Steffi","Adam"]marks=[]foriinrange(0,len(students)):marks.append(random.randint(0,101))plt.figure(figsize=(12,6))plt.xlabel("Students")plt.ylabel("Marks")plt.title("CLASS RECORDS")plt.plot(students,marks,color='green',linestyle='solid',marker='o',markerfacecolor='red',markersize=12)

Output:

LINEPLOT2
Markers in Line Plots

Example 3: Adding Grid Lines in Line Plots

Here the marks are displayed using a dashed magenta line graph.Grid lines are added to provide better readability and reference across the plot.

  • plt.figure(figsize=(14, 6)): It creates a new figure for the plot with a specified size of 14 inches wide by 6 inches high.
  • plt.plot(students, marks, 'm--'): It will plots a line graph using a magenta dashed line style ('m--').
  • plt.grid(True, which='both', linestyle='--', linewidth=0.5): Adds grid lines to the plot with dashed lines for both major and minor grid lines and sets the line width to 0.5.
Python
importmatplotlib.pyplotaspltimportrandomasrandomstudents=["Jane","Joe","Beck","Tom","Sam","Eva","Samuel","Jack","Dana","Ester","Carla","Steve","Fallon","Liam","Culhane","Candance","Ana","Mari","Steffi","Adam"]marks=[]foriinrange(0,len(students)):marks.append(random.randint(0,101))plt.figure(figsize=(14,6))plt.xlabel("Students")plt.ylabel("Marks")plt.title("CLASS RECORDS")plt.plot(students,marks,'m--')plt.grid(True,which='both',linestyle='--',linewidth=0.5)plt.show()

Output:

LINEPLOT3
Grid Lines in Line Plots

We can identify trends and patterns in our data by using multiple styling features including line styles, markers and colors together with gridlines for better understanding of data.


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