Python:Savefig 切断标题

Posted

技术标签:

【中文标题】Python:Savefig 切断标题【英文标题】:Python: Savefig cuts off title 【发布时间】:2016-06-29 18:48:37 【问题描述】:

嘿,我试图保存我的情节,但它总是切断我的标题。 我认为这是因为 y=1.05 (设置与标题的距离)。 我无法修复它。有没有办法保存整个图表?

time=round(t[time_period],0)
most_sensitive=sorted(most_sensitive)
plt.figure(figsize=(10, 5))
plt.suptitle("Scatterplot "+str(name)+" , "+r'$\Delta$'+"Output , Zeit= "+str(time)+" s",fontsize=20,y=1.05)
figure_colour=["bo","ro","go","yo"]
for i in [1,2,3,4]:
    ax=plt.subplot(2,2,i)
    plt.plot(parm_value[:,most_sensitive[i-1]], Outputdiff[:,most_sensitive[i-1]],figure_colour[i-1])
    ax.set_xlabel(name+"["+str(most_sensitive[i-1])+"] in "+str(unit))
    ax.set_ylabel(r'$\Delta$'+"Output")
    lb, ub = ax.get_xlim( )
    ax.set_xticks( np.linspace(lb, ub, 4 ) )
    lb, ub = ax.get_ylim( )
    ax.set_yticks( np.linspace(lb, ub, 8 ) )
    ax.grid(True)


plt.tight_layout()
newpath = r'C:/Users/Tim_s/Desktop/Daten/'+str(name)+'/'+str(time)+'/'+'scatterplot'+'/'
if not os.path.exists(newpath):
    os.makedirs(newpath)
savefig(newpath+str(name)+'.png')

【问题讨论】:

请关注Minimal, Complete, and Verifiable example。就目前而言,由于缺少重要代码,无法重新创建您的问题,或者从屏幕截图中查看问题所在。 相关问题:***.com/questions/6774086/… 如果这些答案中的任何一个有帮助,请考虑接受其中一个。 【参考方案1】:

我不知道我的情况是否与您的情况相同,但我通过在 savefig 调用中添加参数 bbox_inches='tight' 解决了我的问题。

鉴于其标题,这对于偶然发现此问题的人可能很有价值。本来是给我的……

【讨论】:

谢谢一百万,这正是我需要让 savefig 在标题很多时停止切断我的标题。 这个! ??请采纳这个答案。如此简单。【参考方案2】:

您可以使用plt.subplots_adjust 控制子图的位置。在这种情况下,要调整的相关选项是top

除了改变它之外,您还需要使suptitle 中的y 小于 1(因为这适用于图形坐标 - 任何大于 1 的值都将不在图形顶部)。如果您正确设置了subplots_adjust,您甚至可以完全忘记设置y

请注意,如果您仍希望 tight_layout 控制子图的其余位置,则需要在 tight_layout 之后添加 subplots_adjust 行,否则您在此处设置的任何内容都将被覆盖。

(或者,您可以在subplots_adjust 中设置leftrightbottom,并删除对tight_layout 的需要。

这是一个示例脚本(从您的示例中获取相关部分):

import matplotlib.pyplot as plt

plt.figure(figsize=(10,5))
name='mdot'
time='918.0'

plt.suptitle("Scatterplot "+str(name)+" , "+r'$\Delta$'+"Output , Zeit= "+str(time)+" s",fontsize=20)

for i in [1,2,3,4]:
    ax=plt.subplot(2,2,i)

plt.tight_layout()
plt.subplots_adjust(top=0.88)

plt.savefig('example.png')

【讨论】:

不错 :) 错误是 subplots_adjust 行之前的 tight_layout【参考方案3】:

很难知道你得到了什么,但以下应该有助于解决它:

将您现有的suptitle 替换为:

import matplotlib.pyplot as plt
import numpy as np

name = "test"
unit = 'cms'
most_sensitive = [1, 2, 3, 4, 5]
time = 5 #round(t[time_period],0)
most_sensitive=sorted(most_sensitive)
fig = plt.figure(figsize=(10, 5))
figure_colour=["bo","ro","go","yo"]
plt.suptitle("Scatterplot "+str(name)+" , "+r'$\Delta$'+"Output , Zeit= "+str(time)+" s",fontsize=20, y=0.95)

for i in [1, 2, 3, 4]:
    ax = plt.subplot(2, 2, i)
    #plt.plot(parm_value[:,most_sensitive[i-1]], Outputdiff[:,most_sensitive[i-1]],figure_colour[i-1])
    ax.set_xlabel(name+"["+str(most_sensitive[i-1])+"] in "+str(unit))
    ax.set_ylabel(r'$\Delta$'+"Output")
    lb, ub = ax.get_xlim( )
    ax.set_xticks( np.linspace(lb, ub, 4 ) )
    lb, ub = ax.get_ylim( )
    ax.set_yticks( np.linspace(lb, ub, 8 ) )
    ax.grid(True)

plt.tight_layout()
plt.subplots_adjust(top=0.85)     # Add space at top

newpath = r'C:/Users/Tim_s/Desktop/Daten/'+str(name)+'/'+str(time)+'/'+'scatterplot'+'/'
if not os.path.exists(newpath):
    os.makedirs(newpath)

plt.savefig(newpath+str(name)+'.png')

给你:

【讨论】:

现在我再次覆盖了我的子图(y=0.98)...但是当 y=1.05 时它不在我的 savefig 中

以上是关于Python:Savefig 切断标题的主要内容,如果未能解决你的问题,请参考以下文章

调用 savefig 后 Python matplotlib 更新图

使用 matplotlib 的 savefig 保存从 python pandas 生成的图(AxesSubPlot)

为啥 Matplotlib savefig 图像重叠?

python使用matplotlib的savefig保存时图片保存不清晰以及不完整的问题

Python Matplotlib 中如何用 plt.savefig 存储图片

python 画图后柱状图保存到本地方法 plt.savefig("filename.png") python 使用 plt 画图后保存到本地 python保存柱状图/