请用Python matplot写一个显示中文坐标,中文标题,中文图例的图,并保存为pdf格式
时间: 2023-08-10 15:05:14 浏览: 58
这里提供一个示例代码,需要安装中文字体包,以SimHei为例:
```python
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 设置中文字体
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=12)
# 生成数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# 绘图
plt.plot(x, y, label="曲线", linewidth=2)
# 添加标题和标签
plt.title("中文标题", fontproperties=font)
plt.xlabel("横坐标", fontproperties=font)
plt.ylabel("纵坐标", fontproperties=font)
# 添加图例
plt.legend(loc="upper left", prop=font)
# 显示图形
plt.show()
# 保存为pdf格式
plt.savefig("figure.pdf")
```
其中,`FontProperties`用于设置中文字体,`fname`参数为字体文件路径,`size`参数为字体大小。在绘制标题、标签和图例时,使用`fontproperties`参数指定字体。
最后一行`plt.savefig("figure.pdf")`用于将图形保存为pdf格式。