Python: Savefig shortens the title

Hey, I'm trying to save my plot, but it always shortens my title. I think this is because of y = 1.05 (to set the distance to the name). I can’t fix it. Is there a way to save the whole schedule?

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')
+4
source share
2 answers

You can control the placement of subheadings with plt.subplots_adjust. In this case, the corresponding parameter for setting is - top.

, y suptitle 1 ( - > 1 ). y , subplots_adjust.

, , tight_layout , subplots_adjust tight_layout, , , .

( left, right bottom subplots_adjust tight_layout).

script ( ):

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

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

:

Screenshot Matplotlib

0

All Articles