So in this article, we will learn how we can change the marker color in a Matplotlib plot. We will first see how we draw these markers and then see what we can do to change their colors.
Does that sound good? Great! Then let us start right away!
But before we start looking on how to change the Marker color of a plot, we need a plot. Right? But where do we get one?
Well, how about we make use of the plot we got from our previous article:
That plot should be good enough. Right? So we will use just that!
Here is how the plot then looks like:
But what do we see here? We are seeing that each of the line marker in the plot already has different color. Right? But how did that happen? Who set the color for these markers?
Well the answer to that lies in the default behavior of the Matplotlib library. Because, even if we did not set those color, the library did it by itself. It made sure that each of the line markers got a different color.
That is cool! right? Because in that case, we will not have to worry about setting color ourselves. Isn’t it?
Well yes. That is true for most of the time. But there are times when we want to set the markers with a specific color. So having an option to change the marker color In Matplotlib is still needed. Right?
So then how can we do that? Well that is when the parameters of plot( ) function once again helps us! Here is how we can use it to change the marker color.
Change Marker Color In Matplotlib
So before we look at how to change the marker color in Matplotlib, let us look at current code. The code that is responsible for the plot created above. This is how that code looks like:
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 using the above code we got three sets of markers in the above plot, right? And each set had a different color set to it. But what if I want the triangle in the first set to be in the color of magenta?
Well, luckily we can do that! How? By passing our desired color value to the plot( ) function. So the code for that will then look like this:
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()
Notice the addition of the color value “m” to our third plt.plot( ) function call? That is what will do the trick for us! Here the alue “m” stands for the color “magenta”. This tells the plot( ) function to draw the triangles using magenta color!
So then how does our final plot look like then? Take a look at it for yourself!
So what do you see?!
As you can see, the color of the triangles have changed from green to magenta. And that is what we wanted. Right? 😉
But then you must be asking what are all the available colors that you can use? Right? Well, they are the same set of colors that you used while changing line color in Matplotlib earlier! So it is quite easy then. Isn’t it?
So there you have it! That is how you change the marker color In Matplotlib. With this, I will end this article now. But if you have any questions, do let me know in the comments below.
So until next time, take care!