Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:Savefig删除标题_Python_Matplotlib_Plot_Save_Title - Fatal编程技术网

Python:Savefig删除标题

Python:Savefig删除标题,python,matplotlib,plot,save,title,Python,Matplotlib,Plot,Save,Title,嘿,我试图保存我的情节,但它总是切断我的标题。 我认为这是因为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",

嘿,我试图保存我的情节,但它总是切断我的标题。 我认为这是因为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')

很难知道你得到了什么,但以下几点有助于解决这个问题:

将现有的
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')
给你:


您可以使用控制子批次的放置。在这种情况下,要调整的相关选项是
top

除了改变这一点,您还需要将
suptitle
中的
y
设置为小于1(因为这在地物坐标中起作用-任何大于1的内容都将脱离地物顶部)。如果正确设置
subplot\u adjust
,甚至可以完全忘记设置
y

请注意,如果您仍然希望
紧密布局
控制子地块的其余位置,则需要在
紧密布局
之后调整
子地块
行,否则您在那里设置的任何内容都将被覆盖

(或者,您可以在
子批次调整
中设置
左侧
右侧
底部
,并消除对
紧凑布局的需要)

下面是一个示例脚本(从您的示例中选取相关部分):


我不知道我的场景是否与您的场景相同,但我通过在
savefig
调用中添加参数
bbox\u inches='tight'
解决了我的问题


这对于那些偶然发现这个问题的人来说是很有价值的。如果是我的话……

请跟我来。目前,由于缺少重要代码,无法重新创建问题,也无法从屏幕截图中查看问题所在。相关问题:现在我再次覆盖了子图(y=0.98)。。。但当y=1.05时,它不在我的SaveFignity中:)错误是在紧凑布局前调整子地块线。这正是我需要让savefig在有很多单词时停止删除标题的原因。
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')