-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
68f91d5
Basic changes
CharlesHe16 dcd55aa
Basic changes
CharlesHe16 c611877
Basic changes
CharlesHe16 a87ffda
Removed abs value on parameter
CharlesHe16 049e6e6
Update lib/matplotlib/sankey.py
CharlesHe16 4fa79f1
Add test for callable
CharlesHe16 58d8f5c
Reduced size of long line
CharlesHe16 5852974
Add new line to EOF as per linting message.
CharlesHe16 c908a4f
Add new line to EOF as per linting message.
CharlesHe16 ba42251
Added entry in doc/users/next_whats_new/, as per checklist.
CharlesHe16 ba2d7f8
Added entry in doc/users/next_whats_new/, as per checklist.
CharlesHe16 35954a0
Cat emoji in example changed to '🐱', as previous Glyph '🐈' not includ…
CharlesHe16 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
doc/users/next_whats_new/callables_for_formatting_sankey_labels.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
Support callable for formatting of Sankey labels | ||
------------------------------------------------ | ||
|
||
The `format` parameter of `matplotlib.sankey.Sankey` can now accept callables. | ||
|
||
This allows the use of an arbitrary function to label flows, for example allowing | ||
the mapping of numbers to emoji. | ||
|
||
.. plot:: | ||
|
||
import matplotlib.pyplot as plt | ||
from matplotlib.sankey import Sankey | ||
import math | ||
|
||
|
||
def display_in_cats(values, min_cats, max_cats): | ||
def display_in_cat_scale(value): | ||
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 * '🐱') | ||
|
||
return display_in_cat_scale | ||
|
||
|
||
flows = [35, 15, 40, -20, -15, -5, -40, -10] | ||
orientations = [-1, 1, 0, 1, 1, 1, -1, -1] | ||
|
||
# Cats are good, we want a strictly positive number of them | ||
min_cats = 1 | ||
# More than four cats might be too much for some people | ||
max_cats = 4 | ||
|
||
cats_format = display_in_cats(flows, min_cats, max_cats) | ||
|
||
sankey = Sankey(flows=flows, orientations=orientations, format=cats_format, | ||
offset=.1, head_angle=180, shoulder=0, scale=.010) | ||
|
||
diagrams = sankey.finish() | ||
|
||
diagrams[0].texts[2].set_text('') | ||
|
||
plt.title(f'Sankey flows measured in cats \n' | ||
f'🐱 = {max(flows, key=abs) / max_cats}') | ||
|
||
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
