Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matplotlib 1.5 broken_barh fails on empty data. #5572

Closed
mscuthbert opened this issue Nov 27, 2015 · 2 comments
Closed

Matplotlib 1.5 broken_barh fails on empty data. #5572

mscuthbert opened this issue Nov 27, 2015 · 2 comments
Assignees
Milestone

Comments

@mscuthbert
Copy link
Contributor

A change from 1.4 to 1.5 has caused subplots to not be able to plot broken bars where the xrange has no data. Previously an empty list as xranges worked fine (no data was added to the subplot). Now it throws an IndexError exception:

  File "/home/travis/build/cuthbertLab/music21base/music21/graph.py", line 1058, in process
    ax.broken_barh(xranges, yrange, facecolors=faceColor, alpha=self.alpha)
  File "/home/travis/miniconda2/lib/python3.4/site-packages/matplotlib/__init__.py", line 1811, in inner
    return func(ax, *args, **kwargs)
  File "/home/travis/miniconda2/lib/python3.4/site-packages/matplotlib/axes/_axes.py", line 2336, in broken_barh
    self._process_unit_info(xdata=xranges[0],
  IndexError: list index out of range

This could be considered a backwards-incompatible issue. Or maybe a feature. In any case, it was something that my toolkit (music21) was counting on being able to use in some cases; a simple "if len(xranges) > 0:" wrap around the call fixed it.

@mdboom
Copy link
Member

mdboom commented Nov 27, 2015

Can you provide a self-contained standalone example to reproduce this so I can git bisect to try to find the culprit?

It's cool to hear that music21 is using matplotlib -- I wasn't aware of that. music21 looks like a really cool project (I used to dabble in computational musicology, but haven't been plugged into the field since the early 2000's).

@mscuthbert
Copy link
Contributor Author

Here's a minimal program that demonstrates the problem:

from matplotlib import pyplot

subplot = pyplot.subplot(1, 1, 1)
xrangesList = [[(0.2, 0.4), (0.6, 0.8)], []]
yrange = (.1, .5)
for xranges in xrangesList:
    subplot.broken_barh(xranges, yrange)

It's the second, empty list that causes the problem. Empty lists in xrangesList arise in our program because we were generating a bar graph for every data element but some of them didn't have a range associated with it. That could definitely be something on the "user-error" side for us to fix; it's only because it worked before that I mention it. The docs could instead be changed from "a sequence of" to "a non-empty sequence of" in various places.

Since axes._base._AxesBase()._process_unit_info allows xdata and ydata to be None, this could be a fix in broken_barh:

In axes/_axes.py

        self._process_unit_info(xdata=xranges[0],
                                ydata=yrange[0],
                                kwargs=kwargs)

use:

        if len(xranges) > 0:
            xdata = xranges[0]
        else:
            xdata = None
        if len(yrange) > 0:
            ydata = yrange[0]
        else:
            ydata = None

        self._process_unit_info(xdata=xdata,
                                ydata=ydata,
                                kwargs=kwargs)

Thanks for the kind words about computational musicology! Yes, we've been using matplotlib since 2007 for all our graphing needs. We don't use it as well as we could because we were still learning Python and numpy when we added graphing capabilities to the system and it's worked well enough that we haven't done a refactor. We absolutely love that we can use high code points in our axes, which makes this the only system that lets us use real sharp and flat signs in our axes. And everything else matplotlib enables.

@mdboom mdboom added this to the Critical bugfix release (1.5.1) milestone Dec 1, 2015
mdboom added a commit to mdboom/matplotlib that referenced this issue Dec 1, 2015
@mdboom mdboom self-assigned this Dec 1, 2015
mdboom added a commit to mdboom/matplotlib that referenced this issue Dec 10, 2015
@mdboom mdboom closed this as completed in f255c33 Dec 28, 2015
efiring added a commit that referenced this issue Dec 28, 2015
Fix #5572: Allow passing empty range to broken_barh
efiring added a commit that referenced this issue Dec 28, 2015
Fix #5572: Allow passing empty range to broken_barh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants