0
0
Matplotlibdata~5 mins

Named colors and hex codes in Matplotlib

Choose your learning style9 modes available
Introduction

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.

You want to make a chart with specific colors to match a theme.
You need to highlight parts of a graph with clear, distinct colors.
You want to use colors that everyone can recognize by name.
You want to use custom colors by specifying their hex codes.
You want consistent colors across different charts or reports.
Syntax
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.

Examples
This uses the named color 'green' for the line.
Matplotlib
plt.plot(x, y, color='green')
This uses a hex code for a bright orange color.
Matplotlib
plt.plot(x, y, color='#FF5733')
Scatter points colored with the named color 'navy'.
Matplotlib
plt.scatter(x, y, color='navy')
Sample Program

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.

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()
OutputSuccess
Important Notes

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).

Summary

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.