Friday, August 26, 2022

How to change curve color in Matplotlib plot ?

To change the color in the matplotlib plot function simply type the "color = color_name". The color abbreviations that are supported are given in Table 1.
Table 1 - Types of curve colors.
AbbreviationColor
'b'blue
'g'green
'r'red
'c'cyan
'm'magenta
'y'yellow
'k'black
'w'white
All the colors will be tested in the plot function.
Example 1 - plot functions \(y_1 = \sin x\), \(y_2 = \cos x\), \(y_3 = 0.5*x+2\), \(y_4 = 5*x\), \(y_5 = 5x-3\), \(y_6 = 2\sin 2x\), \(y_7 = \cos(2x)\), and \(y_8 = 2x\). The x for all functions will be set in the range of 0 to 20.
Solution: To plot these functions we will need numpy and matplotlib.pyplot library.
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,20.1,0.1)
y1 = [];y2=[];y3 = []; y4=[]; y5=[]; y6=[]; y7=[]; y8=[]
for i in range(len(x)):
y1.append(np.sin(x[i]))
y2.append(np.cos(x[i])
y3.append(0.5*x[i]+2)
y4.append(5*x[i])
y5.append(5*x[i]-3)
y6.append(2*np.sin(2*x[i]))
y7.append(np.cos(2*x[i])
y8.append(2*x[i])
Now it's time to plot all functions. Each function will be plotted in a different color so which is shown in Table 1.
plt.plot(x,y1,color ='b')
plt.plot(x,y2,color='g')
plt.plot(x,y3,color='r')
plt.plot(x,y4,color='c')
plt.plot(x,y5,color='m')
plt.plot(x,y6,color='y')
plt.plot(x,y7,color='k')
plt.plot(x,y8,color='w')
plt.show()
The result of the previous chunk of code is shown in Figure 3.
Figure 3 - graphical representation of 8 different curves in different colors
Besides abbreviations the colors can be specified in full names ('blue'), hex strings, RGB or RGBA tuples or grayscale intensities as a string. The different color type definitions will be shown in following figure.
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,4.1,0.1)
y1=[]; y2=[]; y3=[]
y4=[]; y5=[]; y6=[]
y7=[]; y8=[]
for i in range(len(x)):
y1.append(np.sin(x[i]))
y2.append(np.cos(x[i]))
y3.append(0.5*x[i]+2)
y4.append(5*x[i])
y5.append(5*x[i]-3)
y6.append(2*np.sin(2*x[i]))
y7.append(np.cos(2*x[i]))
y8.append(2*x[i])
plt.plot(x,y1,color='blue',label="color - blue")
plt.plot(x,y2,color='#008000',label="color-#008000")
plt.plot(x,y3,color=(0.1,0.2,0.5),label="color-(0.1,0.2,0.5)")
plt.plot(x,y4,color=(0,1,0.5,1), label= "color-(0,1,0.5,1)")
plt.plot(x,y5,color=('0.8'), label="color-0.8")
plt.plot(x,y6,color='#33FF88', label="color-#33FF88")
plt.plot(x,y7,color=(0.56,0.03,0.67), label="color - (0.56, 0.03,0.67)")
plt.plot(x,y8,color='m',label="color - m")
plt.legend()
plt.show()
In previous code, the argument label = label_name of plot function was introduced, and also the legend function. The plt.legend function takes all labels and creates the legend block inside the graph. After running the previous code the graph is obtained which is shown in the following figure.
Figure 4 - graphical representation of 8 different curves in different colors.
The legend function is introduced in this example as necessety, for more details about legend see plt.legend() post (HYPERLINK MISSING).

No comments:

Post a Comment