如何使pylab.savefig()保存图像的“最大化”窗口,而不是默认大小

我正在matplotlib中使用pylab来创build一个绘图,并将绘图保存到一个图像文件。 但是,当我使用pylab.savefig( image_name )保存图像时,我发现保存的SIZE图像与使用pylab.show()时显示的图像相同。

碰巧,我有很多的数据在图中,当我使用pylab.show() ,我必须最大化窗口之前,我可以正确地看到所有的情节,并且xlabel tickers不叠加在每个其他。

无论如何,我可以编程方式“最大化”的窗口之前,将图像保存到文件? – 目前,我只获取“默认”窗口大小的图像,这导致x轴标签相互叠加。

您在初始化时设置大小:

 fig2 = matplotlib.pyplot.figure(figsize=(8.0, 5.0)) # in inches! 

编辑

如果问题出现在X轴上 – 可以将它们设置为“手动”:

 fig2.add_subplot(111).set_xticks(arange(1,3,0.5)) # You can actually compute the interval You need - and substitute here 

等你的情节的其他方面。 你可以configuration这一切。 这是一个例子:

 from numpy import arange import matplotlib # import matplotlib as mpl import matplotlib.pyplot # import matplotlib.pyplot as plt x1 = [1,2,3] y1 = [4,5,6] x2 = [1,2,3] y2 = [5,5,5] # initialization fig2 = matplotlib.pyplot.figure(figsize=(8.0, 5.0)) # The size of the figure is specified as (width, height) in inches # lines: l1 = fig2.add_subplot(111).plot(x1,y1, label=r"Text $formula$", "r-", lw=2) l2 = fig2.add_subplot(111).plot(x2,y2, label=r"$legend2$" ,"g--", lw=3) fig2.add_subplot(111).legend((l1,l2), loc=0) # axes: fig2.add_subplot(111).grid(True) fig2.add_subplot(111).set_xticks(arange(1,3,0.5)) fig2.add_subplot(111).axis(xmin=3, xmax=6) # there're also ymin, ymax fig2.add_subplot(111).axis([0,4,3,6]) # all! fig2.add_subplot(111).set_xlim([0,4]) fig2.add_subplot(111).set_ylim([3,6]) # labels: fig2.add_subplot(111).set_xlabel(r"x $2^2$", fontsize=15, color = "r") fig2.add_subplot(111).set_ylabel(r"y $2^2$") fig2.add_subplot(111).set_title(r"title $6^4$") fig2.add_subplot(111).text(2, 5.5, r"an equation: $E=mc^2$", fontsize=15, color = "y") fig2.add_subplot(111).text(3, 2, unicode('f\374r', 'latin-1')) # saving: fig2.savefig("fig2.png") 

那么 – 你想要configuration什么?

在matplotlib(pylab)中有两个主要的选项来控制图像大小:

  1. 您可以以英寸为单位设置生成图像的大小
  2. 您可以定义输出文件的DPI(每英寸点数)(基本上,这是一个分辨率)

通常情况下,您希望同时执行这两个操作,因为这样您将完全控制 以像素为单位生成的图像大小。 例如,如果要渲染800×600的图像,则可以使用DPI = 100,并将尺寸设置为8 x 6英寸:

 import matplotlib.pyplot as plt # plot whatever you need... # now, before saving to file: figure = plt.gcf() # get current figure figure.set_size_inches(8, 6) # when saving, specify the DPI plt.savefig("myplot.png", dpi = 100) 

可以使用任何DPI。 事实上,你可能想玩各种DPI和尺寸值,以得到你最喜欢的结果。 但是,要小心,使用非常小的DPI不是一个好主意,因为matplotlib可能找不到一个好的字体来渲染图例和其他文本。 例如,您不能设置DPI = 1,因为没有带有以1像素呈现的字符的字体:)

从其他评论我明白,你有其他问题是适当的文字渲染。 为此,您还可以更改字体大小。 例如,您可以使用每个字符6个像素,而不是每个字符默认使用12个像素(有效地,使所有文本两次更小)。

 import matplotlib #... matplotlib.rc('font', size=6) 

最后,对原始文档的一些引用: http : //matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.savefig,http : //matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot .gcf , http: //matplotlib.sourceforge.net/api/figure_api.html#matplotlib.figure.Figure.set_size_inches,http://matplotlib.sourceforge.net/users/customizing.html#dynamic-rc-settings

PS对不起,我没有使用pylab,但据我所知,所有上面的代码将在pylab中以相同的方式工作 – 只需用pylab (或导入pylab时分配的任何名称)replace我的代码中的plt 。 相同的matplotlib – 使用pylab来代替。

我想你需要指定一个不同的分辨率时,将图保存到一个文件:

 fig = matplotlib.pyplot.figure() # generate your plot fig.savefig("myfig.png",dpi=600) 

指定较大的dpi值应该与最大化GUI窗口具有相似的效果。

选中此项: 如何使用Python最大化plt.show()窗口

该命令根据您使用的后端而不同。 我发现这是确保保存的图片具有与我在屏幕上观看的图片相同比例的最佳方式。

由于我使用Canopy和QT后端:

 pylab.get_current_fig_manager().window.showMaximized() 

然后根据需要调用savefig(),每个silvado的答案增加DPI。

如果我正确理解你想要做什么,你可以创build你的graphics,并设置窗口的大小。 之后,您可以使用matplotlib工具箱button保存graphics。 这里是一个例子:

 from pylab import get_current_fig_manager,show,plt,imshow plt.Figure() thismanager = get_current_fig_manager() thismanager.window.wm_geometry("500x500+0+0") #in this case 500 is the size (in pixel) of the figure window. In your case you want to maximise to the size of your screen or whatever imshow(your_data) show()