-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Support callable for formatting of Sankey labels #19187
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This definitely needs a test. Wording of the docstring could be made more explicit.
lib/matplotlib/sankey.py
Outdated
if isinstance(self.format, str): | ||
quantity = self.format % abs(number) + self.unit | ||
elif callable(self.format): | ||
quantity = self.format(100. * abs(number)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm skeptical you want to multiply by 100 here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry. First PR and I derped.
I think what happened was that I was confused or missed the difference between committing to local branch and pushing to master branch on my fork. What you saw was an "early version" and not the best indicator of quality, not that the final version is complex or deep.
Thanks for catching the error.
Can you elaborate or provide an example?
Do you have suggestions for the docstring? For reference, the matplotlib/lib/matplotlib/axes/_axes.py Lines 2942 to 2946 in 6305e8d
|
Please start here, and in particular the section under "testing". I assume there are other sankey tests you could likely add to. https://matplotlib.org/devel/index.html |
Co-authored-by: Jody Klymak <[email protected]>
Thanks, I have reviewed the guide and testing in general. I have searched the indicated directory of tests, and found this 12 line file for testing matplotlib/lib/matplotlib/tests/test_sankey.py Lines 1 to 12 in 099c173
My current guess for satisfying your testing suggestion, is that I will append code to I'll go ahead and do this in a few days or so. |
Ok, wow. I pressed a push button in my IDE, and the whole testing suite started firing in my browser window. Seems like magic. Anyways, I know how to test for new functionality in my local, offline branch, but I don't know how to "test" my test against Matplotlib's Github testing suites. So I just uploaded my test as I understand it. |
That's the correct thing to do. π |
RIP kitty cat title. @timhoffm, I have a few questions: Finishing the PR Checklist:There's a number of tasks in the "PR Checklist". I have completed a few, but others such as documentation changes are incomplete. Should I go ahead and try to do these? Or is the normal practice to leave these to approved maintainers (perhaps to benefit from batching and writing uniformity in the docs)? Replicating functionality to other functions:There's been mention of a need for adding/unifying format functionality in other functions, see @ianhi #19133 (comment):
Should I try to work on these other cases, replicating my work here? |
PR checklist:Almost all items are checked (or not relevant in the context of the PR). The only thing that could be added is:
Replicating functionality to other functions:I'd first see that this PR gets merged so that we have proven consensus on the concept. The other functions can be fixed in a followup PR. You are welcome to contribute that as well. |
Ok, I added an entry as indicated.
Emoji fixed, example should work. |
β¦ed in Matplotlib's standard font DejaVu Sans, breaking doc building.
max_value = max(values, key=abs) | ||
number_cats_to_show = \ | ||
max(min_cats, math.floor(abs(value) / max_value * max_cats)) | ||
return str(number_cats_to_show * 'π±') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least for me the way GH renders this I don't see the cat emoji, but it is in there. This renders as https://51382-1385122-gh.circle-artifacts.com/0/doc/build/html/users/next_whats_new/callables_for_formatting_sankey_labels.html
Thanks @CharlesHe16 and congratulations on your first Matplotlib PR π Hopefully we will hear from you again! |
PR Summary
This PR is in regards to #19133:
This slightly extends
sankey
by allowing theformat
parameter to accept a callable (more specifically, a callable that accepts a numerical value for each flow, and outputs a string).Example:
Comment 1:
This change is modelled after
pie
autopct
, and pretty much copies and pastes this code from that class:matplotlib/lib/matplotlib/axes/_axes.py
Lines 3133 to 3139 in 6305e8d
Note that @timhoffm's suggestion in issue 19133 was that there was two templates to model this PR on:
fmt
orautopct
If I understand correctly, in the first, the
fmt
scheme used for thecontour
class, the additional functionality seems extremely large. The label seems to have its own class, coming in at ~600 lines. See:matplotlib/lib/matplotlib/contour.py
Line 48 in 99e6240
I choose the simpler
autopct
because it's far more feasible for me and this seems to match the intention of the PR.Comment 2:
In the current
sankey
class, when theformat
parameter is explicitly set toNone
, this produces an error.This is different than the functionality in the two examples previously mentioned:
fmt
orautopct
In these examples, when the respective parameter is set to
None
,contour
seems to revert to a defaultScalarFormatter
object, andpie
removes labels altogether.I left the current behavior of
sankey
, instead of changing it to the two choices above.PR Checklist
pytest
passes).flake8
on changed files to check).flake8-docstrings
and runflake8 --docstring-convention=all
).doc/users/next_whats_new/
(follow instructions in README.rst there).doc/api/next_api_changes/
(follow instructions in README.rst there).