我在绘制basemap上的注释并将图像保存到一个文件时遇到了问题。我以前见过几次提出这个问题,但在我的例子中,遵循这个建议不起作用,即使将代码反转回一个没有注释的以前工作的版本时,错误仍然存在。
基本上,在没有注释的情况下,数周来这个数字一直在毫无问题地绘制。现在,如果我将大约130个注释添加到图中,就会得到:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
C:\Users\Joshua\Canopy\scripts\Client Model\nicoleheatmap3.py in <module>()
251 '''
252 #plt.show()
--> 253 plt.savefig('Demand_map.png',bbox_inches="tight",figsize=(800/96, 800/96),dpi=1600)
254 plt.close()
255
C:\Users\Joshua\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\pyplot.pyc in savefig(*args, **kwargs)
575 def savefig(*args, **kwargs):
576 fig = gcf()
--> 577 res = fig.savefig(*args, **kwargs)
578 draw() # need this if 'transparent=True' to reset colors
579 return res
C:\Users\Joshua\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\figure.pyc in savefig(self, *args, **kwargs)
1474 self.set_frameon(frameon)
1475
-> 1476 self.canvas.print_figure(*args, **kwargs)
1477
1478 if frameon:
C:\Users\Joshua\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_qt5agg.pyc in print_figure(self, *args, **kwargs)
159
160 def print_figure(self, *args, **kwargs):
--> 161 FigureCanvasAgg.print_figure(self, *args, **kwargs)
162 self.draw()
163
C:\Users\Joshua\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
2209 orientation=orientation,
2210 bbox_inches_restore=_bbox_inches_restore,
-> 2211 **kwargs)
2212 finally:
2213 if bbox_inches and restore_bbox:
C:\Users\Joshua\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)
519
520 def print_png(self, filename_or_obj, *args, **kwargs):
--> 521 FigureCanvasAgg.draw(self)
522 renderer = self.get_renderer()
523 original_dpi = renderer.dpi
C:\Users\Joshua\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_agg.pyc in draw(self)
462 if __debug__: verbose.report('FigureCanvasAgg.draw', 'debug-annoying')
463
--> 464 self.renderer = self.get_renderer(cleared=True)
465 # acquire a lock on the shared font cache
466 RendererAgg.lock.acquire()
C:\Users\Joshua\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_agg.pyc in get_renderer(self, cleared)
479
480 if need_new_renderer:
--> 481 self.renderer = RendererAgg(w, h, self.figure.dpi)
482 self._lastKey = key
483 elif cleared:
C:\Users\Joshua\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_agg.pyc in __init__(self, width, height, dpi)
92 self.height = height
93 if __debug__: verbose.report('RendererAgg.__init__ width=%s, height=%s'%(width, height), 'debug-annoying')
---> 94 self._renderer = _RendererAgg(int(width), int(height), dpi, debug=False)
95 self._filter_renderers = []
96
ValueError: width and height must each be below 32768因此,我的第一个想法是,我对迭代添加注释的方式有问题(我之前还有些错误试图将其作为数组提供给它),因此对代码进行了散列处理,即返回到我以前的代码库,问题仍然存在。我重新启动了Enthought中的内核,它又高兴了,然后添加注释会再次破坏它,直到内核重新启动为止。
这是代码:
m = Basemap(projection='merc',llcrnrlon=53.898184,llcrnrlat=24.019957,urcrnrlon=56.721985,urcrnrlat=26.076649,lon_0=55.545,lat_0=24.735,resolution='i')
base_image = plt.imread("UAEpeninsulagrey.jpg")
implot = plt.imshow(base_image)
m.imshow(base_image,origin='upper')
a,b = m(preparePlot[1],preparePlot[0])
m.scatter(a,b,marker="o",s=preparePlot[2],color="#0000FF",alpha=0.5,)
c,d = m(preparePlot[4],preparePlot[3])
m.scatter(c,d,marker="o",s=6,color="#FF0000",alpha=0.5,)
'''
for t in range(len(annotations[0])):
x,y = m(annotations[1][t],annotations[0][t])
x2,y2 = m(annotations[4][t],annotations[3][t])
plt.annotate(annotations[2][t],xy=(x,y),xytext=(x2,y2),arrowprops=dict(facecolor='black', shrink=0.05),fontsize=2)
'''
#plt.show()
plt.savefig('Demand_map.png',bbox_inches="tight",figsize=(800/96, 800/96),dpi=1600)
plt.close()其他一些奇怪的事情。我相信我在限制plt.savefig中已经存在的宽度和高度(从以前的SO线程(例如this ))中限制宽度和高度方面是明确和正确的,这是不应该发生的?而且,plt.show()不会抛出错误。我需要高分辨率才能放大到地图的街道水平。我已经能够在图像上得到一个注释,没有问题,甚至是地图区域以外的位置的注释,我认为这可能会导致它试图在图像之外显示一些东西。
将DPI设置为200不会删除错误。重命名annotations数组没有任何区别(万一它会给basemap内部组件带来问题)。
有没有人对如何阻止这件事有任何建议?我是否在plt.savefig中设置了错误的东西?在此之前,非常感谢您。
P.S.注释数组:
annotations[0][0]
Out[14]: 26.053275624999998
annotations[1][0]
Out[15]: 56.068128225
annotations[2][0]
Out[16]: 'Tot: 1, 0%'
annotations[3][0]
Out[17]: 26.043275624999996
annotations[4][0]
Out[18]: 56.058128225000004发布于 2015-10-29 17:56:29
在这么多小时之后,我追踪到了它与bbox_inches="tight"的冲突--这将消除当标签稍微倾斜到原始图片的框架之外时的错误(这是因为我的lat/long位置的长偏移)。我不确定错误消息是否为问题的根源提供了有意义的反馈:
https://stackoverflow.com/questions/33417908
复制相似问题