The method to solve the Chinese garbled problem of matplotlib requires specific code examples
Matplotlib is a commonly used Python library for data visualization, which can generate various charts and graphs . However, for Chinese users, a problem they often encounter is that the Chinese characters in the generated charts are garbled. This problem can be solved with some simple methods. This article will introduce some common solutions and attach relevant code examples to help readers solve this annoying problem.
Method 1: Set the font
One of the most common solutions is to set a suitable font to display Chinese characters. Matplotlib uses English fonts by default, so you need to manually specify a Chinese font. It can be set through the following code example:
import matplotlib.pyplot as plt import matplotlib.font_manager as fm # 查找并加载中文字体 font_path = 'path/to/your/font.ttf' font_name = fm.FontProperties(fname=font_path).get_name() plt.rcParams['font.family'] = font_name # 以下是绘制图表的代码 fig, ax = plt.subplots() ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) ax.set_xlabel('横坐标') ax.set_ylabel('纵坐标') plt.show()
In the above code, you first need to find and load the specified Chinese font file through the font_manager
module. The font_path
variable refers to The path of the Chinese font file. Then get the font name through the FontProperties
class and set it as the global default font, which is font.family
. The following code is an example of drawing a simple chart, which can be modified according to your own needs.
Method 2: Use font caching
In addition to setting the font directly, Matplotlib also provides a font caching mechanism, which can place the font file in the default cache directory so that it can be automatically loaded. And render Chinese characters. This can be set via the following code example:
import matplotlib.pyplot as plt import matplotlib.font_manager as fm # 将字体文件放入字体缓存目录 font_path = 'path/to/your/font.ttf' fm.findfont(font_path) plt.rcParams['font.family'] = fm.FontProperties(fname=font_path).get_name() # 以下是绘制图表的代码 fig, ax = plt.subplots() ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) ax.set_xlabel('横坐标') ax.set_ylabel('纵坐标') plt.show()
In the above code, the font file is placed into the font cache directory via the findfont
function and its path is passed to FontProperties
Class gets the font name and sets it as the global default font. The following code is an example of drawing a simple chart, which can be modified according to your own needs.
Method 3: Use the system default font
If you do not have a specific Chinese font file, you can also use the system default font to display Chinese characters. Matplotlib provides a function for obtaining the system default font path, which can be set directly using this path. The following is a relevant code example:
import matplotlib.pyplot as plt import matplotlib.font_manager as fm # 获取系统默认字体路径 font_path = fm.findfont(fm.FontProperties()) plt.rcParams['font.family'] = fm.FontProperties(fname=font_path).get_name() # 以下是绘制图表的代码 fig, ax = plt.subplots() ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) ax.set_xlabel('横坐标') ax.set_ylabel('纵坐标') plt.show()
In the above code, the system default font path is obtained through the findfont
function and passed to the FontProperties
class to obtain the font name, and set it as global default font. The following code is an example of drawing a simple chart, which can be modified according to your own needs.
The above are three common methods to solve the problem of Chinese garbled characters in Matplotlib. You can choose the method that suits you according to the specific situation and set it up. I hope the content of this article will be helpful to readers.
The above is the detailed content of matplotlib method to solve the problem of Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!