Colors help make charts easy to understand and nice to look at. Using named colors or hex codes lets you pick exact colors for your plots.
Named colors and hex codes in Matplotlib
plt.plot(x, y, color='color_name') plt.plot(x, y, color='#RRGGBB')
Use color='color_name' to pick a named color like 'red' or 'blue'.
Use color='#RRGGBB' to pick a color by its hex code, where RR, GG, BB are two-digit hex numbers.
plt.plot(x, y, color='green')plt.plot(x, y, color='#FF5733')plt.scatter(x, y, color='navy')This code draws two lines: one red using a named color, and one blue using a hex code. It shows how to use both color types in matplotlib.
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y1 = [10, 20, 25, 30] y2 = [30, 23, 15, 10] plt.plot(x, y1, color='red', label='Red line') plt.plot(x, y2, color='#0000FF', label='Blue line') plt.title('Example of Named Colors and Hex Codes') plt.xlabel('X axis') plt.ylabel('Y axis') plt.legend() plt.show()
Named colors are easy to remember but limited to common colors.
Hex codes give you millions of colors by mixing red, green, and blue.
You can find lists of named colors in matplotlib documentation or by running import matplotlib.colors as mcolors; print(mcolors.CSS4_COLORS).
Use named colors or hex codes to pick colors in matplotlib plots.
Named colors are simple words like 'red', 'blue', 'green'.
Hex codes start with '#' and use six hex digits to define colors.