matplotlib / matplotlib/basemap

error plotting points that wrap around longitudinal map boundaries in plot() method of Basemap

Open
#214 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
817
Forks
395
PR merge metrics
No merged PRs in 30d

Description

Hi

Using Python 2.7.8 and Basemap 1.0.7 there seems to be a problem plotting points that wrap around longitudinal map boundaries in plot() method of Basemap if the points wrap back and forth. The problem can be avoided by sorting the points by longitude prior plotting (but this does of course not work if plotting a segment based on the points since sorting will screw up the order of the points along the segment)

The following code illustrates the problem by plotting the same three points sorted in different ways (first plot works)

#! /usr/bin/env python

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

bm = Basemap(llcrnrlon=0,llcrnrlat=-80,urcrnrlon=360,urcrnrlat=80,projection='mill')
fig=plt.figure(figsize=(8,4.5))   

# figure with three points work
bm.plot([-30,-20,10], [10,10,10], "ro", latlon=True)
plt.show()

# figure with less than three points fail
bm.plot([-30,10,-20], [10,10,10], "ro", latlon=True)
plt.show()

and the error running the above code (after closing the first successful plot window) is

:~/SNSN/ALERT/seedlink> python TryBasemap.py 
Traceback (most recent call last):
  File "TryBasemap.py", line 16, in <module>
    bm.plot([-30,10,-20], [10,10,10], latlon=True)
  File "[...]/python2.7/site-packages/mpl_toolkits/basemap/__init__.py", line 536, in with_transform
    x = self.shiftdata(x)
  File "[...]/python2.7/site-packages/mpl_toolkits/basemap/__init__.py", line 4777, in shiftdata
    if itemindex:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

regP

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the supplied Python script, then inspect mpl_toolkits/basemap/init.py at the plot wrapper and shiftdata method around the reported line. The fix is done when the unsorted [-30, 10, -20] points plot without the ambiguous-array error while preserving their order.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.