Line plot styles in Matplotlib (original) (raw)

Last Updated : 23 Apr, 2025

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.

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.

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.

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.