python - 标题和绘图 matplotlib 之间的巨大空间

标签 python matplotlib

我的 matplotlib 有一个问题,它生成的绘图与标题相去甚远。我的代码是:

df = pandas.read_csv(csvpath, delimiter=',', 
                     index_col=0, 
                     parse_dates=[0], dayfirst=True, 
                     names=['Date','test1'])

df.plot(subplots=True, marker='.',markersize=8, title ="test", fontsize = 10, color=['b','g','g'], figsize=(8, 22))

imagepath = 'capture.png'

plt.savefig(imagepath)

生成的图形示例是:

enter image description here

知道为什么会这样吗?非常感谢。

最佳答案

您可以通过从 df.plotaxis 对象之一获取 figure 对象来调整标题位置。使用 fig.tight_layout 会删除多余的空格,但一个已知的问题是它还会在顶部绘图中设置图形标题。但是,您可以使用下面的最后一行来解决这个问题。

ax = df.plot(subplots=True, marker='.', markersize=8, 
             title ="test", fontsize = 10, color=['b','g','g'], 
             figsize=(8, 22))[0] 
fig = ax.get_figure()
fig.tight_layout()
fig.subplots_adjust(top=0.95)

关于python - 标题和绘图 matplotlib 之间的巨大空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39331143/

相关文章:

python - 无法在Python中保存tiff图像

python - 开罗,ImageSurface : Cannot get correct alpha blending

python - python自动提取近九个月数据

python - 如何修改ndarray矩阵中的特定字段

python - 如何广播向量和张量之和?

python - 3D 图和 3D 直方图子图

python - 在 matplotlib 蜡烛图上注释某些内容

python - 求解和绘制具有相关参数的 ODE

python - 使用 matplotlib 绘图 : hours shown as float

python - "version file"是什么样的?