Matplotlib tight_layout() doesn't take into account figure suptitle

后端 未结 10 1596
既然无缘
既然无缘 2020-11-28 18:02

If I add a subtitle to my matplotlib figure it gets overlaid by the subplot\'s titles. Does anybody know how to easily take care of that? I tried the tight_layout()

10条回答
  •  悲哀的现实
    2020-11-28 18:34

    I have struggled with the matplotlib trimming methods, so I've now just made a function to do this via a bash call to ImageMagick's mogrify command, which works well and gets all extra white space off the figure's edge. This requires that you are using UNIX/Linux, are using the bash shell, and have ImageMagick installed.

    Just throw a call to this after your savefig() call.

    def autocrop_img(filename):
        '''Call ImageMagick mogrify from bash to autocrop image'''
        import subprocess
        import os
    
        cwd, img_name = os.path.split(filename)
    
        bashcmd = 'mogrify -trim %s' % img_name
        process = subprocess.Popen(bashcmd.split(), stdout=subprocess.PIPE, cwd=cwd)
    

提交回复
热议问题