FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Issue with single contour level in contourf · Issue #138 · matplotlib/matplotlib · GitHub

Repository navigation

Issue with single contour level in contourf #138

Closed

Description

The following code works:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.contourf([[0.,1.], [1.,0.]], levels=[0.5, 1.0, 2.0])
fig.savefig('contourf.png')

but if I try and specify a single level, the code crashes:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.contourf([[0.,1.], [1.,0.]], levels=[0.5])
fig.savefig('contourf.png')

The exception is:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    ax.contourf([[0.,1.], [1.,0.]], levels=[0.5])
  File "/Users/tom/Library/Python/2.6/site-packages/matplotlib/axes.py", line 7403, in contourf
    return mcontour.QuadContourSet(self, *args, **kwargs)
  File "/Users/tom/Library/Python/2.6/site-packages/matplotlib/contour.py", line 1103, in __init__
    ContourSet.__init__(self, ax, *args, **kwargs)
  File "/Users/tom/Library/Python/2.6/site-packages/matplotlib/contour.py", line 719, in __init__
    self.allsegs, self.allkinds = self._get_allsegs_and_allkinds()
  File "/Users/tom/Library/Python/2.6/site-packages/matplotlib/contour.py", line 1136, in _get_allsegs_and_allkinds
    lowers, uppers = self._get_lowers_and_uppers()
  File "/Users/tom/Library/Python/2.6/site-packages/matplotlib/contour.py", line 820, in _get_lowers_and_uppers
    if self.zmin == lowers[0]:
IndexError: index out of bounds

Activity

  1. efiring commented on Jun 8, 2011

    Member

    On 06/08/2011 11:18 AM, astrofrog wrote:

    The following code works:

     import matplotlib as mpl
     mpl.use('Agg')
     import matplotlib.pyplot as plt
    
     fig = plt.figure()
     ax = fig.add_subplot(1, 1, 1)
     ax.contourf([[0.,1.], [1.,0.]], levels=[0.5, 1.0, 2.0])
     fig.savefig('contourf.png')
    

    but if I try and specify a single level, the code crashes:

     import matplotlib as mpl
     mpl.use('Agg')
     import matplotlib.pyplot as plt
    
     fig = plt.figure()
     ax = fig.add_subplot(1, 1, 1)
     ax.contourf([[0.,1.], [1.,0.]], levels=[0.5])
     fig.savefig('contourf.png')
    

    contourf contours between levels, so there is not much it can do with
    a single level. The error (an invalid input) could be trapped at an
    early stage. That sort of input validation is done somewhat erratically
    in mpl. Personally, I don't want to see us try to anticipate every
    possible input error and clutter the code with validation, but some
    types of error are worth trapping.

    Eric

    The exception is:

     Traceback (most recent call last):
       File "test.py", line 7, in<module>
         ax.contourf([[0.,1.], [1.,0.]], levels=[0.5])
       File "/Users/tom/Library/Python/2.6/site-packages/matplotlib/axes.py", line 7403, in contourf
         return mcontour.QuadContourSet(self, *args, **kwargs)
       File "/Users/tom/Library/Python/2.6/site-packages/matplotlib/contour.py", line 1103, in __init__
         ContourSet.__init__(self, ax, *args, **kwargs)
       File "/Users/tom/Library/Python/2.6/site-packages/matplotlib/contour.py", line 719, in __init__
         self.allsegs, self.allkinds = self._get_allsegs_and_allkinds()
       File "/Users/tom/Library/Python/2.6/site-packages/matplotlib/contour.py", line 1136, in _get_allsegs_and_allkinds
         lowers, uppers = self._get_lowers_and_uppers()
       File "/Users/tom/Library/Python/2.6/site-packages/matplotlib/contour.py", line 820, in _get_lowers_and_uppers
         if self.zmin == lowers[0]:
     IndexError: index out of bounds
    
  2. astrofrog commented on Jun 8, 2011

    ContributorAuthor

    Ok, that makes sense. One possibility would be that if the length of the levels iterable is 1, one could append +inf as the second value (since that is probably the expected behavior if people try and specify a single value). Just a thought, but feel free to close the ticket without any changes.

  3. efiring commented on Jun 16, 2011

    Member

    Closed with 58c4713. It turns out that a check for 2 or more levels for contourf was already being made, but it was missing the case where the levels were specified via a kwarg. I slight rearrangement fixed that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL