The best thing about Matplotlib is that it supports various color formats that make it easier for you to specify colors in the plot:
1. CSS4 color names: It is the easiest way of specifying colors. Colors such as beige, aquamarine, lime, chocolate, and others are CSS color names. For a complete list of CSS colors, visit Matplotlib documentation.
2. CSS color abbreviations: These are also called base colors, and they are eight in number:
Base Colors | Description |
---|---|
r | red |
g | green |
b | blue |
c | cyan |
m | magenta |
y | yellow |
k | black |
w | white |
3. Hexadecimal RGB color value: It is a case-insensitive hexadecimal RGB string. It has the following format:
#RRGGBB
The value of each digit ranges from 0 to F.
4. Hexadecimal RGBA color value: It is also a case-insensitive hexadecimal RGBA string. Here, A refers to the alpha value and provides transparency to the color. RGBA value looks like this #RRGGBBAA
. The value of each digit ranges from 0 to F.
5. Tableau color: There are ten colors in the Tableau palette. Here, you specify the color like this tab:color
. Following are the ten colors that come under the Tableau palette:
tab:red, tab:green, tab:blue, tab:orange, tab:purple, tab:cyan, tab:olive, tab:gray, tab:brown, and tab:pink.
6. Property cycle color: These are 10 colors in the property cycle, and their value is case-sensitive. It is written as Cx, where x is a number from 0 to 9. Note that C must be in a capital letter; otherwise, it won't work. The following table shows the color value of property cycle color:
Property Cycle Color | Description |
---|---|
C0 | #1f77b4 |
C1 | #ff7f0e |
C2 | #2ca02c |
C3 | #d62728 |
C4 | #9467bd |
C5 | #8c564b |
C6 | #e377c2 |
C7 | #7f7f7f |
C8 | #bcbd22 |
C9 | #17becf |
Note: Apart from the size color formats, Matplotlib also supports xkcd color survey.
Let's learn how to specify colors in Matplotlib using an example.
import matplotlib.pyplot as plt plt.plot([-3,-2,-1,0,1,2,3], [0,1,2,3,4,5,6], color='red', label='CSS color name') plt.plot([-3,-2,-1,0,1,2,3], [-1,0,1,2,3,4,5], color='g', label='CSS color abbreviation') plt.plot([-3,-2,-1,0,1,2,3], [-2,-1,0,1,2,3,4], color='#3366CC', label='RGB color') plt.plot([-3,-2,-1,0,1,2,3], [-3,-2,-1,0,1,2,3], color='#F3993EF0', label='RGBA color') plt.plot([-3,-2,-1,0,1,2,3], [-4,-3,-2,-1,0,1,2], color='tab:pink', label='Tableau color') plt.plot([-3,-2,-1,0,1,2,3], [-5,-4,-3,-2,-1,0,1], color='C9', label='Property cycle color') plt.legend(bbox_to_anchor=(1.5, 1.0)) plt.title('Matplotlib Colors Example') plt.xlabel('X Axis') plt.ylabel('Y Axis')
Output
In this code, all the six different ways of setting colors are shown.