Alf
Alf

Reputation: 1989

Setting the linestyle for the longitude and latitude lines in matplotlib's Basemap?

I am using maplotlib's Basemap to draw maps of the world and want to include longitude and latitude lines. This can be done using drawmeridians() and drawparallels(), but the linestyle of the corresponding lines can only be set via the keyword dashes. According to the documentation, see see here, is should work as follows:

dash pattern for meridians (default [1,1], i.e. 1 pixel on, 1 pixel off)

I tried dashes=[1,0] but that did not worked. Is there any simple way to have solid linestyle?

Here is my code:

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()

Here is the resulting map: enter image description here


Edit 1: I just tried on a different computer and there it works, i.e. dashes=[1,0] results in solid linestyle. The version used on that computer are (according to a pip freeze)

basemap==1.2.0

matplotlib==2.2.3

As soon as I have access again to the original computer, I'll check what is going on there (and which versions are installed).

Edit 2: Being back at the computer where it did not worked, I can now tell a bit more. First, the following versions are used:

basemap==1.1.1

matplotlib==3.0.2

Then the error message (which I forgot to include previously):

ValueError: All values in the dash list must be positive

Edit 3: For the sake of completeness (and since it was partly helpful to hunt down the solution), here is the full 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")

Upvotes: 1

Views: 1475

Answers (1)

Alf
Alf

Reputation: 1989

After some research on some bugreports on github I found the solution [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

Upvotes: 1

Related Questions