Skip to content

PcolorImage does not set _extent #8426

Closed

Description

@Zaharid

To help us understand and resolve your issue, please fill out the form to the best of your ability. You can feel free to delete the sections that do not apply.

Bug report

Bug summary

Creating a simple 2d histogram fails when the bins are not equally spaced under some circumstances.

Code for reproduction

  • A minimum code snippet required to reproduce the bug, also minimizing the number of dependencies required

On a Jupyter notebook

%matplotlib inline
import matplotlib.pyplot as plt
x = plt.hist2d([1,2,3], [3,5,6], bins=[[0,3,7], [1,2,3]])

Actual outcome

  • The output produced by the above code, which may be a screenshot, console output, etc.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/zah/anaconda3/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    305                 pass
    306             else:
--> 307                 return printer(obj)
    308             # Finally look for special method names
    309             method = get_real_method(obj, self.print_method)

/home/zah/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
    238 
    239     if 'png' in formats:
--> 240         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    241     if 'retina' in formats or 'png2x' in formats:
    242         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

/home/zah/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    122 
    123     bytes_io = BytesIO()
--> 124     fig.canvas.print_figure(bytes_io, **kw)
    125     data = bytes_io.getvalue()
    126     if fmt == 'svg':

/home/zah/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2200                 bbox_filtered = []
   2201                 for a in bbox_artists:
-> 2202                     bbox = a.get_window_extent(renderer)
   2203                     if a.get_clip_on():
   2204                         clip_box = a.get_clip_box()

/home/zah/anaconda3/lib/python3.6/site-packages/matplotlib/image.py in get_window_extent(self, renderer)
    704 
    705     def get_window_extent(self, renderer=None):
--> 706         x0, x1, y0, y1 = self._extent
    707         bbox = Bbox.from_extents([x0, y0, x1, y1])
    708         return bbox.transformed(self.axes.transData)

TypeError: 'NoneType' object is not iterable

<matplotlib.figure.Figure at 0x7f828c153240>

This works if I remove the %matplotlib inline above, even if I try to write a png image with e.g. plt.savefig('/tmp/xx.png'). However the bug was found in a code that doesn't use IPython (though I don't know how to reproduce it).

It does work if I change the histogram to:

x = plt.hist2d([1,2,3], [3,5,6], bins=[[0,3,6], [1,2,3]])

making all the bins equally spaced.

Expected outcome

  • No crash.

I'm using the conda forge package of mpl 2.0.

Activity

  1. tacaswell commented on Apr 4, 2017

    @tacaswell
    Member

    For some reason <matplotlib.image.PcolorImage object at 0x7f46ea157080> has None as it's extent. This is blowing up when the bbox_inches tight tries to sort out how big it is, but the underlying issue is that PcolorImage does not have _exentent properly set.

  2. added this to the milestone on Apr 4, 2017
  3. changed the title [-]hist2d fails sometimes when bins are not equally spaced.[/-] [+]PcolorImage does not set `_extent`[/+] on Apr 4, 2017
  4. added
    Difficulty: Medium
    on Apr 4, 2017
  5. tacaswell commented on Apr 4, 2017

    @tacaswell
    Member

    Tagging as medium as this will require a fair amount of code tracing to sort out what is going on.

  6. modified the milestones: , on May 3, 2017
  7. t-bltg commented on Sep 20, 2017

    @t-bltg

    Hitting this issue as well with mpl 2.1.0 (rc1), although a different test case (using a NonUniformImage) ...

    import pylab as plt
    x = plt.hist2d([1,2,3], [3,5,6], bins=[[0,3,7], [1,2,3]])
    im = x[-1]
    
    print(im.get_window_extent())   # <= throws TypeError: 'NoneType' object is not iterable

    workaround

    import pylab as plt
    x = plt.hist2d([1,2,3], [3,5,6], bins=[[0,3,7], [1,2,3]])
    im = x[-1]
    
    im.set_extent(im.get_extent())  # <= workaround
    print(im.get_window_extent())  # no issue
  8. modified the milestones: , on Oct 9, 2017
  9. modified the milestones: , v2.1.1 on Oct 23, 2017
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

    Difficulty: Mediumstatus: confirmed bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions