在 matplotlib 的底图中设置经度和纬度线的线型?
Setting the linestyle for the longitude and latitude lines in matplotlib's Basemap?
我正在使用 maplotlib
的 Basemap
绘制世界地图并希望包括经度和纬度线。这可以使用 drawmeridians()
和 drawparallels()
来完成,但是相应线条的线型只能通过关键字 dashes
来设置。根据文档,请参阅 see here,应该按如下方式工作:
dash pattern for meridians (default [1,1], i.e. 1 pixel on, 1 pixel off)
我尝试了 dashes=[1,0]
但没有成功。有没有什么简单的方法可以让线条更立体?
这是我的代码:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
fig1, ax1 = plt.subplots(1,1)
map1 = Basemap( resolution='l', projection='mill',
llcrnrlat=-60., llcrnrlon=-180.,
urcrnrlat=90., urcrnrlon=180. )
map1.drawcoastlines()
map1.drawmapboundary( fill_color='aqua' )
map1.fillcontinents( color='coral', lake_color='aqua' )
# labels=[left,right,top,bottom]
map1.drawparallels( np.arange(-80.,81.,20.), labels=[True,True,False,False] )
map1.drawmeridians( np.arange(-180.,181.,40.), labels=[False,False,True,True] )
plt.show()
这是生成的地图:
编辑 1:我刚在另一台计算机上试过,它可以工作,即 dashes=[1,0]
产生实线样式。该计算机上使用的版本是(根据 pip freeze
)
basemap==1.2.0
matplotlib==2.2.3
一旦我再次访问原来的计算机,我将检查那里发生了什么(以及安装了哪些版本)。
编辑 2:回到它无法工作的计算机前,我现在可以说得更多了。首先,使用以下版本:
basemap==1.1.1
matplotlib==3.0.2
然后是错误信息(我之前忘了包括):
ValueError: All values in the dash list must be positive
编辑 3:为了完整起见(并且因为它对寻找解决方案有一定帮助),这里是完整的 Traceback
:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
return self.func(*args)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 280, in resize
self.show()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 351, in draw
FigureCanvasAgg.draw(self)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 464, in draw
self.figure.draw(self.renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 1143, in draw
renderer, self, dsu, self.suppressComposite)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2409, in draw
mimage._draw_list_compositing_images(renderer, self, dsu)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/lines.py", line 822, in draw
drawFunc(renderer, gc, tpath, affine.frozen())
File "/usr/local/lib/python2.7/dist-packages/matplotlib/lines.py", line 1267, in _draw_lines
self._lineFunc(renderer, gc, path, trans)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/lines.py", line 1297, in _draw_dashed
gc.set_dashes(self._dashOffset, self._dashSeq)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 1007, in set_dashes
raise ValueError("All values in the dash list must be positive")
在研究 github 上的一些错误报告后,我找到了解决方案 [1],dashes=(None,None)
:
map1.drawmeridians( np.arange(-180.,181.,40.), labels=[False,False,True,True], dashes=(None,None) )
[1] https://github.com/matplotlib/basemap/issues/173#issuecomment-68243710
我正在使用 maplotlib
的 Basemap
绘制世界地图并希望包括经度和纬度线。这可以使用 drawmeridians()
和 drawparallels()
来完成,但是相应线条的线型只能通过关键字 dashes
来设置。根据文档,请参阅 see here,应该按如下方式工作:
dash pattern for meridians (default [1,1], i.e. 1 pixel on, 1 pixel off)
我尝试了 dashes=[1,0]
但没有成功。有没有什么简单的方法可以让线条更立体?
这是我的代码:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
fig1, ax1 = plt.subplots(1,1)
map1 = Basemap( resolution='l', projection='mill',
llcrnrlat=-60., llcrnrlon=-180.,
urcrnrlat=90., urcrnrlon=180. )
map1.drawcoastlines()
map1.drawmapboundary( fill_color='aqua' )
map1.fillcontinents( color='coral', lake_color='aqua' )
# labels=[left,right,top,bottom]
map1.drawparallels( np.arange(-80.,81.,20.), labels=[True,True,False,False] )
map1.drawmeridians( np.arange(-180.,181.,40.), labels=[False,False,True,True] )
plt.show()
这是生成的地图:
编辑 1:我刚在另一台计算机上试过,它可以工作,即 dashes=[1,0]
产生实线样式。该计算机上使用的版本是(根据 pip freeze
)
basemap==1.2.0
matplotlib==2.2.3
一旦我再次访问原来的计算机,我将检查那里发生了什么(以及安装了哪些版本)。
编辑 2:回到它无法工作的计算机前,我现在可以说得更多了。首先,使用以下版本:
basemap==1.1.1
matplotlib==3.0.2
然后是错误信息(我之前忘了包括):
ValueError: All values in the dash list must be positive
编辑 3:为了完整起见(并且因为它对寻找解决方案有一定帮助),这里是完整的 Traceback
:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
return self.func(*args)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 280, in resize
self.show()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 351, in draw
FigureCanvasAgg.draw(self)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 464, in draw
self.figure.draw(self.renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 1143, in draw
renderer, self, dsu, self.suppressComposite)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2409, in draw
mimage._draw_list_compositing_images(renderer, self, dsu)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/lines.py", line 822, in draw
drawFunc(renderer, gc, tpath, affine.frozen())
File "/usr/local/lib/python2.7/dist-packages/matplotlib/lines.py", line 1267, in _draw_lines
self._lineFunc(renderer, gc, path, trans)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/lines.py", line 1297, in _draw_dashed
gc.set_dashes(self._dashOffset, self._dashSeq)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 1007, in set_dashes
raise ValueError("All values in the dash list must be positive")
在研究 github 上的一些错误报告后,我找到了解决方案 [1],dashes=(None,None)
:
map1.drawmeridians( np.arange(-180.,181.,40.), labels=[False,False,True,True], dashes=(None,None) )
[1] https://github.com/matplotlib/basemap/issues/173#issuecomment-68243710