Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the margins for a matplotlib figure?

I am generating an on-screen figure that has two subplots: one is an image and the other is a graph. The margins are extremely large around the figures.

How do I adjust the margins around the figures?


Most questions that I searched for involved saving images (bbox seemed perfect), and using axes instead of subplots for absolute positioning.

Here is the code I used to generate the figure:

def __init__(self, parent):
    wx.Panel.__init__(self, parent)
    ...

    self.figure, (self.picture, self.intensity) = \
        plt.subplots(nrows=2, figsize=(12, 5))
    self.figure.set_dpi(80)
    #self.figure.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
    #self.picture.imshow(np.random.uniform()) #size=(5, 50)))
    self.intensity.plot(np.random.random()) #size=641))

    self.intensity.autoscale(axis='x', tight=True)
like image 744
Jesvin Jose Avatar asked Jun 05 '12 14:06

Jesvin Jose


People also ask

How do I remove the margins in matplotlib?

The python plotting library matplotlib will by default add margins to any plot that it generates. They can be reduced to a certain degree through some options of savefig() , namely bbox_inches='tight' and pad_inches=0 .


1 Answers

Have a look at plt.tight_layout() or plt.subplots_adjust() or fig.savefig(bbox_inches='tight').

With subplots_adjust you can adjust most parameters, while tight_layout() and bbox_inches='tight' are more or less semi automatic.

like image 170
bmu Avatar answered Sep 20 '22 01:09

bmu