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

Generating legend from figure options panel of qt backend raise exception for large number of plots #5636

Closed
Acanthostega opened this issue Dec 8, 2015 · 1 comment
Labels
Milestone

Comments

@Acanthostega
Copy link
Contributor

Trying to generate a legend from the figure options panel (in the qt backend) when there is more than 2 plots on the graph raise a exception.

Problem comes from this part of the code inside matplotlib/backends/qt_editor/figureoptions.py for the figure_edit function:

        if generate_legend:
            draggable = None
            ncol = None
            if axes.legend_ is not None:
                old_legend = axes.get_legend()
                draggable = old_legend._draggable is not None
                ncol = old_legend._ncol
            new_legend = axes.legend(ncol=ncol)
            if new_legend:
                new_legend.draggable(draggable)

ncol is always set to None, except for cases of an already existing legend. Inside the axes.legend method, internally ncol is set to 1 if the number of handles (which is the list of artists (lines, patches) to be added to the legend) is less than 2. So setting ncol=None inside figure_edit works when the number of plots is less than 2, but not instead, since ncol is already set to None.

Steps to reproduce in interactive mode in ipython (pylab loaded) with qt backend:

>>> ax.subplot(111)
>>> x = arange(100)
>>> y = sin(x)
>>> ax.plot(x, y, label="1")
>>> ax.plot(x, y, label="2")
>>> ax.plot(x, y, label="3")
>>> ax.plot(x, y, label="4")
>>> ax.plot(x, y, label="5")

then select in the figure options panel to generate a legend, then apply.

I started to write a PR with an option in the panel, next to the legend generation for the number of columns, but thought that someone has surely a better idea than this...

@tacaswell
Copy link
Member

TypeError                                 Traceback (most recent call last)
/home/tcaswell/source/my_source/matplotlib/lib/matplotlib/backends/backend_qt5.py in edit_parameters(self)
    643                     return
    644 
--> 645             figureoptions.figure_edit(axes, self)
    646 
    647     def _update_buttons_checked(self):

/home/tcaswell/source/my_source/matplotlib/lib/matplotlib/backends/qt_editor/figureoptions.py in figure_edit(axes, parent)
    176                             apply=apply_callback)
    177     if data is not None:
--> 178         apply_callback(data)

/home/tcaswell/source/my_source/matplotlib/lib/matplotlib/backends/qt_editor/figureoptions.py in apply_callback(data)
    164                 draggable = old_legend._draggable is not None
    165                 ncol = old_legend._ncol
--> 166             new_legend = axes.legend(ncol=ncol)
    167             if new_legend:
    168                 new_legend.draggable(draggable)

/home/tcaswell/source/my_source/matplotlib/lib/matplotlib/axes/_axes.py in legend(self, *args, **kwargs)
    536             raise TypeError('Invalid arguments to legend.')
    537 
--> 538         self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
    539         self.legend_._remove_method = lambda h: setattr(self, 'legend_', None)
    540         return self.legend_

/home/tcaswell/source/my_source/matplotlib/lib/matplotlib/legend.py in __init__(self, parent, handles, labels, loc, numpoints, markerscale, markerfirst, scatterpoints, scatteryoffsets, prop, fontsize, borderpad, labelspacing, handlelength, handleheight, handletextpad, borderaxespad, columnspacing, ncol, mode, fancybox, shadow, title, framealpha, bbox_to_anchor, bbox_transform, frameon, handler_map)
    383 
    384         # init with null renderer
--> 385         self._init_legend_box(handles, labels, markerfirst)
    386 
    387         if framealpha is None:

/home/tcaswell/source/my_source/matplotlib/lib/matplotlib/legend.py in _init_legend_box(self, handles, labels, markerfirst)
    659             # (num_largecol) columns will have (nrows+1) rows, and remaining
    660             # (num_smallcol) columns will have (nrows) rows.
--> 661             ncol = min(self._ncol, len(handleboxes))
    662             nrows, num_largecol = divmod(len(handleboxes), ncol)
    663             num_smallcol = ncol - num_largecol

TypeError: unorderable types: int() < NoneType()

for reference is the exception .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants