将Matplotlib图形保存为全屏图像 [英] Saving Matplotlib graphs to image as full screen
问题描述
我正在使用Pandas和MatPlotLib构建一个小型绘图实用程序,以从工作中的计算机解析数据并输出图形.
I'm building a small graphing utility using Pandas and MatPlotLib to parse data and output graphs from a machine at work.
当我使用
plt.show()
最后我得到一张不清楚的图像,上面有图例和标签,彼此挤在一起.
I end up with an unclear image that has legends and labels crowding each other out like so.
但是,将窗口扩展到全屏可以解决我的问题,以一种使图形可见的方式重新放置所有内容.
However, expanding the window to full-screen resolves my problem, repositioning everything in a way that allows the graph to be visible.
然后我像这样将图形保存到.png
I then save the graph to a .png like so
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.
如何将绘图的全屏plt.show()保存为.png?
How can I save the full-screen plt.show() of the plot to .png?
我希望我不要太困惑.
谢谢您的帮助!
推荐答案
用于最大化窗口大小的方法取决于您所使用的matplotlib后端.请参阅以下示例,了解3种最常见的后端:
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:
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')
您可以使用命令matplotlib.get_backend()
确定正在使用哪个后端.保存该图的最大化版本时,它将根据需要保存更大的图像.
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图形保存为全屏图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!