python - matplotlib savefig() 大小控制

标签 python matplotlib pandas

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

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 按钮手动保存图形。

如何修复我的函数以便 savefig() 正确保存图表?我试图在 pl.savefig() 之前放这样一行来控制我的身材:

       pl.figure(figsize=....) 

但我最终生成了一些空图。编写一个 matplotlib 函数让我完全控制保存图形的正确方法是什么?

更新为问题图示例: enter image description here

最佳答案

来自 documentation ,您可以添加一个 dpi 参数来设置分辨率。

savefig('foo.png', dpi=199)

关于python - matplotlib savefig() 大小控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13073045/

相关文章:

python - 在 Pandas 中捕捉时间

python - 使用 matplotlib 绘制点数图

graphics - 如何在matplotlib的散点图中设置点的边框颜色?

python - 在 Python 中使用 Pandas/matplotlib 更改 X 轴标签

python - 拆分 pandas 数据框中的字符串数据

python - 如何使用 python 连接来自不同列表的两个项目?

python - 有没有办法通过只调用类的名称来调用函数?

python - pytorch .cuda() 无法将张量获取到 cuda

python - (Python,数据帧): Record the average of all numbers in a column that are smaller than the n'th percentile

python - 检验大量股票 yield 的自相关性