If you have been working with Matplotlib to plot lines, you might be looking for how you can style lines. Right? So if that is the case, they you have come to the right place. Because in this article, we will learn how to change line style in Matplotlib.
But before we do that, I hope you already know how to plot multiple lines on Matplotlib. But if you do not know, then take a look at the article linked above. That should get you going. Alright?
Okay then. With that, it is time for us to get started on changing the style of our lines now! Are you ready?
Drawing Multiple Lines Using Matplotlib
So as I said in the earlier section, we first need to have the code to draw multiple lines. Here is that code we have from our earlier article:
import matplotlib.pyplot as plt x = range(1, 10) plt.plot(x, [xi*1 for xi in x]) plt.plot(x, [xi*2 for xi in x]) plt.plot(x, [xi*3 for xi in x]) plt.show()
So this is the same piece of code we have taken from our earlier article. When we run this code, we get the following output:
So as you can see from the above pic, we have 3 different lines plotted on the same graph. But what is even more important is that all these lines are in different color. That is good. Right?
But what we want to do now is to change the line style in this Matplotlib output. Correct? So how do we go abut doing that?
How To Change Line Style In Matplotlib?
With the basic multi line plot ready, let us now see how we can change the style of these lines.
So how do we go about doing this? Any guess? I want you to think a little bit to see if you can make a guess before you look for the answer. Alright? Just take a minute or two to think it over before you read further.
Okay. Here is what we can do to change the line style that we draw using Matplotlib:
Just like we could change the color of our lines ourselves, we can also change the style of these lines. So how did we change the color of our lines using Matplotlib to what we want, earlier? Yes, we passed in a new parameter to our plot( ) function. Right?
So in the same way, we can change the line style in Matplotlib as well. By passing our desired style as a parameter to our plot( ) function!
So how does that look like? Well, we just need to pass in the style we want as another plot( ) function parameter. So we modify our code in the plt.plot( ) function as shown below:
plt.plot(x, [xi*1 for xi in x], '-.') plt.plot(x, [xi*2 for xi in x], '--') plt.plot(x, [xi*3 for xi in x], ':')
So what happens when you do that? You have just changed the line style of the plots. So here is how it now looks like:
Now that looks like a perfect plot, isn’t it? But this begs us to ask the next question:
What Are The Options Available To Change The Line Style In Matplotlib
So there are 4 line styles that Matplotlib provides us to choose from. They are:
|Style Symbol||Style Type|
|-.||Dash Dot Line|
So if you choose from any one of these styles, you can get some pretty looking plots out there!
So that is all there is to changing the line style of a Matplotlib plot! You just have to add a new parameter to the plot( ) function with the style information. That is it and then it just works!
Pretty easy way to achieve what we want then, right? 😉 That is the beauty of Matplotlib library. It has so many interesting features with such a simple API!
So then what does our final code look like? Here it is:
import matplotlib.pyplot as plt x = range(1, 10) plt.plot(x, [xi*1 for xi in x], '-.') plt.plot(x, [xi*2 for xi in x], '--') plt.plot(x, [xi*3 for xi in x], ':') plt.show()
So with that, I will end this article now. But if you still have any questions, do let me know in the comment below. I will be more than happy to help! Alright?
So until next time, take care guys and gals out there! Have a great day! 🙂