Open In App

Line plot styles in Matplotlib

Last Updated : 23 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Line plots are important data visualization elements that can be used to identify relationships within the data. Using matplotlib.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 in Matplotlib.

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
import matplotlib.pyplot as plt
import random as random
students = ["Jane","Joe","Beck","Tom",
            "Sam","Eva","Samuel","Jack",
            "Dana","Ester","Carla","Steve",
            "Fallon","Liam","Culhane","Candance",
            "Ana","Mari","Steffi","Adam"]
marks=[]
for i in range(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
import matplotlib.pyplot as plt
import random as random

students = ["Jane","Joe","Beck","Tom","Sam",
            "Eva","Samuel","Jack","Dana","Ester",
            "Carla","Steve","Fallon","Liam","Culhane",
            "Candance","Ana","Mari","Steffi","Adam"]
marks=[]
for i in range(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
import matplotlib.pyplot as plt
import random as random

students = ["Jane", "Joe", "Beck", "Tom",
            "Sam", "Eva", "Samuel", "Jack",
            "Dana", "Ester", "Carla", "Steve",
            "Fallon", "Liam", "Culhane", "Candance",
            "Ana", "Mari", "Steffi", "Adam"]
marks = []
for i in range(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.



Next Article

Similar Reads