plotly / plotly/plotly.py

constraining axes leads to paper coordinates being off and to extra space between plot area and annotations

Open
#4,872 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P2
Dominant language
Python
Stars
18.8k
Forks
2.8k
Avg merge
16h 26m
Merged PRs (30d)
21

Description

If I constrain the x-axis, a legend to the right of the plot area shows up too far to the right and paper coordinates x=0 and x=1 are no longer at the edges of the plot area. Paper coordinate x=1 should always be the right edge of the plot area. There should not be excessive trapped white space between the legend and the plot area. The constrained, no-width behavior should be similar to the unconstrained and constrained-with-width behaviors.

Here I reproduce the problem using the documentation example here:
https://plotly.com/python/axes/#fixed-ratio-axes

I'm happy to provide and diff the output JSON if that would be helpful

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(
    x = [0,1,1,0,0,1,1,2,2,3,3,2,2,3],
    y = [0,0,1,1,3,3,2,2,3,3,1,1,0,0],
    name="demo"
))

##Add some elements outside the main plot area
fig.update_layout(
        title=dict(text="Source:  Plotly Bug Report",
                   yref="container",
                   xref="paper",
                   xanchor="right",
                  x=1, y=0.005),
                  showlegend=True
)


#the current layout is reasonable -- the legend is close to the plot area and x=1 in paper coordinates is where we expect it.
fig.show()

#now we constrain the domain and it injects lots of space between the plot area and the legend
#paper coordinates no longer reference the plot area -- paper coordinate x=1 is well to the right of the plot area
fig.update_xaxes(
    range=[-1,4],  # sets the range of xaxis
    constrain="domain",  # meanwhile compresses the xaxis by decreasing its "domain"
)
fig.update_yaxes(
    scaleanchor = "x",
    scaleratio = 1
)

fig.show()

#now we specify the width and the layout becomes reasonable again

fig.update_layout(
    width = 800,
)

fig.show()

unconstrained graphic (expected behavior):
Image
constrained graphic (unexpected behavior)
Image
constrained graphic with width (expected behavior)
Image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the fixed-ratio axes example at the linked documentation page and run the three layout variants shown in the report. Compare the constrained no-width result with the unconstrained and explicit-width results; done means paper x=1 aligns with the plot area's right edge and no excessive space remains between the plot and legend or annotations.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.