qwe*_*rtz 7 python plot matplotlib title save
嘿,我试图保存我的情节,但总是切断我的头衔.我认为这是因为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')
Run Code Online (Sandbox Code Playgroud)
Mar*_*ram 49
我不知道我的场景是否和你的一样,但我通过bbox_inches='tight'在savefig调用中添加参数解决了我的问题。
鉴于其标题,这对于偶然发现这个问题的人来说可能很有价值。本来对我...
您可以使用来控制子图的放置plt.subplots_adjust。在这种情况下,要调整的相关选项是top。
以及改变,你将需要y在suptitle小于1(因为图坐标的作品-任何> 1将关闭该图的顶部)。y如果设置subplots_adjust正确,您甚至可能会忘记完全设置。
请注意,如果您仍想tight_layout控制其余子图的放置,则需要在subplots_adjust后面加上一行tight_layout,否则您设置的任何内容都将被覆盖。
(或者,您也可以设置left,right而且bottom在subplots_adjust和删除的需要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')
Run Code Online (Sandbox Code Playgroud)
很难知道你得到了什么,但以下内容应该有助于解决这个问题:
将现有的替换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')
Run Code Online (Sandbox Code Playgroud)
给你: