matplotlib轻松解决中文乱码问题

python里matplotlib模块在画图方面用着十分爽,但是在图中不能显示中文,这里介绍一种十分简单的解决方法。

1. 在代码开始加入两行代码,即可成功解决。

#解决中文显示问题
plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题

2.效果展示

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np


#解决中文显示问题
plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题

x = np.linspace(-8, 8, 1024)
y1 = 0.618 * np.abs(x) - 0.8 * np.sqrt(64 - x ** 2)
y2 = 0.618 * np.abs(x) + 0.8 * np.sqrt(64 - x ** 2)
plt.plot(x, y1, color='r')
plt.plot(x, y2, color='r')
plt.title("爱你一万年")
plt.show()

3.代码加入前

4.代码加入后

猜你喜欢

转载自blog.csdn.net/qq_41689620/article/details/85218329