Styling & Themes
Matplotlib is a Python library for creating charts and visualizations — and its built-in style sheets let you restyle every chart's colors, fonts, and grids with a single line instead of tweaking each element by hand.
Learn Styling & Themes in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll list the available styles, apply themes like 'ggplot' and 'dark_background' globally, and switch styles temporarily with a context manager.
A style is a named bundle of defaults — colors, line widths, fonts, grid lines, and background — that controls how every chart looks. Before applying one, see what's available with plt.style.available .
What you'll see: a printed Python list of style names. No chart is drawn here — this just tells you which themes you can pass to plt.style.use() in the next step.
Call plt.style.use('name') once , near the top of your script, and every chart you draw afterward picks up that theme. The classic 'ggplot' style gives you a gray background with white gridlines, inspired by R's ggplot2.
What you'll see: the familiar line plot, but now on a light-gray panel with bold white gridlines and a reddish default line color — the signature ggplot look, applied without styling any element by hand.
Prefer a dark look? Swap in 'dark_background' , which inverts everything to light text and lines on a black canvas — great for slideshows.
What you'll see: the same rising parabola, but drawn in a bright line on a black background with light-colored axis text and ticks — ideal for dark presentations.
Because plt.style.use() is global, it sticks around for the rest of your program. When you want a theme for just one chart, wrap it in with plt.style.context('name'): — the style applies inside the block and is dropped automatically afterward.
What you'll see: two windows. The first uses the soft, colorful bmh theme; the second reverts to Matplotlib's plain default — proof the context manager only styled the chart inside its block.
Replace each ___ to apply the 'fivethirtyeight' theme and plot some data.
❌ OSError: 'seaborn' is not a valid package style
The plain seaborn names were renamed. Use 'seaborn-v0_8' (or check plt.style.available ).
plt.style.use() is global. For a one-off, use with plt.style.context('name'): instead.
Draw the same data once with 'ggplot' globally, then once temporarily with 'dark_background'.
Lesson 12 complete — your charts now have style!
You listed the available themes, applied ggplot and dark_background globally, and scoped a style to a single chart with a context manager.
🚀 Up next: Annotations & Text — point arrows at the interesting parts of your chart.
Practice quiz
What does a Matplotlib style sheet bundle together?
- Default colors, fonts, grids, and background for every chart
- Only the figure size
- The data passed to plot()
- A list of saved image files
Answer: Default colors, fonts, grids, and background for every chart. A style is a named bundle of defaults that controls how every chart looks.
Which call lists every available built-in style name?
- plt.style.list()
- plt.style.available
- plt.show_styles()
- plt.styles()
Answer: plt.style.available. plt.style.available returns the list of style names your installation knows about.
How do you apply a theme globally for the rest of the program?
- plt.theme('ggplot')
- plt.set_style('ggplot')
- plt.style.use('ggplot')
- plt.apply('ggplot')
Answer: plt.style.use('ggplot'). plt.style.use('name') sets that style globally for every chart drawn afterward.
Which theme gives light text and lines on a black canvas?
- 'ggplot'
- 'bmh'
- 'fivethirtyeight'
- 'dark_background'
Answer: 'dark_background'. 'dark_background' inverts everything to light elements on black, great for slideshows.
How do you apply a style to just one chart and drop it afterward?
- with plt.style.context('bmh'):
- plt.style.once('bmh')
- plt.style.temp('bmh')
- plt.style.use('bmh', temp=True)
Answer: with plt.style.context('bmh'):. The context manager applies the style inside its block and reverts automatically.
What happens to charts after plt.style.use('ggplot')?
- Only the next chart uses ggplot
- Every chart from then on uses ggplot until changed
- Nothing changes until you call show()
- Only bar charts are affected
Answer: Every chart from then on uses ggplot until changed. plt.style.use() is global and sticks around for the rest of the program.
Why were the plain 'seaborn' style names renamed?
- They were deleted entirely
- Seaborn became a paid library
- Modern Matplotlib uses names like 'seaborn-v0_8' instead
- They only work in Jupyter
Answer: Modern Matplotlib uses names like 'seaborn-v0_8' instead. Use 'seaborn-v0_8' (or check plt.style.available); plain 'seaborn' is no longer valid.
What does plt.style.use(['seaborn-v0_8', 'dark_background']) do?
- Raises an error — only one style is allowed
- Picks one style at random
- Ignores the list entirely
- Applies styles left to right, later ones override earlier
Answer: Applies styles left to right, later ones override earlier. A list of styles is applied in order, so later styles win where they overlap.
Which classic theme is inspired by R's ggplot2?
- 'ggplot'
- 'classic'
- 'grayscale'
- 'tableau'
Answer: 'ggplot'. 'ggplot' gives a gray background with white gridlines, inspired by ggplot2.
What error appears if you pass an outdated 'seaborn' style?
- SyntaxError
- OSError: 'seaborn' is not a valid package style
- ValueError: bad color
- ImportError: no seaborn module
Answer: OSError: 'seaborn' is not a valid package style. The plain seaborn names were renamed; use 'seaborn-v0_8' or check plt.style.available.