首页 > 解决方案 > 使用熊猫图时如何设置图形标题,而不是斧头标题?

问题描述

使用 pandas 后df.plot,我想设置整个图的标题,而不是图中的标题,如何实现?

以下两个代码更好地解释了我的问题:

我想在这里添加标题:

lst = [[1, 2], [2, 3], [3, 4]]
plt.figure("I want the title here!")
plt.plot([0, 1, 2], lst)
plt.show()

使用时如何实现这一点df.plot

lst = [[1, 2], [2, 3], [3, 4]]
df = pd.DataFrame(lst)
ax = df.plot()
ax.set_title("not the title here")
plt.show()

标签: pythonpandasmatplotlib

解决方案


您可以从 中返回一个axis对象df.plot(**kwargs),并用于get_figure()获取图形对象,然后设置图形标题。

ax = df.plot()
ax.get_figure().canvas.set_window_title('Is this the title you want?')

推荐阅读