web-dev-qa-db-ja.com

Python:Savefigがタイトルを切り落とす

ねえ、私は自分のプロットを保存しようとしますが、それは常に私のタイトルを切り落とします。 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')
8
qwertz

plt.subplots_adjust を使用して、サブプロットの配置を制御できます。この場合、調整するための関連オプションはtopです。

これを変更するだけでなく、ysuptitleを1未満にする必要があります(これは図の座標で機能するため、1より大きいものはすべて図の上部から外れます)。 subplots_adjustを正しく設定すると、yを完全に設定することを忘れることさえあります。

それでもtight_layoutでサブプロットの残りの配置を制御したい場合は、subplots_adjustの後にtight_layout行を追加する必要があります。そうしないと、設定した内容が上書きされます。

(または、subplots_adjustleftright、およびbottomを設定し、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

6
tmdavison

私のシナリオがあなたのシナリオと同じかどうかはわかりませんが、パラメータbbox_inches='tight'savefig呼び出しに追加することで問題を解決しました。

それは、そのタイトルを与えられたこの質問につまずく人々にとって価値があるかもしれません。それは私のためだっただろう...

1
MariusSiuram

あなたが何を得ているかを知ることは難しいですが、以下はそれを解決するのに役立つはずです:

既存の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
Martin Evans