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

2024-04-19 19:11:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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

碰巧,我在绘图中有很多数据,当我使用pylab.show()时,我必须在能够正确看到所有绘图之前最大化窗口,并且xlabel标记不会相互重叠。

在将图像保存到文件之前,是否可以通过编程将窗口“最大化”?-目前,我只得到“默认”窗口大小的图像,这将导致x轴标签相互叠加。


Tags: 文件数据name标记图像image绘图matplotlib
3条回答

matplotlib(pylab)中有两个主要选项用于控制图像大小:

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

通常,您希望同时执行这两项操作,因为这样您就可以对生成的图像大小(像素)进行完全控制。例如,如果要精确渲染800x600图像,可以使用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.savefighttp://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.gcfhttp://matplotlib.sourceforge.net/api/figure_api.html#matplotlib.figure.Figure.set_size_incheshttp://matplotlib.sourceforge.net/users/customizing.html#dynamic-rc-settings

抱歉,我没有使用pylab,但据我所知,上面的所有代码在pylab中的工作方式都是一样的-只需用pylab(或导入pylab时指定的任何名称)替换我的代码中的plt。对于matplotlib也一样-改用pylab

我认为在将图形保存到文件时,需要指定不同的分辨率:

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

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

初始化时设置大小:

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

等等你的阴谋的其他方面。你可以全部配置。下面是一个例子:

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")

那么-您到底想配置什么?

相关问题 更多 >