Categories
DATA MINING DATA SCIENCE DATA VISUALIZATION MACHINE LEARNING PROGRAMMING PYTHON TUTORIALS

How To Change Line Color In Matplotlib

When we draw the lines in Matplotlib, it uses default colors to draw the plot. However, what if we want to change the line color in Matplotlib to our liking? Is there a way we can do it? In this article, we will try and learn how to do just this.

How To Draw Lines In Matplotlib

But before we change the line color, we need to know how to draw the lines in Matplotlib. We need to know what functions to use and in what order to call them. But lucky enough, we already have an article that explains how to do just this.

So if we simply follow the linked article, we get a graph that looks like this:

Matplotlib multiple lines same graph
Matplotlib plot with multiple lines

And we got the above plot by running the following code:

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 just to help you better, let us go through the code once again.

The second line in the code is how we get a set of values between 1 to 10 stored into our array x.

Now, the next three lines of code is how we draw the lines using Matplotlib. The plot function is the one responsible to draw our lines. Since this function is called 3 times, we get 3 lines drawn.

Also, you can see that the plot function is taking x values as its input parameters. Now since we want 3 different lines, we cant pass the same value of x in all three plot() function calls. Therefore, we multiplied x with 1, 2 & 3 respectively for each of the three lines. So that is how we got the above Matplotlib plot with three lines.

But wait a second! How are the colors of these three lines set? How are the three lines drawn in different colors? Who made them do it?

Well the answer is, it was all done by Matplotlib. Because the default behavior of Matplotlib is to draw lines in different colors if color is not specified. So now you know it!

With this, let us now see how we can change the color of these lines.

How To Change Line Color In Matplotlib

Ok so how do we go about changing colors in Matplotlib? Can we get any help from Matplotlib here?

Luckily, yes! Since changing a line’s color is such a basic requirement that Matplotlib has included it as part of the plot() function itself!

Yes, you read that right. We can change the color of our line by using our plot() function itself. So far we have used plot() function by passing only parameter x & y. However, it also supports another parameter that we can use to set the color of the line it draws. So if we pass a third parameter for the color we want, it draws line using that color! Cool, isn’t it?!

So this begs us the next question. What type of values ca we pass to specify a color to the plot() function?

Different Color Parameter Values Accepted By The Plot Function

The Matplotlib’s plot() function supports 3 different types of parameter values:

1. Predefined color set

One is a predefined set of values. This is shown in the following table:

charactercolor
‘b’blue
‘g’green
‘r’red
‘c’cyan
‘m’magenta
‘y’yellow
‘k’black
‘w’white

2. Hexadecimal Color Codes

Since the color drawn by Matplotlib is same as what we use in web development as well, we can use the same Hexademical color codes. So all the colors between #000000 to #FFFFFF becomes valid!

3. RGBA Color Codes

And the final type of color code it supports is the RGBA color code. Again, this is similar to what we see in our web development activity. Hence color codes like (0,1,0,1) become valid color codes again!

So now that we know the different color codes we can pass, let us modify our code to use them.

Now for the sake of simplicity, I will choose the first option. That is, use the values from Matplotlib’s predefined color set. So from the color set, we can see that there are 8 values to choose from. Since we have three lines to draw, let me pick these 3 colors – yellow, red & black.

So with our colors now selected, let us modify the code to add these values. Since we only need to modify three lines of code calling plot(), the final looks like this:

import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x], 'y')
plt.plot(x, [xi*2 for xi in x], 'r')
plt.plot(x, [xi*3 for xi in x], 'k')
plt.show()

Since the three colors are represented by ‘y’, ‘r’ & ‘k’, I just plugged in those values to our plot() function call. So just like that, we get the below output plot with changed line colors:

Matplotlib Plot with changed line colors
Matplotlib Plot with changed line colors

So, there you have it! Since Matplotlib provided us with such a simple solution, this was pretty straight forward. But not all features will be as easy as this one. But we will tackle each one of these problems as and when they appear. So with that I will conclude this article over here. But if you still have any doubts about this, do let me know in the comments below. Until next time, ciao! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *