Home >Backend Development >Python Tutorial >Quick tips and steps to solve matplotlib Chinese character display problems

Quick tips and steps to solve matplotlib Chinese character display problems

WBOY
WBOYOriginal
2024-01-04 11:26:051236browse

Quick tips and steps to solve matplotlib Chinese character display problems

Tips and steps to quickly solve matplotlib Chinese garbled characters

When we use matplotlib to draw graphics, we often encounter the problem of Chinese garbled characters. This is because by default, matplotlib uses English fonts, and for Chinese, this will cause the displayed text to not be displayed properly. However, there are some simple tricks and steps we can take to solve this problem and allow our graphics to display Chinese correctly.

1. Change the default font

matplotlib will use the default font to display text. First, we need to find the font file we are using. You can use the following code to view the fonts currently installed on the system:

import matplotlib.font_manager as fm
font_list = fm.findfont(fm.FontProperties())
print(font_list)

This code will print out the font path installed on the system. You can choose one of the font files as our Chinese font.

Next, we need to set the selected font as the default font in the matplotlibrc configuration file. Find the location of the matplotlibrc file (you can use the following code to get its location):

import matplotlib
print(matplotlib.matplotlib_fname())

In the found matplotlibrc file, find the following two parameters:

#font.serif : Times New Roman, ...
#font.sans-serif : Arial, ...

Change the values ​​​​of these two parameters Change the name of the font file we selected, for example:

font.serif : SimHei
font.sans-serif : SimHei

Save the modified matplotlibrc file and restart the python environment.

2. Use the specified font

If you need to use the specified font in one drawing, you can use the following code:

import matplotlib.pyplot as plt
font = {'family' : 'SimHei',
        'weight' : 'normal',
        'size'   : 14}

plt.rc('font', **font)

Among them, the 'family' parameter specifies the font The name of the font, the 'weight' parameter specifies the weight of the font, and the 'size' parameter specifies the size of the font. In this way, we can use the specified font in subsequent drawings.

Next, let’s look at a specific example.

import matplotlib.pyplot as plt
import numpy as np

font = {'family' : 'SimHei',
        'weight' : 'normal',
        'size'   : 14}

plt.rc('font', **font)

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

plt.plot(x, y)
plt.title('正弦曲线')
plt.xlabel('x轴')
plt.ylabel('y轴')

plt.show()

In this example, we used the SimHei font to set the labels for the title, x-axis and y-axis.

Through the above two methods, we can quickly solve the problem of Chinese garbled characters in matplotlib. Whether we change the default font or use the specified font in each drawing, we can make our graphics clearly display Chinese. I hope this article is helpful for you to use matplotlib to draw Chinese graphics.

The above is the detailed content of Quick tips and steps to solve matplotlib Chinese character display problems. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn