Python 在matplotlib basemap图像上设置注释会导致无法撤消的宽度/高度错误
我在basemap上打印注释并将图像保存到文件时遇到问题。我以前见过多次出现这个问题,但在我的例子中,遵循建议不起作用,即使将代码恢复到以前没有注释的工作版本,错误仍然存在 基本上,在没有注释的情况下,这幅图已经绘制了好几个星期没有问题了。现在,如果我在图表中添加大约130个注释,我会得到:Python 在matplotlib basemap图像上设置注释会导致无法撤消的宽度/高度错误,python,matplotlib,matplotlib-basemap,Python,Matplotlib,Matplotlib Basemap,我在basemap上打印注释并将图像保存到文件时遇到问题。我以前见过多次出现这个问题,但在我的例子中,遵循建议不起作用,即使将代码恢复到以前没有注释的工作版本,错误仍然存在 基本上,在没有注释的情况下,这幅图已经绘制了好几个星期没有问题了。现在,如果我在图表中添加大约130个注释,我会得到: --------------------------------------------------------------------------- ValueError
---------------------------------------------------------------------------
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
还有一些奇怪的事情。我相信我已经在plt.savefig
中明确且正确地限制了宽度和高度(例如,取自之前的SO线程),这不应该发生吗?另外,plt.show()
不会引发错误。我需要高分辨率,以便能够放大到街道级别的地图。我已经能够毫无问题地在图像上获得单个注释,甚至是地图区域以外位置的注释,我认为这可能导致它尝试在图像之外显示某些内容
将DPI设置为200并不能消除该错误。重命名annotations
array没有任何区别(以防它以某种方式导致basemap内部出现问题)
有没有人对如何阻止这种情况有什么建议?我是否在plt.savefig
中设置错误?非常感谢
附注:注释数组:
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
经过这么多小时,我发现它与
bbox_inches=“tight”
冲突。删除此项将删除标签略微倾斜而超出原始图片边框时的错误(这是由于标签位置的横向/纵向偏移造成的)。我不确定错误消息是否为问题的根本原因提供了任何有意义的反馈:(
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