Skip to content

Legends and Colors

Legends

Legends are needed when you plot multiple series.

Legend
import matplotlib.pyplot as plt
 
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 2, 3, 4, 5]
 
plt.figure(figsize=(7, 4))
plt.plot(x, y1, label="square")
plt.plot(x, y2, label="linear")
plt.title("Two series")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
plt.tight_layout()
plt.show()
Legend
import matplotlib.pyplot as plt
 
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 2, 3, 4, 5]
 
plt.figure(figsize=(7, 4))
plt.plot(x, y1, label="square")
plt.plot(x, y2, label="linear")
plt.title("Two series")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
plt.tight_layout()
plt.show()

Colors

Use:

  • high-contrast colors
  • colorblind-friendly palettes when possible
  • consistent color meaning across charts
Custom color
import matplotlib.pyplot as plt
 
plt.figure(figsize=(7, 4))
plt.plot([1, 2, 3], [3, 2, 5], color="#1f77b4")
plt.title("Custom color")
plt.tight_layout()
plt.show()
Custom color
import matplotlib.pyplot as plt
 
plt.figure(figsize=(7, 4))
plt.plot([1, 2, 3], [3, 2, 5], color="#1f77b4")
plt.title("Custom color")
plt.tight_layout()
plt.show()

Tip

Avoid using too many colors in one chart.

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did