matlibplot画散点气温图显示中文
时间: 2023-07-30 16:09:51 浏览: 44
要在 Matplotlib 绘制的散点气温图中显示中文,需要先设置中文字体。以下是示例代码:
``` python
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
# 设置中文字体
font_path = "SimHei.ttf" # 中文字体文件路径
font_prop = fm.FontProperties(fname=font_path)
# 输入数据
x = [0, 1, 2, 3, 4, 5, 6]
y = [15, 17, 19, 20, 22, 24, 25]
# 绘制散点图
plt.scatter(x, y)
# 添加标题和坐标轴标签
plt.title("温度散点图", fontproperties=font_prop)
plt.xlabel("日期", fontproperties=font_prop)
plt.ylabel("温度 (℃)", fontproperties=font_prop)
# 显示图形
plt.show()
```
以上代码中,首先通过 `matplotlib.font_manager` 模块的 `FontProperties` 类设置中文字体,其中 `fname` 参数指定中文字体文件路径。然后,将 `fontproperties` 参数设置为 `font_prop`,即可在标题和坐标轴标签中显示中文。
如果没有中文字体文件,可以在网上搜索下载,或者直接使用系统自带的中文字体文件,例如 Windows 系统自带的 `simhei.ttf`。
注意:由于不同操作系统和不同 Python 环境可能存在字体加载问题,以上代码可能需要根据具体情况进行调整。