Skip to content

Disable autosize on double click #149

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 2 commits into from
Oct 22, 2019

Conversation

harupy
Copy link
Contributor

@harupy harupy commented Oct 20, 2019

Doc upgrade checklist:

  • random seed is set if using random data
  • file has been moved from unconverted/x/y.md to x/y.md
  • old boilerplate at top and bottom of file has been removed
  • Every example is independently runnable and is optimized for short line count
  • no more plot() or iplot()
  • graph_objs has been renamed to graph_objects
  • fig = <something> call is high up in each example
  • minimal creation of intermediate trace objects
  • liberal use of add_trace and update_layout
  • fig.show() at the end of each example
  • px example at the top if appropriate
  • minimize usage of hex codes for colors in favour of those in https://github.com/plotly/plotly.py-docs/issues/14

https://plot.ly/python/images/#zoom-on-static-images

This PR proposes to disable autosize on double click and use only reset because autosize adds margins around the image.

Current (example on the doc)

demo-reset

Proposed (tested on Jupyter)

proposed

@emmanuelle
Copy link
Contributor

Thanks for your pull request! Could you please add a comment (in the same code cell) explaining that this is for disabling autosize because of unwanted margins, and on another comment line link to https://plot.ly/python/configuration-options/? Then I'll merge :-)

@harupy
Copy link
Contributor Author

harupy commented Oct 21, 2019

@emmanuelle Thanks for the comment! I updated the code.

@harupy
Copy link
Contributor Author

harupy commented Oct 21, 2019

If we don't use the autosize, the dummy scatter trace can be omitted.

@emmanuelle
Copy link
Contributor

Thank you very much! Merging 💃 :-)

@emmanuelle emmanuelle merged commit 3f71269 into plotly:master Oct 22, 2019
@harupy
Copy link
Contributor Author

harupy commented Oct 22, 2019

@emmanuelle Thanks for merging!

What do you think about this?

If we don't use the autosize, the dummy scatter trace can be omitted.

I think it should be removed because it's no longer necessary.

@nicolaskruchten
Copy link
Contributor

It seems like this example will be much nicer/easier when the image trace type comes out :) no more “dummy” traces

@harupy
Copy link
Contributor Author

harupy commented Oct 22, 2019

@nicolaskruchten That'd be great! When will it come out?

I actually used this feature on this PR (mlflow/mlflow#1934) but it was a bit of pain to fill out all the layout options.

@harupy
Copy link
Contributor Author

harupy commented Oct 22, 2019

btw the dummy trace can be removed. I tested the following code on Binder and it worked.

import plotly.graph_objects as go

# Create figure
fig = go.Figure()

# Constants
img_width = 1600
img_height = 900
scale_factor = 0.5

- Add invisible scatter trace.
- This trace is added to help the autoresize logic work.
- fig.add_trace(
-     go.Scatter(
-         x=[0, img_width * scale_factor],
-         y=[0, img_height * scale_factor],
-         mode="markers",
-         marker_opacity=0
-     )
- )

# Configure axes
fig.update_xaxes(
    visible=False,
    range=[0, img_width * scale_factor]
)

fig.update_yaxes(
    visible=False,
    range=[0, img_height * scale_factor],
    # the scaleanchor attribute ensures that the aspect ratio stays constant
    scaleanchor="x"
)

# Add image
fig.update_layout(
    images=[go.layout.Image(
        x=0,
        sizex=img_width * scale_factor,
        y=img_height * scale_factor,
        sizey=img_height * scale_factor,
        xref="x",
        yref="y",
        opacity=1.0,
        layer="below",
        sizing="stretch",
        source="https://raw.githubusercontent.com/michaelbabyn/plot_data/master/bridge.jpg")]
)

# Configure other layout
fig.update_layout(
    width=img_width * scale_factor,
    height=img_height * scale_factor,
    margin={"l": 0, "r": 0, "t": 0, "b": 0},
)

# Disable the autosize on double click because it adds unwanted margins around the image
# More detail: https://plot.ly/python/configuration-options/
fig.show(config={'doubleClick': 'reset'})

@harupy harupy deleted the disable-autosize-on-doubleclick branch October 23, 2019 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants