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

Skip to content

Add legend.labelcolor in rcParams #20084

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

Conversation

Carloscerq
Copy link
Contributor

@Carloscerq Carloscerq commented Apr 26, 2021

PR Summary

This is a proposal for a new legend rcParam.
The legend.labelcolor was proposed in #20049. It's for changing the labelcolor of all figures(similar to the labelcolor of plt.legend)

Example:

import numpy as np
import lib.matplotlib.pyplot as plt

a = np.arange(2, 3, .02)
c = np.exp(a)
plt.rcParams['legend.labelcolor'] = 'linecolor'

fig, ax = plt.subplots()
ax.plot(a, c, 'g', label='Model length')
ax.plot(c, a, 'r', label='Data length')

legend = ax.legend()

plt.show()

Figure_1

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@Carloscerq Carloscerq marked this pull request as draft April 27, 2021 21:03
@Carloscerq Carloscerq force-pushed the add_legend_labelcolor_rcParam branch from 64661db to 2216de0 Compare April 27, 2021 22:53
@Carloscerq Carloscerq force-pushed the add_legend_labelcolor_rcParam branch from 2216de0 to 9652ce5 Compare April 28, 2021 00:28
@Carloscerq Carloscerq marked this pull request as ready for review April 28, 2021 01:28
@Carloscerq Carloscerq marked this pull request as draft April 28, 2021 01:42
@Carloscerq Carloscerq force-pushed the add_legend_labelcolor_rcParam branch from 9652ce5 to 5fbf7cf Compare April 28, 2021 02:02
@Carloscerq Carloscerq marked this pull request as ready for review April 28, 2021 02:44
@tacaswell
Copy link
Member

Thanks for working on this!

Overall 👍, but is it possible to have the default rcParam be None? Currently the default label color will fall back to the rcParams['text.color'] but with this PR will default to 'black'

@tacaswell tacaswell added this to the v3.5.0 milestone Apr 28, 2021
@Carloscerq
Copy link
Contributor Author

Sure. I think that making a little change in the lib/matplotlib/legend.py it can work. I will give it a try.

@Carloscerq Carloscerq force-pushed the add_legend_labelcolor_rcParam branch from 5fbf7cf to 2dabca5 Compare April 28, 2021 23:18
@Carloscerq Carloscerq force-pushed the add_legend_labelcolor_rcParam branch 3 times, most recently from 7dc4eaa to a76663b Compare April 30, 2021 04:00
@Carloscerq Carloscerq force-pushed the add_legend_labelcolor_rcParam branch 2 times, most recently from 411cfac to 942c3b2 Compare April 30, 2021 16:54
@@ -544,8 +544,11 @@ def __init__(self, parent, handles, labels,
'mec': ['get_markeredgecolor', 'get_edgecolor'],
}
if labelcolor is None:
pass
elif isinstance(labelcolor, str) and labelcolor in color_getters:
if mpl.rcParams['legend.labelcolor'] != 'none':
Copy link
Member

Choose a reason for hiding this comment

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

The legend docstring should note that this rcparam exists....

Copy link
Member

Choose a reason for hiding this comment

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

Should this be `

Suggested change
if mpl.rcParams['legend.labelcolor'] != 'none':
if mpl.rcParams['legend.labelcolor'] is not None:

along with similar changes to validator? There are a number of places where we use the string 'none' as a color to mean "this thing does not get a color" and None (the singleton) to mean "use the default color".

That said, currently doing

...
ax.legend(labelcolor='none')

does not result in an invisible label which is a bug in the implementation below (cycle of a 0 length thing is also 0 length, not infinite).

Copy link
Member

@QuLogic QuLogic May 14, 2021

Choose a reason for hiding this comment

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

The rcParam can't be able to both be None and 'none', though, right? The fallback version would need a special value to work, like 'auto' or 'textcolor'?

Copy link
Member

Choose a reason for hiding this comment

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

#20225 fixes the bug I noted above!

@Carloscerq Carloscerq force-pushed the add_legend_labelcolor_rcParam branch 3 times, most recently from 5fb07c8 to 8240661 Compare May 11, 2021 13:02
@@ -179,6 +179,8 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
also be made to match the color of the line or marker using 'linecolor',
'markerfacecolor' (or 'mfc'), or 'markeredgecolor' (or 'mec').

Otherly, labelcolor can be set globally using :rc:`legend.labelcolor`.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry to be picky, bit this should go in as

labelcolor : str or list, default: :rc:`legend.labelcolor`.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review.
But when the rcParams[legend.labelcolor] is None(the default value(, should the docs say that it just uses the rcParams['text.color'] value?

Copy link
Member

Choose a reason for hiding this comment

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

I think that should be specified in the description of the parameter if it is not already

Copy link
Member

Choose a reason for hiding this comment

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

This is fine w/ me now. @tacaswell you have a block on this...

@Carloscerq Carloscerq force-pushed the add_legend_labelcolor_rcParam branch 2 times, most recently from 53bde8f to c905216 Compare May 14, 2021 01:37
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.

Looks on the right path, but the default value should be None not 'none'.

@@ -544,8 +544,11 @@ def __init__(self, parent, handles, labels,
'mec': ['get_markeredgecolor', 'get_edgecolor'],
}
if labelcolor is None:
pass
elif isinstance(labelcolor, str) and labelcolor in color_getters:
if mpl.rcParams['legend.labelcolor'] != 'none':
Copy link
Member

Choose a reason for hiding this comment

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

Should this be `

Suggested change
if mpl.rcParams['legend.labelcolor'] != 'none':
if mpl.rcParams['legend.labelcolor'] is not None:

along with similar changes to validator? There are a number of places where we use the string 'none' as a color to mean "this thing does not get a color" and None (the singleton) to mean "use the default color".

That said, currently doing

...
ax.legend(labelcolor='none')

does not result in an invisible label which is a bug in the implementation below (cycle of a 0 length thing is also 0 length, not infinite).

@Carloscerq Carloscerq force-pushed the add_legend_labelcolor_rcParam branch from c905216 to ffbd8b9 Compare May 14, 2021 16:43
@Carloscerq Carloscerq marked this pull request as draft May 16, 2021 17:52
@Carloscerq Carloscerq force-pushed the add_legend_labelcolor_rcParam branch from 49ac9a2 to 4397fa8 Compare May 16, 2021 17:58
@Carloscerq Carloscerq marked this pull request as ready for review May 16, 2021 19:07
@jklymak jklymak requested a review from tacaswell May 17, 2021 15:40
@Carloscerq Carloscerq force-pushed the add_legend_labelcolor_rcParam branch from 4397fa8 to 8354d0e Compare May 18, 2021 02:18
@Carloscerq Carloscerq force-pushed the add_legend_labelcolor_rcParam branch from 8354d0e to 13149be Compare May 20, 2021 02:37
@jklymak
Copy link
Member

jklymak commented May 22, 2021

@tacaswell you are still blocking on this....

@tacaswell tacaswell merged commit 9b9c42d into matplotlib:master May 22, 2021
@tacaswell
Copy link
Member

Thanks @Carloscerq ! Congratulations on your first merged Matplotlib PR 🎉 hope we hear from you again!

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

Successfully merging this pull request may close these issues.

4 participants