Built-in Styles in Matplotlib
Learn how to use predefined Matplotlib styles to improve the appearance of charts without manually changing every plotting property.
You will explore available styles, apply different themes, compare chart appearances and temporarily use styles in your Python programs.
1. Learning Objectives
After completing this lesson, you will be able to:
- Understand what built-in styles are in Matplotlib.
- Explain why styles are useful in data visualization.
- List the styles available in your Matplotlib installation.
- Apply a built-in style using
plt.style.use(). - Understand popular styles such as ggplot, classic and dark_background.
- Temporarily apply a style using
plt.style.context(). - Reset the plotting style to the default configuration.
2. Introduction to Built-in Styles
Matplotlib provides different predefined styles that help developers change the overall appearance of charts. These styles are known as built-in styles.
Normally, when creating a chart, we may need to configure the background colour, grid lines, font sizes, line colours and other properties manually.
With built-in styles, we can apply a predefined collection of visual settings using a single command.
Example
Instead of manually changing multiple properties, we can simply write:
import matplotlib.pyplot as plt
plt.style.use('ggplot')
Matplotlib applies the settings associated with the selected style to subsequently created plots.
3. Why Do We Use Built-in Styles?
Built-in styles make charts visually consistent and reduce the amount of formatting code required.
| Benefit | Description |
|---|---|
| Save Time | Apply multiple visual settings with a single command. |
| Consistent Design | Maintain a similar appearance across multiple charts. |
| Better Readability | Use suitable backgrounds, grids and colours. |
| Professional Appearance | Choose a style that matches the purpose of the visualization. |
| Less Code | Avoid repeatedly writing the same formatting properties. |
4. Checking Available Built-in Styles
Matplotlib provides a collection of built-in styles. The available styles can depend on the installed Matplotlib version.
We can use plt.style.available to get the list of
styles supported by the current installation.
Python Code
import matplotlib.pyplot as plt
print(plt.style.available)
Output
[
'Solarize_Light2',
'_classic_test_patch',
'_mpl-gallery',
'_mpl-gallery-nogrid',
'bmh',
'classic',
'dark_background',
'fast',
'fivethirtyeight',
'ggplot',
'grayscale',
'petroff10',
'seaborn-v0_8',
'seaborn-v0_8-bright',
'seaborn-v0_8-colorblind',
'seaborn-v0_8-dark',
'seaborn-v0_8-dark-palette',
'seaborn-v0_8-darkgrid',
'seaborn-v0_8-deep',
'seaborn-v0_8-muted',
'seaborn-v0_8-notebook',
'seaborn-v0_8-paper',
'seaborn-v0_8-pastel',
'seaborn-v0_8-poster',
'seaborn-v0_8-talk',
'seaborn-v0_8-ticks',
'tableau-colorblind10'
]
plt.style.available before selecting a style.
5. Applying a Built-in Style
The plt.style.use() function is used to apply a
predefined style to Matplotlib plots.
The style should normally be selected before creating the figure and axes so that the style settings are applied when the plot is created.
Syntax
plt.style.use('style_name')
Example: Applying ggplot Style
import matplotlib.pyplot as plt
plt.style.use('ggplot')
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [120, 150, 135, 180, 210]
plt.plot(months, sales, marker='o')
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
Output
Figure displayed with:
- ggplot-style background and grid.
- Monthly sales represented by a line.
- Circular markers on each data point.
- Month labels on the X-axis.
- Sales values on the Y-axis.
The chart uses the ggplot style settings while keeping the title, labels, data and marker settings provided in the code.
6. Popular Built-in Styles
Matplotlib provides different styles for different visualization requirements. Here are some commonly used examples.
6.1 ggplot Style
The ggplot style provides a visual appearance
inspired by the ggplot2 plotting system.
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.plot([1, 2, 3, 4], [10, 20, 15, 30])
plt.title('ggplot Style')
plt.show()
Output
Chart displayed using the ggplot-inspired
background, grid and plotting colours.
6.2 classic Style
The classic style provides a traditional Matplotlib
appearance. It is useful when you want a familiar, older plotting look.
import matplotlib.pyplot as plt
plt.style.use('classic')
plt.plot([1, 2, 3, 4], [10, 20, 15, 30])
plt.title('Classic Style')
plt.show()
Output
Chart displayed using the traditional
Matplotlib plotting appearance.
6.3 dark_background Style
The dark_background style uses a dark plotting
background. It can be useful for presentations and dark-themed
visualizations.
import matplotlib.pyplot as plt
plt.style.use('dark_background')
plt.plot([1, 2, 3, 4], [10, 20, 15, 30])
plt.title('Dark Background Style')
plt.show()
Output
Chart displayed with a dark background
and contrasting plot elements and labels.
6.4 grayscale Style
The grayscale style presents plots using shades
of grey instead of regular colours.
import matplotlib.pyplot as plt
plt.style.use('grayscale')
plt.plot([1, 2, 3, 4], [10, 20, 15, 30])
plt.title('Grayscale Style')
plt.show()
Output
Chart displayed using grayscale visual settings
instead of the regular colour palette.
6.5 fivethirtyeight Style
The fivethirtyeight style provides a presentation
inspired by the visual style used by FiveThirtyEight.
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
plt.plot([1, 2, 3, 4], [10, 20, 15, 30])
plt.title('FiveThirtyEight Style')
plt.show()
Output
Chart displayed with the FiveThirtyEight-inspired
background, grid and typography settings.
6.6 bmh Style
The bmh style provides a clean chart appearance
with its own background and grid settings.
import matplotlib.pyplot as plt
plt.style.use('bmh')
plt.plot([1, 2, 3, 4], [10, 20, 15, 30])
plt.title('BMH Style')
plt.show()
Output
Chart displayed using the bmh style settings.
7. Understanding Different Style Appearances
The same plotting code can produce different visual appearances when different styles are selected.
Conceptual illustration of how different styles can change a chart's appearance. These panels are not exact Matplotlib screenshots.
Remember that a style changes visual configuration. It does not change the actual data values or the mathematical calculation used to create the chart.
8. Applying Styles to Multiple Plots
When a style is applied using plt.style.use(), it
affects plots created afterwards. This is useful when we want
multiple charts to follow the same visual theme.
Example
import matplotlib.pyplot as plt
plt.style.use('ggplot')
x = [1, 2, 3, 4, 5]
sales = [100, 150, 130, 180, 220]
profit = [20, 35, 30, 45, 60]
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.plot(x, sales, marker='o')
plt.title('Sales')
plt.subplot(1, 2, 2)
plt.plot(x, profit, marker='s')
plt.title('Profit')
plt.tight_layout()
plt.show()
Output
One figure containing two charts:
1. Sales chart displaying five data points.
2. Profit chart displaying five data points.
Both charts use the ggplot style settings.
9. Using Temporary Styles with style.context()
Sometimes we need a particular style for only one chart or a small section of our code. We may not want that style to affect every plot created later.
Matplotlib provides plt.style.context() for this purpose.
It temporarily applies a style inside a context manager.
Example
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 30, 25]
with plt.style.context('dark_background'):
plt.plot(x, y, marker='o')
plt.title('Temporary Dark Style')
plt.show()
# This plot uses the style active outside the context
plt.plot(x, y, marker='s')
plt.title('Style Outside Context')
plt.show()
Output
First chart:
- Uses dark_background style.
Second chart:
- Uses the style that was active before entering
the temporary style context.
plt.style.context() when you want to apply a
style temporarily without permanently changing the style settings
for the rest of your plotting code.
10. Resetting the Style to Default
If you have applied a different style and want to return to the default Matplotlib appearance, use:
plt.style.use('default')
Complete Example
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 15, 30]
# Apply dark background
plt.style.use('dark_background')
plt.plot(x, y)
plt.title('Dark Style')
plt.show()
# Reset to default
plt.style.use('default')
plt.plot(x, y)
plt.title('Default Style')
plt.show()
Output
First chart:
Displayed using the dark_background style.
Second chart:
Displayed using the default Matplotlib style.
11. Comparison of Common Styles
| Style | General Appearance | Common Use |
|---|---|---|
| default | Standard Matplotlib appearance | General plotting |
| ggplot | ggplot2-inspired appearance | Statistical charts |
| classic | Traditional Matplotlib appearance | Legacy-style charts |
| dark_background | Dark plotting background | Dark-themed presentations |
| grayscale | Grey shades | Black-and-white reports |
| fivethirtyeight | FiveThirtyEight-inspired design | Data storytelling |
| bmh | Distinct background and grid | General data visualization |
| seaborn-v0_8 | Seaborn-inspired appearance | Statistical visualization |
plt.style.available
before using a particular style.
12. Practical Example: Comparing Sales with Different Styles
In this example, we will use the same sales data and create charts using two different built-in styles.
Python Code
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [120, 150, 135, 180, 210, 240]
# First chart using ggplot
with plt.style.context('ggplot'):
plt.figure(figsize=(8, 4))
plt.plot(months, sales, marker='o', linewidth=2)
plt.title('Monthly Sales - ggplot')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
# Second chart using dark_background
with plt.style.context('dark_background'):
plt.figure(figsize=(8, 4))
plt.plot(months, sales, marker='o', linewidth=2)
plt.title('Monthly Sales - Dark Background')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
Output
Chart 1:
Monthly Sales - ggplot
The chart uses the ggplot visual theme.
Chart 2:
Monthly Sales - Dark Background
The chart uses the dark background visual theme.
Both charts contain the same sales data.
This example demonstrates that we can change the appearance of charts without changing the original sales data.
13. Best Practices for Using Built-in Styles
- Choose a style according to the purpose of your visualization.
- Check available styles before using a style name.
- Apply the style before creating figures and axes.
- Use consistent styles across charts in the same report.
- Choose readable text and line colours.
- Use temporary style contexts when only one chart needs a different theme.
- Use the default style when no additional theme is required.
- Do not select a style only because it looks attractive; consider readability too.
14. Practice Questions
Basic Questions
- What are built-in styles in Matplotlib?
- Why do we use built-in styles?
- Which function displays available styles?
- How do you apply the ggplot style?
- Which style provides a dark plotting background?
Practical Questions
- Create a line chart using the ggplot style.
- Create a chart using the dark_background style.
- Display a chart using the grayscale style.
- Use style.context() to apply a temporary style.
- Apply a different style and then reset it to default.
Summary
Built-in styles in Matplotlib provide predefined visual settings that help developers improve the appearance of charts. They reduce manual formatting work and make it easier to maintain consistent chart designs.
The plt.style.available property displays the styles
available in the current Matplotlib installation. The
plt.style.use() function applies a selected style to
subsequently created plots.
Popular styles include ggplot, classic, dark_background, grayscale, fivethirtyeight and bmh. Each style provides a different visual appearance.
The plt.style.context() function is useful when a
style should be applied temporarily. The
plt.style.use('default') command can be used to
return to the default Matplotlib style.