Thanks to visit codestin.com
Credit goes to github.com

Skip to content

If Legend shadow=True set framealpha=1 if not passed explicitly instead of consulting rcParams #8988

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 6 commits into from
Aug 11, 2017

Conversation

s0vereign
Copy link
Contributor

PR Summary

This PR fixes a behavior in the legend, that if shadow is set True
the legend will be grayed out.

The changes are in detail:

  1. Change legend behavior in constructor for framealpha,
    so that not the rcParams are not consulted if shadow is set
    to True and framealpha was not passed.
  2. Added a testcase which check for the desired behavior

The following code:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,100)
fig, ax = plt.subplots()
ax.plot(x,x**2,label="test")
leg = ax.legend(shadow=True, facecolor='w')
plt.show()

results in:
fix

The PR fixes Issue #8943 according to @tacaswell instructions.

PR Checklist

  • [βœ…] Has Pytest style unit tests
  • [βœ…] Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

if shadow is True:
self.framealpha = 1
else:
self.get_frame().set_alpha(rcParams["legend.framealpha"])
Copy link
Member

Choose a reason for hiding this comment

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

why are there two ways of setting framealpha? I would stick with the self.get_frame().set_alpha() way, unless we should change it everywhere to self.framealpha. Also, we don't need shadow is True, it can be just shadow.

Copy link
Contributor Author

@s0vereign s0vereign Aug 4, 2017

Choose a reason for hiding this comment

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

Alright will change both lines as proposed.

fig, ax = plt.subplots()
ax.plot(range(100), label="test")
leg = ax.legend(shadow=True, facecolor='w')
assert leg.get_frame().get_alpha() == 1
Copy link
Member

Choose a reason for hiding this comment

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

This will need a new (empty) line at the end to assuage pep8

Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

πŸ‘ for the code.

This case of ignoring the default facealpha needs to be documented in Axes.legend.

Thanks for you work on this!

@@ -384,8 +384,13 @@ def __init__(self, parent, handles, labels,
# init with null renderer
self._init_legend_box(handles, labels, markerfirst)

# If shadow is activated use framealpha if not
Copy link
Member

Choose a reason for hiding this comment

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

This comment is perfect here! Can you also add a note about this to the docstring at https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_axes.py#L298 ?

Copy link
Contributor Author

@s0vereign s0vereign Aug 9, 2017

Choose a reason for hiding this comment

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

Always glad to help. πŸ‘

I would put it into the doc of framealpha:

framealpha : None or float

to make clear that if shadow is activated and framealpha is None it will set the value to be 1 instead of taking it from legend.framealpha :data:rcParam<matplotlib.rcParams>.

Should I also add something to

shadow : None or bool
?

Copy link
Member

Choose a reason for hiding this comment

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

To both? The goal is to make sure users are not confused / can un-confuse them selves easily. Putting it in shadow warns someone using it that it might affect other parts and someone who is having issues with the default framalpha being ignored might go check it's docstring.

@tacaswell tacaswell added this to the 2.1.1 (next bug fix release) milestone Aug 5, 2017
@s0vereign
Copy link
Contributor Author

@tacaswell did you have a look over the recent changes?

Copy link
Member

@dstansby dstansby left a comment

Choose a reason for hiding this comment

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

πŸ‘ LGTM

@tacaswell tacaswell merged commit 363e25f into matplotlib:master Aug 11, 2017
@tacaswell
Copy link
Member

Thank @s0vereign and congratulations on your first Matplotlib PR πŸŽ‰

Hopefully we will hear from you again!

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.1.1 (next bug fix release) Aug 11, 2017
@s0vereign
Copy link
Contributor Author

Will be looking for issues I can fix and get more familiar with the code πŸ‘

@s0vereign s0vereign deleted the shadowfix branch August 12, 2017 21:23
@tacaswell
Copy link
Member

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.

4 participants