Python Line Plot Styles in Matplotlib

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

Example 1: Plotting a Simple Line Plot Styles in Matplotlib

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

Python3




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.xlabel("Students")
plt.ylabel("Marks")
plt.title("CLASS RECORDS")
plt.plot(students,marks,'m--')


Output:

Example 2: Adding Markers to Line Plots

In this example, we visualize the marks of 20 students using a line plot. Each student’s name is plotted against their corresponding mark. The line is solid green, and data points are marked with red circles. By this example, we can understand how to change the line styles and markers in Matplotlib.

Python3




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.xlabel("Students")
plt.ylabel("Marks")
plt.title("CLASS RECORDS")
plt.plot(students, marks, color = 'green',
         linestyle = 'solid', marker = 'o',
         markerfacecolor = 'red', markersize = 12)


Output:

Example 3: Adding Grid Lines in Line Plots

In this example, we use Matplotlib to visualize the marks of 20 students in a class. The marks are displayed using a dashed magenta line graph. Grid lines are added to provide better readability and reference across the plot.

Python3




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.xlabel("Students")
plt.ylabel("Marks")
plt.title("CLASS RECORDS")
plt.plot(students, marks, 'm--')
 
# Adding grid lines
plt.grid(True, which='both', linestyle='--', linewidth=0.5)
 
plt.show()


Output:

Related Article: Matplotlib.pyplot.colors() in Python



Line plot styles in Matplotlib

Python is a high-level, interpreted, and dynamically typed programming language that can be used to manage huge datasets. Python supports a wide variety of data visualization libraries like Matplotlib, Seaborn, Bokeh, Geoplotlib, Ggplot, and Plotly. Among all these libraries, Matplotlib is comparatively simple and easy to implement.

The Matplotlib library of Python is a popular choice for data visualization due to its wide variety of chart types and its properties that can be manipulated to create chart styles. The matplotlib.pyplot.plot(*args, **kwargs) method of matplotlib.pyplot is used to plot the graph and specify the graph style like color or line style. 

Similar Reads

Line Styles in Matplotlib

Below are the available line styles present in Matplotlib....

Python Line Plot Styles in Matplotlib

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