Fundamentals 5 min read

Explore 26 Matplotlib Styles and How to Set Chinese Fonts

This guide demonstrates how to list and apply the 26 built‑in Matplotlib plotting styles, visualizes each style in a grid, and shows how to configure Chinese fonts to correctly display non‑ASCII characters in your figures.

Model Perspective
Model Perspective
Model Perspective
Explore 26 Matplotlib Styles and How to Set Chinese Fonts

1 Set Plot Style

Matplotlib provides many built‑in plotting styles. You can view all available styles with plt.style.available and apply a style using plt.style.use() . The library includes 26 styles such as 'Solarize_Light2', 'bmh', 'ggplot', 'seaborn', and others.

<code>import matplotlib.pyplot as plt
import numpy as np
# Plot y = sin(x) with default style
x = np.linspace(-2*np.pi, 2*np.pi, 1000)
y = np.sin(x)
plt.plot(x, y, label='y=sin(x)')
plt.title('Default Style')
plt.legend()
plt.show()</code>

The following script iterates over all styles, creates a subplot for each, and displays the resulting figures side by side:

<code>plt.figure(figsize=(18,20))
i = 1
for style in plt.style.available:
    plt.style.use(style)
    plt.subplot(7,4,i)
    plt.plot(x, y, label=style)
    plt.legend()
    i = i + 1
plt.show()</code>

Different styles affect colors, grid lines, titles, and overall aesthetics, allowing you to choose a visual theme that matches your preference.

2 Set Chinese

When a plot contains Chinese characters without proper font configuration, the characters appear as garbled symbols. The example below shows this issue with the default style.

<code>import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2*np.pi, 2*np.pi, 1000)
y = np.sin(x)
plt.plot(x, y, label='y=sin(x)')
plt.title('默认风格')
plt.legend()
plt.show()</code>

To correctly display Chinese text, set the font family to a Chinese font (e.g., SimHei) and disable the Unicode minus sign handling before plotting:

<code>plt.rcParams['font.family'] = ['SimHei']  # Set Chinese font
plt.rcParams['axes.unicode_minus'] = False
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2*np.pi, 2*np.pi, 1000)
y = np.sin(x)
plt.plot(x, y, label='y=sin(x)')
plt.title('默认风格')
plt.legend()
plt.show()</code>

If the specified Chinese font is not installed on the system, you need to check which Chinese fonts are available and import one that exists.

Data ScienceMatplotlibPython visualizationChinese fontsplotting styles
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.