question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug in contour x/y values filtering in mpl_toolkits/basemap/__init__.py ?

See original GitHub issue

I have been trying to plot unstructured data with contour and contourf, using the tri=True parameter. It works fine with contourf, but I get a huge traceback when using contour on the same data.

[...]
  File "/home/share/unix_files/cdat/versions/cdat_install_uv-2.1.0_x86_64_gcc4_13/lib/python2.7/site-packages/matplotlib-1.4.0-py2.7-linux-x86_64.egg/matplotlib/tri/triangulation.py", line 55, in __init__
    self.triangles, self._neighbors = _qhull.delaunay(x, y)
ValueError: x and y arrays must have a length of at least 3

After spending some time in the python debugger, I have found out that indeed the x and y coordinate arrays passed to the triangulation function had a zero size (and therefore less than 3 elements) because they were empty! More digging helped me find a very suspicious way of filtering the x and y values in the contour(self,x,y,data,args,*kwargs) function

                # for unstructured grids, toss out points outside
                # projection limb (don't use those points in triangulation).
[...]
                mask = np.logical_or(x<self.xmin,y<self.xmin) +\
                       np.logical_or(x>self.xmax,y>self.xmax)
                x = np.compress(mask,x)
                y = np.compress(mask,y)

Why would y be compared to the values of xmin and xmax instead of ymin and ymax, and is the logical combination ok???

I think we want to keep the values where: xmin<=x<=xmax AND ymin<=y<=ymax. And the mask has to be True where we want to keep the value, in np.compress! This is a bit misleading because it works in the opposite way that masks work in np.ma …

The contour function works fine if I replace the mask definition above with

                mask = np.logical_and(np.logical_and(x>=self.xmin, x<=self.xmax),
                                      np.logical_and(y>=self.ymin, y<=self.ymax))

Can somebody review this? And it may be wise to use a slightly less misleading name for the mask variable. Maybe replace mask with select_xy_ok?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:2
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
guidocionicommented, Jan 28, 2019

Is there any update on this? I cannot plot unstructured data with basemap contour and using tricontour from matplotlib.pyplot gives me an Error in qhull Delaunay triangulation.

0reactions
maxpittorecommented, Aug 31, 2018

Hallo, it seems the bug is still there. Any news ? It is a pity contour cannot be used on unstructured lists. Besides this, thank you for your efforts, very nice package !

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How can I get the (x,y) values of the line that is plotted ...
Look at the collections property of the returned ContourSet. ... To get a NumPy array of the coordinates, use the Path.vertices attribute.
Read more >
Contour Plots - Problem Solving with Python
A general way to modify the color scheme is to call Matplotlib's plt. get_cmap() function that outputs a color map object.
Read more >
Plotting contours of Z on an x-y axis where Z is not a function ...
I cannot find a way of plotting a contour map where Z is not a function of x or y. The measurements of...
Read more >
Contour lines look blocky, appearing to trace raster cell ...
Cause. The raster values are integers, which align with the contour. This is not a bug, it is exact contouring of the data....
Read more >
matplotlib.axes.Axes.contour — Matplotlib 3.6.2 documentation
The coordinates of the values in Z. X and Y must both be 2D with the same shape as Z (e.g. created via...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found