在matplotlib的底图中设置经度和纬度线的线型?

问题描述 投票:1回答:1

我正在使用maplotlibBasemap来绘制世界地图,并希望包含经度和纬度线。这可以使用drawmeridians()drawparallels()完成,但相应行的linestyle只能通过关键字dashes设置。根据文档,请参阅see here,应该如下工作:

经络的虚线模式(默认[1,1],即1像素开,1像素关)

我尝试了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()

这是结果地图:enter image description here


编辑1:我只是尝试在另一台计算机上工作,即dashes=[1,0]导致实线样式。该计算机上使用的版本是(根据pip freeze

basemap==1.2.0

matplotlib==2.2.3

一旦我再次访问原始计算机,我将检查那里发生了什么(以及安装了哪些版本)。

编辑2:回到没有工作的电脑,我现在可以多说一点。首先,使用以下版本:

basemap==1.1.1

matplotlib==3.0.2

然后是错误消息(我之前忘记包含):

ValueError:破折号列表中的所有值都必须为正数

编辑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")
python matplotlib matplotlib-basemap
1个回答
1
投票

在对github上的一些bug报告进行了一些研究之后,我找到了解决方案[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

© www.soinside.com 2019 - 2024. All rights reserved.