[英]Setting annotations on matplotlib-basemap image causes a width/height error that cannot be undone
I'm having a problem with plotting annotations on basemap and saving the image to a file. 我在底图上绘制注释并将图像保存到文件时遇到问题。 I have seen this problem raised several times before, but in my case following the advice didn't work and then the error persists even when reversing the code back to a previously working version without annotations.
我之前已经看到过多次出现此问题的情况,但是在我的情况下,遵循建议的建议不起作用,即使将代码恢复为没有注释的以前的版本,该错误仍然存在。
Basically, without annotations, the figure has been plotting without problem for weeks. 基本上,没有注释,该图已经绘制了好几周没有问题。 Now if I add around 130 annotations to the graph I get:
现在,如果我在图形上添加约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
So my first thought was that I have a problem with the way I'm adding annotations iteratively (I had other errors earlier trying to feed it an array), so hashed that code out ie back to my old code base, and the problem is still there. 所以我首先想到的是,我迭代添加批注的方式存在问题(我在尝试将其输入数组时遇到了其他错误),因此将该代码散列到了我的旧代码库中,问题是还在那里。 I restart the kernel in Enthought Canopy and it's happy again, then adding annotations breaks it again until a kernel restart.
我在Enthought Canopy中重新启动了内核,再次感到高兴,然后添加注释再次破坏了它,直到内核重新启动。
This is the code: 这是代码:
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()
Some other strange things. 其他一些奇怪的事情。 I believe I'm explicit and correct in limiting the width and height already in
plt.savefig
(taken from previous SO threads eg this ) that this shouldn't be happening? 我相信我是明确的,正确的限制已经在宽度和高度
plt.savefig
(从以前的SO线程如采取此 ),这不应该发生? Also, plt.show()
does not throw an error. 另外,
plt.show()
不会引发错误。 I require the high resolution to be able to zoom in to the map at street level. 我需要高分辨率才能放大街道级别的地图。 I have been able to get single annotations onto the image with no problem, even annotations for locations outside of the map area, which I thought could have been causing it to try display something way outside of the image.
我已经能够毫无问题地将单个注释添加到图像上,即使是地图区域之外的位置的注释也是如此,我认为这可能导致它尝试在图像外部显示某种方式。
Setting DPI down to 200 does not remove the error. 将DPI设置为200不会消除该错误。 Renaming
annotations
array didn't make any difference (in case it somehow caused problems for basemap internals). 重命名
annotations
数组没有任何区别(以防它以某种方式引起底图内部问题)。
Does anyone have any suggestions on how to stop this? 有人对如何阻止这种情况有任何建议吗? Have I incorrectly set something up in
plt.savefig
? 我是否在
plt.savefig
设置了错误? Many thanks in advance. 提前谢谢了。
PS The annotations array: PS批注数组:
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
After so many hours, I tracked it down to a conflict with bbox_inches="tight"
Removing this will remove the error when a label is slightly angled to come outside the frame of the original picture (which happens because of my lat/long offset for label position). 几个小时后,我发现它与
bbox_inches="tight"
发生冲突。如果标签略微倾斜以超出原始图片的框架,则删除此错误将消除此错误(这是由于我的经/纬度偏移标签位置)。 I'm not sure that the error message provides any meaningful feedback to the root cause of the problem :( 我不确定错误消息是否为问题的根本原因提供了有意义的反馈:(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.