-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Exclude totally empty subplots #2855
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 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
17658da
Added selector argument and changed _subplot_has_no_traces
nicholas-esterer bc1e78a
Test finding empty subplots for all selector combinations
nicholas-esterer e4fe7cf
Assert that we indeed test all the selector combinations
nicholas-esterer eb21739
Anything truthy passed to exclude_empty_subplots works
nicholas-esterer 39c1cbd
changelog
nicholas-esterer 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
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
61 changes: 61 additions & 0 deletions
61
packages/python/plotly/plotly/tests/test_core/test_subplots/test_find_nonempty_subplots.py
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,61 @@ | ||
import pytest | ||
import plotly.graph_objects as go | ||
from plotly.subplots import make_subplots | ||
from itertools import combinations, product | ||
from functools import reduce | ||
|
||
|
||
def all_combos(it): | ||
return list( | ||
reduce( | ||
lambda a, b: a + b, | ||
[list(combinations(it, r)) for r in range(1, len(it))], | ||
[], | ||
) | ||
) | ||
|
||
|
||
def translate_layout_keys(t): | ||
xr, yr = t | ||
xr = xr.replace("axis", "") | ||
yr = yr.replace("axis", "") | ||
return (xr, yr) | ||
|
||
|
||
def get_non_empty_subplots(fig, selector): | ||
gr = fig._validate_get_grid_ref() | ||
nrows = len(gr) | ||
ncols = len(gr[0]) | ||
sp_addresses = product(range(nrows), range(ncols)) | ||
# assign a number similar to plotly's xref/yref (e.g, xref=x2) to each | ||
# subplot address (xref=x -> 1, but xref=x3 -> 3) | ||
# sp_ax_numbers=range(1,len(sp_addresses)+1) | ||
# Get those subplot numbers which contain something | ||
ret = list( | ||
filter( | ||
lambda sp: fig._subplot_not_empty( | ||
*translate_layout_keys(sp.layout_keys), selector=selector | ||
), | ||
[gr[r][c][0] for r, c in sp_addresses], | ||
) | ||
) | ||
return ret | ||
|
||
|
||
def test_choose_correct_non_empty_subplots(): | ||
# This checks to see that the correct subplots are selected for all | ||
# combinations of selectors | ||
fig = make_subplots(2, 2) | ||
fig.add_trace(go.Scatter(x=[1, 2], y=[3, 4]), row=1, col=1) | ||
fig.add_shape(dict(type="rect", x0=1, x1=2, y0=3, y1=4), row=1, col=2) | ||
fig.add_annotation(dict(text="A", x=1, y=2), row=2, col=1) | ||
fig.add_layout_image( | ||
dict(source="test", x=1, y=2, sizex=0.5, sizey=0.5), row=2, col=2 | ||
) | ||
all_subplots = get_non_empty_subplots(fig, "all") | ||
selectors = all_combos(["traces", "shapes", "annotations", "images"]) | ||
subplot_combos = all_combos(all_subplots) | ||
assert len(selectors) == len(subplot_combos) | ||
for s, spc in zip(selectors, subplot_combos): | ||
sps = tuple(get_non_empty_subplots(fig, s)) | ||
assert sps == spc |
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.
does this line mean that one could do
fig.add_shape( ... , row="all", col="all", exclude_empty_subplots=["traces", "shapes"])
?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.
to be clear: i don't think that's necessary, I would just do
True
/False
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.
You could but by default True means "all". I also don't think it's necessary but you get this functionality for free, all that needs to happen is the list of objects to check is made into the keyword argument of the function (and coerced to "all" if it is True). But I can disable that if you want and have it just accept True/False.
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.
Yeah, let's just accept truthy/falsey for now. I think it's easier to not support certain exotic use-cases that then some people depend upon later :)