Home > Article > Backend Development > Tips and methods to implement Chinese display in matplotlib
Tips and methods of matplotlib Chinese display
Introduction:
matplotlib is a powerful data visualization library that provides rich functions and flexible interfaces , allowing users to easily create various types of charts. However, for Chinese users, matplotlib has some problems in displaying Chinese. This article will introduce some commonly used techniques and methods to solve matplotlib Chinese display problems, and provide specific code examples.
1. Problem description:
By default, matplotlib does not support Chinese display, but uses English fonts for drawing. This will cause Chinese characters to appear as squares or garbled characters. Therefore, we need to solve this problem through some methods.
2. Font setting:
In order to solve the problem of Chinese display, we first need to set the font of matplotlib so that Chinese characters can be displayed correctly. In matplotlib, we can set the font in the following ways:
matplotlib.rcParams method:
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体为黑体 plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题 # 示例代码 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.plot(x, y) plt.xlabel('横轴') plt.ylabel('纵轴') plt.title('示例图表') plt.show()
Set the font directly:
import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties font = FontProperties(fname=r'path/to/font.ttf') # 设置自定义字体路径 # 示例代码 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.plot(x, y) plt.xlabel('横轴', fontproperties=font) plt.ylabel('纵轴', fontproperties=font) plt.title('示例图表', fontproperties=font) plt.show()
3. Chinese display of chart title and axis label:
In the above sample code, you can see that we set xlabel
, The
fontproperties parameters in the ylabel
and title
functions set the font to Chinese. This ensures that the chart's title and axis labels display Chinese characters correctly.
4. Chinese display of the legend:
The legend is a label used to explain the elements in the chart, usually located in the corner of the chart. We can set the Chinese display of the legend through the following methods:
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False # 示例代码 x = [1, 2, 3, 4, 5] y1 = [2, 4, 6, 8, 10] y2 = [1, 3, 5, 7, 9] plt.plot(x, y1, label='曲线1') plt.plot(x, y2, label='曲线2') plt.legend(['曲线1', '曲线2'], loc='upper right') # 设置图例文字和位置 plt.xlabel('横轴') plt.ylabel('纵轴') plt.title('示例图表') plt.show()
By setting the parameters in the legend
function, we can control the Chinese display and position of the legend. In the above example code, we set the label of the curve through the label
parameter, and then use the loc
parameter in the legend
function to set the position of the legend.
5. Chinese display of coordinate axis scale:
Sometimes, we also need to set the Chinese display of coordinate axis scale. In matplotlib, we can achieve this through the following methods:
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False # 示例代码 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.plot(x, y) plt.xticks(x, ['一', '二', '三', '四', '五']) # 设置x轴刻度标签 plt.yticks([0, 2, 4, 6, 8, 10], ['零', '二', '四', '六', '八', '十']) # 设置y轴刻度标签 plt.xlabel('横轴') plt.ylabel('纵轴') plt.title('示例图表') plt.show()
In the above example code, we set the x-axis and y-axis through the xticks
and yticks
functions Tick labels and use Chinese characters instead of the default numeric labels.
6. Summary:
Through the above methods, we can easily solve the problem of matplotlib Chinese display. By setting the font, axis labels, legend and axis scale, we can ensure that Chinese characters in the chart are displayed correctly. This is very important for Chinese users.
Appendix: Commonly used Chinese fonts
If you don’t like Song Dynasty, you can also use other Chinese fonts to display Chinese characters. The following are some commonly used Chinese fonts:
Reference materials:
[1] Matplotlib.pyplot.xlabel. https://matplotlib.org/stable/api/_as_gen/ matplotlib.pyplot.xlabel.html
[2] Matplotlib.pyplot.ylabel. https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.ylabel.html
[3] Matplotlib.pyplot. title. https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.title.html
[4] Matplotlib.pyplot.legend. https://matplotlib.org/stable/api/_as_gen/matplotlib .pyplot.legend.html
[5] Matplotlib.pyplot.xticks. https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.xticks.html
[6] Matplotlib.pyplot.yticks . https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.yticks.html
The above is the detailed content of Tips and methods to implement Chinese display in matplotlib. For more information, please follow other related articles on the PHP Chinese website!