gpt4 book ai didi

python - 在matplotlib的 basemap 中设置经度和纬度线的线条样式?

转载 作者:行者123 更新时间:2023-12-01 08:20:44 26 4
gpt4 key购买 nike

我正在使用maplotlibBasemap来绘制世界地图,并希望包括经度和纬度线。这可以使用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()

这是生成的 map :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: All values in the dash list must be positive

编辑 3:为了完整起见(并且因为它对寻找解决方案有部分帮助),这里是完整的回溯:

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

关于python - 在matplotlib的 basemap 中设置经度和纬度线的线条样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54663882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com