前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何在不同的操作系统里保存同样大小的matplotlib图片?

如何在不同的操作系统里保存同样大小的matplotlib图片?

作者头像
气象学家
修改2021-05-20 15:15:15
7380
修改2021-05-20 15:15:15
举报
文章被收录于专栏:气象学家气象学家

One of our tests is failing because the output image is sometimes a slightly different size. On Linux it is 579x517 pixels and on Windows it is 582x520 pixels. I have checked the versions of matplotlib and pandas and they are the same. There are slight differences in matplotlib.rcParams; I've tried changing the parameters that look relevant, but it didn't help.

Is it because the display size is slightly different on the two machines? If I set dpi=99.99 on the savefig on the Windows machine, the output size is 579x517 pixels. I don't want to change this in the application though as it is a bit of a hack just to fix a failing test.

This is the plot function we're using, percentages is a pandas dataframe.

代码语言:javascript
复制
def plot_and_save(percentages, output_directory, filename, title, legend, x_label, y_label,
                  font_size=16):
    """Plot data and save figure to file."""
    matplotlib.rcParams['font.size'] = font_size
    ax1 = percentages.plot.bar(color=['#484D7A', '#F6A124'])
    ax1.grid(which='major', axis='y', color='#4A4A49', alpha=0.1)
    ax1.set_axisbelow(True)  # Puts gridlines below the bars
    ax1.set(xlabel=x_label, ylabel=y_label, title=title)
    ax1.legend(legend, framealpha=1)

    output_path = os.path.join(output_directory, filename)

    plt.savefig(output_path, bbox_inches='tight')
    plt.close()

来源:https://stackoverflow.com/questions/67515453/how-to-save-matplotlib-figures-the-same-size-on-different-operating-systems

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-05-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 气象学家 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档