matplotlib savefig()大小控制 [英] matplotlib savefig() size control

查看:816
本文介绍了matplotlib savefig()大小控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个函数,该函数采用了从Pandas生成的数据帧并生成热图:

I wrote a function that took a dataframe generated from Pandas and produce a heatmap:

def drawHeatMap(df, city, province, collector, classtype, color, titleposy):
    try:
        thePlot = pl.matshow(df.values, cmap='PuBuGn')
        pl.colorbar(thePlot, orientation='vertical')
        aTitle = (classtype + ' Composition Changes Over Time in ' + city + 
                ', ' + province + '\n' + collector + ' collector. ' + 'rs100')
        pl.title(aTitle, x=0.5, y=titleposy, style='oblique', weight='bold')
        pl.xlabel('Collection Time')
        pl.xticks(range(len(df.columns)), df.columns, rotation=90)
        pl.yticks(range(len(df.index)), df.index)
        fileName = (classtype + '-' + city + '-' 
                + province + '-' + collector + '.png')
        pl.savefig(fileName)
    except ZeroDivisionError:
        errorMessage = ('No Data Avaiable for ' + city + ', ' + province + 
                ' with ' + collector + ' collector.')
        print errorMessage

我遇到的问题是,savefig()将保存带有修剪的轴和图形的图形.我必须使用show(),最大化图形并使用GUI按钮手动保存图形.

The problem I am having is, savefig() would save figures with the axis and graphics trimmed. I have to use show(), maximize the graph and manually save the figure with the GUI button myself.

如何修复我的功能,以便savefig()可以正确保存图形?我试图在pl.savefig()之前放置这样的一行来控制自己的身材:

How can I fix my function so savefig() would save the graphs properly? I tried to put a line like this before pl.savefig() to control my figure:

       pl.figure(figsize=....) 

但是我最终产生了一些空图.编写matplotlib函数以使我能够完全控制保存图形的正确方法是什么?

but I end up producing some empty graphs. What is the proper way to write a matplotlib function that give me full control on saving the figure?

更新为问题图示例:

推荐答案

我在savefig()之前添加了plt.tight_layout(),它解决了我遇到的修整问题.也许对您也有帮助.

I added plt.tight_layout() before savefig(), and it solved the trimming issue I had. Maybe it will help yours as well.

我还在开头rcParams['figure.figsize'] = 40, 12设置了图形大小(您可以设置自己的宽度和高度)

I also set the figure size at the begining rcParams['figure.figsize'] = 40, 12(you can set your own width and height)

这篇关于matplotlib savefig()大小控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆