-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
traces selection/update methods and "magic underscore" support #1534
Merged
Conversation
This file contains 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
E.g. self.layout.update({'updatemenus[1].buttons[2].method': 'restyle'})
The input `spec` was removed from the grid_ref to make it easier to compare traces with the grid subplots
This was referenced Apr 23, 2019
OMG this is such an exciting PR!! |
@jonmmease this is awesome. Thanks for reviving my old concept of magic underscore notation. I’ve loved having it in my Julia library and seeing it here makes me 😁😁 Good work! |
Merged! |
This was referenced May 3, 2019
2 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #1481 and the trace portion of #1484.
Overview
This PR introduces 3 new figure methods to make it easier to make updates to traces that have already been added to a Figure (e.g. a Figure from
plotly_express
or a figure factory). It also introduces a convenient shorthand for updating nested properties that we've been calling "magic underscore" support.New Figure methods
All 3 methods allow the user to select certain traces from the figure using a "selector". This is an
update
style dict that can be used to specify the desired values of arbitrary properties in the trace. For example, a selector of{'type': 'scatter'}
will select all scatter traces in the figure. A selector of{'type': 'scatter', 'mode': 'markers'}
will select only those scatter traces that are in'markers'
mode.In addition to the selector, for figures created using
plotly.subplots.make_subplots
, the trace row and column index may also be used as selection criteria.The lowest level method is
select_traces
. This returns a generator that iterates over all of the traces in the figure that satisfy the selection criteria.The
for_each_trace
method applies a custom function to each trace produced byselect_traces
The
update_traces
method accepts a "patch" to update all of the traces that satisfy the selection criteria. This "patch" is a dict that is passed totrace.update
for all selected traces."Magic underscore" support
For background on "magic underscore" support, see #919 and #1481.
The goal of magic underscore support is to make it more convenient to reference nested figure properties without the need to created deeply nested
dict
instances.Here are the updates that have been implemented:
Figure and graph_objs constructors
Here's a current approach to creating a figure with a particular title:
With magic-underscore support:
.update operations
Here's a current approach to updating the title text for an existing figure
with magic-underscore support:
In addition to using underscores to separate nested keys, when the update argument is supplied as a dict (rather than as kwargs), the path parts can be separated by periods.
add trace methods
Here's a current approach creating a scatter trace with green markers
With magic underscore support
compatibility
One tricky thing to keep track of is that there are a few properties in plotly.js that contain underscores in their proper name. e.g.
scatter.error_y
andlayout.paper_bgcolor
. These are kept track of as special cases.Here's an example of using magic underscore logic alongside the
error_y
property:Note how integer data array indices can be specified directly in the argument.
All together
Combining
update_traces
and magic-underscore support should make it much easier to selectively update trace properties.For example, to update the line width of all scatter traces in line mode:
Or, only the line traces in subplot position 2, 1