Matplotlib - Styles



What is Style in Matplotlib?

In Matplotlib library styles are configurations that allow us to change the visual appearance of our plots easily. They act as predefined sets of aesthetic choices by altering aspects such as colors, line styles, fonts, gridlines and more. These styles help in quickly customizing the look and feel of our plots without manually adjusting individual elements each time.

We can experiment with different styles to find the one that best suits our data or visual preferences. Styles provide a quick and efficient way to enhance the visual presentation of our plots in Matplotlib library.

Built-in Styles

Matplotlib comes with a variety of built-in styles that offer different color schemes, line styles, font sizes and other visual properties.

Examples include ggplot, seaborn, classic, dark_background and more.

Changing Styles

Use plt.style.use('style_name') to apply a specific style to our plots.

Key Aspects of Matplotlib Styles

  • Predefined Styles − Matplotlib library comes with various built-in styles that offer different aesthetics for our plots.

  • Ease of Use − By applying a style we can instantly change the overall appearance of our plot to match different themes or visual preferences.

  • Consistency − Styles ensure consistency across multiple plots or figures within the same style setting.

Using Styles

There are several steps involved in using the available styles in matlplotlib library. Let’s see them one by one.

Setting a Style

For setting the required style we have to use plt.style.use('style_name') to set a specific style before creating our plots.

For example if we want to set the ggplot style we have to use the below code.

import matplotlib.pyplot as plt
plt.style.use('ggplot')  # Setting the 'ggplot' style

Available Styles

We can view the list of available styles using plt.style.available.

Example

import matplotlib.pyplot as plt
print(plt.style.available)  # Prints available styles

Output

['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']

Applying Custom Styles

We can create custom style files with specific configurations and then use plt.style.use('path_to_custom_style_file') to apply them.

Applying the seaborn-darkgrid style

In this example the style 'seaborn-darkgrid' is applying to the plot altering its appearance.

Example

import matplotlib.pyplot as plt
# Using a specific style
plt.style.use('seaborn-darkgrid')

# Creating a sample plot
plt.plot([1, 2, 3, 4], [10, 15, 25, 30])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Sample Plot')
plt.show()
Output
Dark Grid

Applying ggplot style

In this example we are using the ggplot style for our plot.

Example

import matplotlib.pyplot as plt
# Using a specific style
plt.style.use('seaborn-white')

# Creating a sample plot
plt.plot([1, 2, 3, 4], [10, 15, 25, 30])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Sample Plot')
plt.show()
Output
White Grid
Advertisements