ax1.set_title("原数据",fontproperties=font) plt.plot(day_ground['PM10'][:"2020-04-30"], color="c") python中怎么设置这幅图的横纵坐标轴字体为宋体,边框宽度为2
时间: 2024-04-22 09:27:59 浏览: 16
要在 Python 中设置图形的横纵坐标轴字体为宋体并设置边框宽度为2,你可以使用 Matplotlib 库中的 `FontProperties` 类来指定字体,并使用 `Axes` 对象的方法来设置边框宽度。
以下是在 Python 绘图时设置图形的横纵坐标轴字体为宋体并设置边框宽度为2的示例代码:
```python
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 设置字体为宋体
font = FontProperties(fname="/path/to/font.ttf", size=12)
# 绘制图形
fig, ax1 = plt.subplots()
ax1.set_title("原数据", fontproperties=font)
ax1.plot(day_ground['PM10'][: "2020-04-30"], color="c")
# 设置横轴和纵轴的字体
ax1.set_xlabel("横轴", fontproperties=font)
ax1.set_ylabel("纵轴", fontproperties=font)
# 设置边框宽度
ax1.spines["top"].set_linewidth(2)
ax1.spines["right"].set_linewidth(2)
ax1.spines["bottom"].set_linewidth(2)
ax1.spines["left"].set_linewidth(2)
# 显示图形
plt.show()
```
在这个示例中,我们首先导入了 `matplotlib.pyplot` 模块,并从 `matplotlib.font_manager` 模块导入了 `FontProperties` 类。然后,我们创建了一个名为 `font` 的字体对象,指定为系统中的某个字体文件路径(例如宋体)。接下来,在绘制图形时,我们使用 `ax1.set_xlabel()` 和 `ax1.set_ylabel()` 方法设置横纵坐标的标签,并通过 `fontproperties` 参数将字体应用于标签。
为了设置边框宽度,我们使用 `ax1.spines` 字典来获取各个边框,并使用 `set_linewidth()` 方法设置边框的宽度。
请确保将示例代码中的 `/path/to/font.ttf` 替换为你实际的字体文件路径,以及调整字体名称和大小以满足你的需求。
通过这种方式,你可以在 Python 绘图中设置图形的横纵坐标轴字体为宋体,并设置边框宽度为2。