Experts teach you a trick to easily solve the problem of matplotlib Chinese garbled characters. Specific code examples are needed
Introduction:
When using Python for data analysis and visualization In the process, we often use matplotlib, a commonly used drawing library. However, sometimes the Chinese characters in the charts we draw using matplotlib will be garbled, causing us unnecessary trouble. Today, we will share a simple method to solve the problem of Chinese garbled characters in matplotlib and provide specific code examples.
Text:
The way to solve the problem of Chinese garbled characters in matplotlib is to set the font. First, we need to determine which Chinese fonts are installed on the operating system. Under normal circumstances, Windows systems have installed some Chinese fonts by default. You can view the list of fonts available in the system through the following code:
import matplotlib.font_manager as fm font_list = fm.findSystemFonts() font_names = [fm.get_font(font).family_name for font in font_list] print(font_names)
Executing the above code will output the list of fonts available in the system. We can see something like In font names such as "SimSun", "SimHei", and "Microsoft Yahei".
Next, we need to set the font used in matplotlib. The font can be set through the following code example:
import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties # 设置字体 font = FontProperties(fname=r"c:windowsontsSimSun.ttc", size=14) # 绘图示例 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.plot(x, y) # 添加中文字符 plt.title("折线图示例", fontproperties=font) plt.xlabel("x轴", fontproperties=font) plt.ylabel("y轴", fontproperties=font) plt.show()
In the above code, we first set the font to be used through FontProperties
, where the fname
parameter specifies the font path of. It should be noted that the path in the above code is for Windows system settings. If you are using other operating systems, please modify the font path accordingly. We then use the fontproperties
parameter to specify the font to use in the title and axis labels.
Run the above code to draw a line chart with normal Chinese character display.
Also, if you want to use the same font settings throughout the run, you can set global settings in matplotlib's configuration file. First, you need to find the configuration file of matplotlib. You can check its path through the following code:
import matplotlib as mpl print(mpl.matplotlib_fname())
Then, edit the configuration file (usually matplotlibrc
) and find the following line:
#font.family : sans-serif
Modify it to:
font.family : SimSun, Arial, sans-serif
Save the configuration file, restart the Python environment, and all charts drawn using matplotlib will use the specified font.
Conclusion:
Through the above method, we can easily solve the problem of Chinese garbled characters in matplotlib. When drawing charts, we only need to set appropriate fonts to ensure the normal display of Chinese characters. This facilitates our data analysis and visualization work. I hope the above content can help everyone, thank you for reading!
The above is the detailed content of Easily solve the problem of Chinese garbled characters in matplotlib, experts teach you a trick. For more information, please follow other related articles on the PHP Chinese website!