Styling and Colours
Python · Data Visualization
Theory
Colour should carry meaning, not decoration. For categories (cities, products) use a qualitative palette of clearly different hues, such as matplotlib's default 'tab10'. For ordered values (low to high sales) use a sequential colormap like 'viridis' or 'Blues', where lightness changes steadily. For values around a midpoint (profit vs loss, correlation from -1 to 1) use a diverging map such as 'RdBu', with the midpoint in the centre.
A strong technique is highlighting: draw everything in grey and give only the bar that matters a colour. Colours can be names ('tab:blue'), hex codes ('#1f77b4') or a list, one per bar. plt.style.use('ggplot') or a temporary with plt.style.context('ggplot'): block changes the overall look, and plt.rcParams['font.size'] = 12 sets defaults. Avoid red-green pairs, which many colour-blind readers cannot tell apart.
Why we use it
Good colour choices make the important point jump out and keep charts readable for everyone, including colour-blind readers. Poor choices, such as rainbow maps for ordered data, can mislead or hide the message.
When to use it
Use a qualitative palette for categories, sequential for magnitudes, diverging for values around zero, and grey plus one accent to highlight. Set a style once at the top of a notebook so all charts match.
How it works
- What does colour need to show?
- Different categories? Yes: Qualitative palette (tab10). No: Continue.
- Values with a meaningful midpoint (e.g. 0)? Yes: Diverging map (RdBu). No: Sequential map (viridis, Blues).
- Highlight the key item; mute the rest in grey
- Check it still reads in greyscale or for colour-blind readers
- Colour that carries meaning
Syntax
ax.bar(x, y, color=['lightgray', 'tab:orange', ...])
ax.scatter(x, y, c=values, cmap='viridis')
plt.style.use('ggplot')
with plt.style.context('ggplot'): ...
plt.rcParams['font.size'] = 12Example
colors = ['lightgray', 'lightgray', 'tab:orange']
ax.bar(['Jaipur', 'Pune', 'Delhi'], [120, 90, 200], color=colors)Key points
- Qualitative palettes for categories, sequential for magnitude
- Diverging maps (RdBu) for values around a midpoint
- Grey everything, colour one thing, to highlight
- Avoid red vs green; prefer blue vs orange
- plt.style.context applies a style to one block only