2016-03-14 26 views
4

嘿,我試圖拯救我的情節,但它總是切斷我的標題。 我認爲這是因爲y = 1.05(設置與標題的距離)。 我無法修復它。有沒有辦法保存整個圖形?Python:Savefig切斷標題

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') 
+0

請按照[最小的,完整的和可驗證的示例](http://stackoverflow.com/help/MCVE)。按照現狀,重要的代碼丟失或者從屏幕截圖中查看問題是不可能的。 –

回答

0

這是很難知道你做了什麼,但下面應該幫助解決這個問題:

替換現有的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') 

給你:

Matplotlib screenshot

+0

現在我重疊與我的subplots再次(y = 0.98)...但是當y = 1.05它不是在我的savefig – qwertz

3

您可以將con使用plt.subplots_adjust來安置子圖。在這種情況下,相關的調整選項是top

除了改變這種情況,您還需要使y中的suptitle小於1(因爲它在圖形座標中起作用 - 任何大於1的圖形都不在圖的頂部)。如果您正確設置了subplots_adjust,您甚至可以完全忘記設置y

請注意,如果您仍然希望tight_layout控制其餘的副場所擺放位置,您需要在tight_layout之後設置您的subplots_adjust線,否則您在此設置的任何內容都將被覆蓋。

(或者,您也可以設置leftrightbottomsubplots_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') 

enter image description here

+1

尼斯:)錯誤是subplots_adjust線之前tight_layout – qwertz