In this article, we will learn how to to set different line color and marker color in Matplotlib plot. But if have seen my earlier article, I showed you how we can set colors to markers. Right? So this will be a follow up on that article. Alright?
So what is the problem we are trying to solve here? Well you see, we want to have a plot with lines connecting markers. But then main thing is that we want different line color and the marker color in it! So how can we do that? Let me explain!
So first, let us take a look at our earlier plot. This is how it looked like, right?
So as you can see in the plot above, we had changed the color of the triangle markers to magenta color. Right? So what was the code we used to generate this plot? Let us take a look at it as well:
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], 'm^') plt.show()
So we used the color code ‘m‘ in the third plot to get the color. Right? But what is missing here? Well, you can see that there are no lines drawn. Correct? So how do we go about fixing that? And more importantly, how can we add lines with different colors?
Well, to do that we will need to use certain keywords in the plot( ) function! That is the key to solving this problem! Does that make sense? Great!
So then what keyword do we need to use? Well you see, Matplotlib gives us a lot of keywords to use when plotting. So there are special keys for setting line color as well as marker color!
But how do these keywords look like? Let me explain.
Keywords To Use For Different Line Color And Marker Color In Matplotlib
There are three keywords we can use to set the color of a line and the marker. They are:
color or c – So by using a color or c keyword in our plot( ) function, we can set the line color of a plot.
markeredgecolor – By using this keyword, we will tell Matplotlib what color to use to draw the edges of our marker.
markerfacecolor – By using this keyword, we can tell Matplotlib what color to use for the face of our marker.
These are the 3 keywords than we can use to set different line color and marker color in Matplotlib. So now that we know what to use, let us next see how we can use it. Alright?
What better way than to use our previous plot and to change it’s color? Right? So let us do just that!
Let us change the color of our plot line to be Yellow while the triangles to be Red with a green border. Alright? So how will our plot then look like? Any guess?
Well, take a look at it yourself below:
Woah! That is nice, right? We now have total control over the colors we can use in our plots, right? So what is the code change we did to get this? Take a look at the code for yourself!
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], color='yellow', marker='^', markeredgecolor='green', markerfacecolor='red') plt.show()
So there is a tiny little change we have done to get this working. As said earlier, we simply used the keywords to set the color like we want. And it did work as we wanted. Right?
So that is all there is to set different line color and marker color in Matplotlib. You just need to use the right keyword and it will work like a charm!
So with that, I will end this article now. But if you have any doubt about it, do let me know in the comment below. I will be more than happy to help!
So until next time, take care! 🙂