Hom*_*lli 48 python matplotlib
我在matplotlib中使用pylab来创建绘图并将绘图保存到图像文件中.但是,当我使用保存图像时pylab.savefig( image_name ),我发现保存的SIZE图像与我使用时显示的图像相同pylab.show().
碰巧,我在绘图中有很多数据,当我使用时pylab.show(),我必须最大化窗口才能正确看到所有绘图,并且xlabel代码不会相互叠加.
无论如何,我可以在将图像保存到文件之前以编程方式"最大化"窗口吗? - 目前,我只获得'默认'窗口大小的图像,这导致x轴标签相互叠加.
Tim*_*mur 48
matplotlib(pylab)有两个主要选项来控制图像大小:
通常情况下,您希望同时执行这两项操作,因为这样您就可以完全控制生成的图像大小(以像素为单位).例如,如果要精确渲染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)
Run Code Online (Sandbox Code Playgroud)
可以使用任何DPI.实际上,您可能希望使用各种DPI和大小值来获得您最喜欢的结果.但要注意,使用非常小的DPI并不是一个好主意,因为matplotlib可能找不到好的字体来渲染图例和其他文本.例如,你不能设置DPI = 1,因为没有字体用1像素渲染的字符:)
从其他评论中我了解到,您遇到的其他问题是正确的文本呈现.为此,您还可以更改字体大小.例如,您可以为每个字符使用6个像素,而不是默认使用每个字符12个像素(实际上,使所有文本都缩小两倍).
import matplotlib
#...
matplotlib.rc('font', size=6)
Run Code Online (Sandbox Code Playgroud)
最后,对原始文档的一些引用: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中都会以相同的方式工作 - 只需用plt我的代码替换pylab(或者在导入pylab时指定的任何名称).相同matplotlib- pylab改为使用.
Ado*_*obe 26
您在初始化时设置大小:
fig2 = matplotlib.pyplot.figure(figsize=(8.0, 5.0)) # in inches!
Run Code Online (Sandbox Code Playgroud)
编辑:
如果问题是x轴刻度 - 您可以"手动"设置它们:
fig2.add_subplot(111).set_xticks(arange(1,3,0.5)) # You can actually compute the interval You need - and substitute here
Run Code Online (Sandbox Code Playgroud)
等等你的情节的其他方面.您可以全部配置它.这是一个例子:
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")
Run Code Online (Sandbox Code Playgroud)
那么 - 您究竟想要配置什么?
我认为在将图形保存到文件时需要指定不同的分辨率:
fig = matplotlib.pyplot.figure()
# generate your plot
fig.savefig("myfig.png",dpi=600)
Run Code Online (Sandbox Code Playgroud)
指定大的dpi值应该与最大化GUI窗口具有类似的效果.
| 归档时间: |
|
| 查看次数: |
93026 次 |
| 最近记录: |