Python 将 Matplotlib 图形保存为全屏图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32428193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:33:30  来源:igfitidea点击:

Saving Matplotlib graphs to image as full screen

pythonpandasnumpymatplotlibdata-visualization

提问by

I'm building a small graphing utility using Pandas and MatPlotLib to parse data and output graphs from a machine at work.

我正在使用 Pandas 和 MatPlotLib 构建一个小型图形实用程序,以解析工作中机器的数据和输出图形。

When I output the graph using

当我使用输出图形时

plt.show()

I end up with an unclear image that has legends and labels crowding each other out like so.

我最终得到了一个不清晰的图像,其中的图例和标签像这样相互挤在一起。

Sample Bad ImageHowever, expanding the window to full-screen resolves my problem, repositioning everything in a way that allows the graph to be visible.

样本不良图像但是,将窗口扩展到全屏解决了我的问题,以允许图形可见的方式重新定位所有内容。

I then save the graph to a .png like so

然后我将图形保存到 .png 像这样

plt.savefig('sampleFileName.png')

But when it saves to the image, the full-screen, correct version of the plot isn't saved, but instead the faulty default version.

但是当它保存到图像时,全屏的、正确的绘图版本没有保存,而是有错误的默认版本。

How can I save the full-screen plt.show() of the plot to .png?

如何将绘图的全屏 plt.show() 保存为 .png?

I hope I'm not too confusing.

我希望我不要太困惑。

Thank you for your help!

感谢您的帮助!

采纳答案by gtlambert

The method you use to maximise the window size depends on which matplotlib backend you are using. Please see the following example for the 3 most common backends:

用于最大化窗口大小的方法取决于您使用的 matplotlib 后端。请参阅以下 3 个最常见后端的示例:

import matplotlib.pyplot as plt

plt.figure()
plt.plot([1,2], [1,2])

# Option 1
# QT backend
manager = plt.get_current_fig_manager()
manager.window.showMaximized()

# Option 2
# TkAgg backend
manager = plt.get_current_fig_manager()
manager.resize(*manager.window.maxsize())

# Option 3
# WX backend
manager = plt.get_current_fig_manager()
manager.frame.Maximize(True)

plt.show()
plt.savefig('sampleFileName.png')

You can determine which backend you are using with the command matplotlib.get_backend(). When you save the maximized version of the figure it will save a larger image as desired.

您可以通过命令确定正在使用的后端matplotlib.get_backend()。当您保存图形的最大化版本时,它将根据需要保存更大的图像。

回答by kmario23

As one more option, I think it is also worth looking into

作为另一种选择,我认为也值得研究

plt.savefig('filename.png', bbox_inches='tight')

This is especially useful if you're doing subplotsthat has axis labels which look cluttered.

如果您的subplots轴标签看起来很杂乱,这将特别有用。

回答by Westly White

For those that receive errors in the answers above, this has worked for me.

对于那些在上述答案中收到错误的人,这对我有用。

#Show full screen
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()

回答by stepj

For everyone, who failed to save the plot in fullscreen using the above solutions, here is what really worked for me:

对于使用上述解决方案未能以全屏方式保存情节的每个人,以下是对我真正有用的内容:

    figure = plt.gcf()  # get current figure
    figure.set_size_inches(32, 18) # set figure's size manually to your full screen (32x18)
    plt.savefig('filename.png', bbox_inches='tight') # bbox_inches removes extra white spaces

You may also want to play with the dpi (The resolution in dots per inch)

您可能还想使用 dpi(每英寸点数的分辨率)

    plt.savefig('filename.png', bbox_inches='tight', dpi=100)