Skip to content

Allow figure.legend to be called without arguments #7811

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

Merged
merged 3 commits into from
Jan 20, 2017

Conversation

dstansby
Copy link
Member

@dstansby dstansby commented Jan 12, 2017

This supersedes #2128.

(p.s. I can squash this once it's all been reviewed)

@codecov-io
Copy link

codecov-io commented Jan 12, 2017

Current coverage is 62.17% (diff: 73.33%)

Merging #7811 into master will increase coverage by 0.08%

@@             master      #7811   diff @@
==========================================
  Files           174        174          
  Lines         56050      56119    +69   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits          34806      34894    +88   
+ Misses        21244      21225    -19   
  Partials          0          0          

Powered by Codecov. Last update 70bb531...8a78947

Copy link
Member

@NelleV NelleV left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some minor comments, but it looks good overall. There is just a what's new item missing.

It'd also be great if we could improve the error message.

@@ -1,19 +1,19 @@
import numpy as np
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you are at it, do you mind adding a docstring to give this example a title and explain what it does? See http://matplotlib.org/devdocs/devel/MEP/MEP12.html for examples.

fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.4, 0.7])
ax2 = fig.add_axes([0.55, 0.1, 0.4, 0.7])
fig, axs = plt.subplots(1, 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -1261,38 +1261,57 @@ def draw_artist(self, a):
def get_axes(self):
return self.axes

def legend(self, handles, labels, *args, **kwargs):
def legend(self, *args, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you are at it, do you mind migrating this to a numpydoc compatible docstring?
(I can also do it if you don't mind me pushing to your branch).

kwargs['loc'] = loc

else:
raise TypeError('Invalid arguments to legend')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we could have an error message more specific? It's quite obscure right now.
I like better "Invalide number of arguments to legend. Possible arguments are (etc)"

@NelleV
Copy link
Member

NelleV commented Jan 13, 2017

And thanks for the patch :)

@dstansby
Copy link
Member Author

Hold on a minute, not quite done! (sorry for the early push)

@dstansby
Copy link
Member Author

Okay, that should be everything (I think!)

'upper left' 2
'lower left' 3
'lower right' 4
'right' 5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why there's this seemingly extra location.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's the same in axes.legend() too.

``legend.handlelength`` :data:`rcParam<matplotlib.rcParams>`.

handletextpad : float or None
The pad between the legend handle and text.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pad -> padding

parameters. The dimensions of these values are given as a fraction
of the fontsize. Values from rcParams will be used if None.
borderaxespad : float or None
The pad between the axes and legend border.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pad -> padding


borderpad : float or None
The fractional whitespace inside the legend border.
Measured in font-size units.
Copy link
Member

@QuLogic QuLogic Jan 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add this on to the end of the previous sentence, and do the same in all the following parameters: The fractional whitespace inside the legend border, measured in font-size units.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the 2 comments below are on bits of docstring directly copied from axes.legend(), so should probably fix them there at some point too.

fig, axes = plt.subplots(2)
axes[0].plot([0, 1], [1, 0], label='x', color='g')
axes[0].plot([0, 1], [0, 1], label='y', color='r')
axes[1].plot([0, 1], [1, 0], label='y', color='r')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when the labels are the same, but the style is different? I believe it should be separate, but it's not tested.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, seems fine, added it in the image test

=============== =============
Location String Location Code
=============== =============
'best' 0 (currently not supported for figure legends)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just omit this line if it is not supported?

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Jan 15, 2017
@dstansby
Copy link
Member Author

Okay, there goes the un-used location code and with it the PEP8 violation. I've also squashed it all down into one commit since there were at least 2 changes to the test figures previously.

@NelleV NelleV changed the title Allow figure.legend to be called without arguments [MRG+1] Allow figure.legend to be called without arguments Jan 16, 2017
handles = []
labels = []

def in_handles(h, l):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to leave a comment as to what's happening here (at least it's not obvious to me at all...).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How's that? If you can think of better wording feel free to push to my branch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Typo: "consdier".
I would make it a comment rather than a docstring to save some whitespace, and get rid of the whitespace between the try... catch blocks (right now it's very spread out).

@anntzer
Copy link
Contributor

anntzer commented Jan 19, 2017

LGTM. Someone else wants to re-review it after the (minor) changes?

@NelleV
Copy link
Member

NelleV commented Jan 20, 2017

LGTM. I'll wait until appveyor's finished before merging.

@NelleV NelleV merged commit 9000e58 into matplotlib:master Jan 20, 2017
@QuLogic QuLogic changed the title [MRG+1] Allow figure.legend to be called without arguments Allow figure.legend to be called without arguments Jan 20, 2017
@dstansby dstansby deleted the figure-legend branch January 22, 2017 13:54
@jklymak jklymak mentioned this pull request Oct 8, 2017
@jnothman
Copy link

jnothman commented Oct 9, 2017

This appears to break invocations like figure.legend(artists, labels=labels), since it assumes labels will be specified as a positional arg, not named. Rendering scikit-learn docs breaks at https://github.com/scikit-learn/scikit-learn/blob/master/examples/neural_networks/plot_mlp_training_curves.py#L88 with the latest release.

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

Successfully merging this pull request may close these issues.

7 participants