Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

python - 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...