Matplotlib Linestyle Implementation in Python

Matplotlib Linestyle Implementation in Python : Only 4 Steps

The line chart is very useful for finding the relationship between the two data points. It also helps you to do good analysis and find patterns that are helpful in prediction like linear regression. But if you are plotting multiple lines on the same figure then you are unable to do a better analysis if the chart style is the same. Matplotlib provides linestyle argument that allows you to style a line plot. In this entire tutorial, you will know how to implement Matplotlib linestyle in python with steps.

Steps to Implement Matplotlib Linestyle

In this section, you will implement linestyle with steps. I am doing all the coding part on the Jupyter Notebook, so make sure that you should follow with me for better understanding.

Step 1: Import all the necessary packages

The first step is to import all the required libraries that will be used in our examples. I am using only two libraries Matplotlib and NumPy. The use of Numpy is to createe sample data points.

Let’s import all the libraries.

import numpy as np
import matplotlib.pyplot as plt

Step 2: Create Multiple Data Points

Now for the demonstration purpose, I am creating three sin data points for plotting multiple line charts. For this, I have to use the NumPy method sin() method. Let’s create the datapoints

x = np.linspace(0, 20, 1000)
y1= np.sin(x)
y2= np.sin(x)*2
y3 =np.sin(x)*5

Here the linspace() method will create data points from 1000 sample values from the range 0 to 20. The y1, y2, and y3 are the dependent variables on the x that we created before.

Step 3: Plot the Line chart

After the creation of the data points in step 2. Let’s plot all of them. Execute the below lines of code and run it.

%matplotlib inline
plt.figure(figsize=(20,5))
plt.plot(x,y1)
plt.plot(x,y2)
plt.plot(x,y3)

Explanation

Here the %matplotlib inline is used to plot the figure inline. It is used in Jupyter Notebook. If you run the code in Pycharm or other IDEs then it is not required. The plt.figure(figsize=(20,5)) method is used to change the size of the plot. In our case, it is 20 and 5 inches in width and height respectively.

If you run the above code then you will get the output as below.

Plotting multiple line charts on the same figure
Plotting multiple line charts on the same figure

You can see all the line plots are in different colors but the linestyle is the same. In the next step, I will set the style of each line differently.

Step 4: Use the Matplotlib Linestyle

Now I am plotting the same data points but with a different line style. You can find all the matplotlib linestyles using the below code.

from matplotlib import lines
lines.lineStyles.keys()

Output

List of Matplotlib linestyle
List of Matplotlib linestyle

Now I will use the first three linestyle for each line chart and it is “-“, “–” and “-.”. Just run the below lines of code and see the output.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 20, 1000)
y1= np.sin(x)
y2= np.sin(x)*2
y3 =np.sin(x)*5

%matplotlib inline
plt.figure(figsize=(20,5))
plt.plot(x,y1,linestyle='-')
plt.plot(x,y2,linestyle='--')
plt.plot(x,y3,linestyle='-.')

 

Output

Multiple Line charts with different linestyle
Multiple Line charts with different linestyle

You can see all the line charts have different linestyle. Now you can easily remember the line of each datapoints.

Conclusion

Its best practice to style the charts if you are plotting multiple charts on the same figure. These are steps to implement Matplotlib Linestyle with steps. Hope you have liked this tutorial. If you have any query regarding this then you can contact us for more help.

Source:

Matplotlin Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner