Home>Article>Backend Development> Share tips and experiences on how to deal with garbled Chinese characters in matplotlib

Share tips and experiences on how to deal with garbled Chinese characters in matplotlib

王林
王林 Original
2024-01-13 14:14:13 1329browse

Share tips and experiences on how to deal with garbled Chinese characters in matplotlib

Sharing of tips and experiences in solving matplotlib Chinese garbled characters

[Introduction]
When using matplotlib to draw graphics, we will inevitably encounter the problem of Chinese garbled characters. This problem usually occurs in legends, axis labels, etc. In order to solve this problem, this article will share some practical tips and experiences to help readers easily solve the problem of Chinese garbled characters in matplotlib.

[Problem Description]
When using matplotlib to draw graphics, we use the English character set by default. When adding Chinese text, because the default character encoding of matplotlib is the ASCII character set, Chinese characters cannot be displayed correctly and are displayed as garbled characters. Solving this problem requires us to make corresponding adjustments for different operating systems and program environments.

[Solution]
1. Change the system default font
In order to enable matplotlib to display Chinese characters correctly, we can change the system default font. Taking the Windows operating system as an example, we can set it up through the following steps:

  1. Open the font configuration file of matplotlib, and you can use the following code to find the file path:

import matplotlib
matplotlib.matplotlib_fname()

  1. Find thematplotlibrcfile under this path, open and findfont.familyandfont. sans-serifThe settings of the two parameters, change them to:

font.family: Microsoft Yahei, SimHei, Arial
font.sans-serif: Microsoft Yahei, SimHei, Arial

  1. Save the changes and re-run the program, Chinese characters will be displayed correctly.

2. Manually set the font
In addition to changing the system default font, we can also manually set the font in the program. We can use the following code snippet to achieve this:

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']

This way , we set the font to "Microsoft YaHei", which is a commonly used Chinese font and can be replaced by adding other Chinese fonts in square brackets.

3. Use Unicode encoding
In some special cases, we may not be able to change the system default font or set the font manually. At this time, we can use Unicode encoding to display Chinese characters. For example:

plt.xlabel(u'horizontal axis name')

Add the prefix "u" before the string to indicate that the string uses Unicode encoding, so that matplotlib can correctly display Chinese characters .

4. Use fontproperties parameters
In some cases, we may need to use Chinese characters in legends or other places. At this time, we can use the fontproperties parameter to specify the corresponding Chinese font and set the font. For example:

import matplotlib.font_manager as fm

font = fm.FontProperties(fname='Microsoft YaHei.ttf')
plt.xlabel('Horizontal axis name', fontproperties= font)

In this way, we can display Chinese characters by specifying the font.

【Code Example】
The following code example demonstrates how to display Chinese characters correctly:

import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

Set font

font = fm.FontProperties(fname='Microsoft YaHei.ttf')
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']

Draw graphics

x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
plt.plot(x, y)

Chinese display of coordinate axis labels and legend settings

plt.xlabel('horizontal axis name', fontproperties=font)
plt.ylabel('vertical axis name', fontproperties=font)
plt.legend(['curve'], prop=font)

Display graphics

plt.show()

[Summary]
Solve matplotlib Chinese garbled characters question, we can choose the appropriate method based on specific needs. By changing the system default font, setting the font manually, using Unicode encoding, or using the fontproperties parameter, we can easily achieve the correct display of Chinese characters. I hope that the skills and experience in this article can help readers solve the problem of Chinese garbled characters in matplotlib and improve the drawing effect.

The above is the detailed content of Share tips and experiences on how to deal with garbled Chinese characters in matplotlib. 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