Last Updated : 26 Apr, 2025
By using style
function in Matplotlib we can apply predefined themes or create custom styles which helps in making our plots interactive. We can reuse these templates to maintain consistency across multiple plots. In this article we will see how to use Matplotlib’s built-in styles and efficiently apply them to your plots.
Syntax: plt.style.use('style_name")
If we want to explore all the available styles we can print it like this:
Python
from matplotlib import style
import matplotlib.pyplot as plt
print(plt.style.available)
Output:
Example 1: Applying['Solarize_Light2', '_classic_test_patch', 'bmh', ----------------seaborn-whitegrid','tableau-colorblind10']
Solarize_Light2 S
tyle
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
data = np.random.randn(50)
plt.style.use('Solarize_Light2')
plt.plot(data)
plt.show()
Output:
Solarize_Light2 Example 2: Applyingdark_background S
tyle
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
data = np.random.randn(50)
plt.style.use('dark_background')
plt.plot(data)
plt.show()
Output:
dark_background Example 3: Applyingggplot S
tyle
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
data = np.random.randn(50)
plt.style.use('ggplot')
plt.plot(data, linestyle=":", linewidth=2)
plt.show()
Output:
ggplot Example 4: Temporarily applydark_background S
tyle
Note: If we only want to use a style for a particular plot but don't want to change the global styling for all the plots, for that it provides a context manager for limiting the area of styling for a particular plot.
with plt.style.context('dark_background')
: Use a context manager to temporarily apply the dark_background
style to the plot within the block which ensures that only this plot is affected not others.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
with plt.style.context('dark_background'):
plt.plot(np.sin(np.linspace(0, 2 * np.pi)), 'r-o')
plt.show()
Output:
dark_background (temporary)By using Matplotlib's styling features we can easily create visually appealing and consistent plots which helps in enhancing overall presentation of our data.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4