Skip to content
LearnPY
Matplotlib

Style and annotate the message

Choose color with purpose, remove visual noise, annotate the important point, and keep the chart readable without color.

11 min readbeginnermatplotlib, numpy

By the end

  • Use a restrained palette with enough contrast.
  • Remove spines and grid lines that do not help comparison.
  • Annotate a specific data point directly.
  • Reuse a Matplotlib style safely.
  • Keep labels and markers meaningful in grayscale.

Decide what the eye should see

Styling is not decoration. Every line, grid, spine, and label competes for attention. Keep the data prominent, make labels easy to read, and remove chart furniture that does not help the comparison.

Python

A quieter frame around the data

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

Use color with purpose

Color can separate series, encode a category, or highlight one observation. It should not be the only difference between series. Pair hue with markers, line styles, or direct labels so the chart still works in grayscale.

Python

Distinct colors and distinct markers

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

Annotate the point that matters

A direct annotation explains an unusual point where it appears. ax.annotate connects text at xytext to a data coordinate at xy. Use it for a peak, a gap, or a threshold rather than making the reader hunt through the caption.

Python

A label connected to the data

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

Annotation coordinates are in data units for both xy and xytext by default. If the text lands off the axes, adjust the offset or use axes-fraction coordinates deliberately.

Reuse a style without surprises

Matplotlib ships with named styles. plt.style.use changes the active settings globally; plt.style.context applies a style only inside a block. The context manager is safer when one figure needs a different visual system.

Python

Apply a style to one figure

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

A style changes defaults, not your data. Still provide titles, units, and legends; a theme cannot supply the meaning of the chart.

Export a chart worth keeping

Use savefig with a vector format such as PDF or SVG for reports, or PNG at a deliberate DPI for the web. bbox_inches="tight" removes excess whitespace. In this runner, open figures are captured automatically.

Python

The figure captured by this runner

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

Practice

Make it stick

Run the tests as often as you like. Hints and the solution are available when you need them.

Exercise

Plot signups with color "#2563eb", marker "o", and line width 2. Add an annotation containing the word "Peak" at the maximum value. Set the title to "Monthly signups" and hide the top and right spines.

Your solution

Ready
Loading editor…

Results

Run the tests to check your solution. The first run downloads Python; later runs reuse the session.

Quiz

Check your understanding

01Why pair color with a marker or line style?
02What does ax.annotate connect?
03What is the advantage of plt.style.context over plt.style.use?

Lesson progress