如何使 pylab.savefig() 为“最大化"窗口保存图像而不是默认大小 [英] How to make pylab.savefig() save image for 'maximized' window instead of default size
问题描述
我在matplotlib中使用pylab创建图并将其保存到图像文件中.但是,当我使用 pylab.savefig( image_name )
保存图像时,我发现保存的 SIZE 图像与使用 pylab.show()
.
I am using pylab in matplotlib to create a plot and save the plot to an image file. However, when I save the image using pylab.savefig( image_name )
, I find that the SIZE image saved is the same as the image that is shown when I use pylab.show()
.
碰巧的是,我的图中有很多数据,当我使用 pylab.show()
时,我必须先最大化窗口,然后才能正确地看到所有的图,并且 xlabel 代码不会相互叠加.
As it happens, I have a lot of data in the plot and when I am using pylab.show()
, I have to maximize the window before I can see all of the plot correctly, and the xlabel tickers don't superimpose on each other.
无论如何,我可以在将图像保存到文件之前以编程方式最大化"窗口吗?- 目前,我只得到默认"窗口大小的图像,这导致 x 轴标签相互叠加.
Is there anyway that I can programmatically 'maximize' the window before saving the image to file? - at the moment, I am only getting the 'default' window size image, which results in the x axis labels being superimposed on one another.
推荐答案
您在初始化时设置大小:
You set the size on initialization:
fig2 = matplotlib.pyplot.figure(figsize=(8.0, 5.0)) # in inches!
编辑:
如果问题出在x轴刻度上-您可以手动"设置它们:
If the problem is with x-axis ticks - You can set them "manually":
fig2.add_subplot(111).set_xticks(arange(1,3,0.5)) # You can actually compute the interval You need - and substitute here
等等以及你情节的其他方面.您可以全部配置.举个例子:
And so on with other aspects of Your plot. You can configure it all. Here's an example:
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")
那么-您到底要配置什么?
So - what exactly do You want to be configured?
这篇关于如何使 pylab.savefig() 为“最大化"窗口保存图像而不是默认大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!