TypeError in Axes.get_legend_handles_labels #775

Closed
wesm opened this Issue Mar 19, 2012 · 4 comments

Projects

None yet

3 participants

@wesm
wesm commented Mar 19, 2012

I had to hack this method due to a TypeError in master. No time for a pull request or unit test

    def get_legend_handles_labels(self, legend_handler_map=None):
        """
        Return handles and labels for legend

        ``ax.legend()`` is equivalent to ::

          h, l = ax.get_legend_handles_labels()
          ax.legend(h, l)

        """

        handles = []
        labels = []
        for handle in self._get_legend_handles(legend_handler_map):
            label = handle.get_label()
            #if (label is not None and label != '' and not label.startswith('_')):
            if label and not (isinstance(label, basestring) and label.startswith('_')):
                handles.append(handle)
                labels.append(label)

        return handles, labels
@mdboom
Member
mdboom commented Mar 20, 2012

Can you provide some code to reproduce the bug? Also, this doesn't seem equivalent -- if label == '' it will be added to the legend in the new version of your code.

@wesm
wesm commented Mar 20, 2012

I'll go back and try to get you a reproduction later today. Obviously if label is not a string (for me it was an integer) then label == '' fails

@wesm
wesm commented Mar 27, 2012

Here you go:

ax = plt.gca()
ax.bar(np.arange(10), np.arange(10), label=1910)
plt.legend()

Happens if label is an integer

@dmcdougall
Member

Fixed in #1107.

@dmcdougall dmcdougall closed this Jan 26, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment