Ajuster les bords d'une figure Matplotlib


Cas général

Image No Tight Layout

Image With Tight Layout

#!/usr/bin/env python

from numpy import *

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
h = [0,1,2,3,5,6,4,2,1,0]

fig = plt.figure()

plt.title('Title')
plt.xlim(0, 10)
plt.ylim(0, 8)
plt.xlabel('x label')
plt.ylabel('y label')

bar1 = plt.bar(x,h,width=1.0,bottom=0,color='Green',alpha=0.65,label='Legend')

plt.legend()
#plt.savefig('ImageNoTightLayout.png')
plt.savefig('ImageWithTightLayout.png',bbox_inches='tight')

Cas ou la légende est à l'extérieur

Image with Legend and Tight Layout

#!/usr/bin/env python

from numpy import *

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
h = [0,1,2,3,5,6,4,2,1,0]

fig = plt.figure()

plt.title('Title')
plt.xlim(0, 10)
plt.ylim(0, 8)
plt.xlabel('x label')
plt.ylabel('y label')

bar1 = plt.bar(x,h,width=1.0,bottom=0,color='Green',alpha=0.65,label='Legend')

lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2)
plt.savefig('ImageWithLegendAndTightLayout.png', bbox_extra_artists=(lgd,), bbox_inches='tight')

Recherches associées

Image

of