plotly / plotly/dash

Editable legend in dcc.Graph compresses the plot

Open
#2,613 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P3
Dominant language
Python
Stars
24.4k
Forks
2.3k
Avg merge
2d 7h
Merged PRs (30d)
13

Description

The bug happens in Dash using the config options of the dcc.Graph component to make the legend content editable.

pip list:

dash==2.11.1
dash_bootstrap_components==1.4.1
plotly==5.14.1

More exactly, it happens when the "edits":{"titleText:True} config option is set. This allows to modify the legend content, but it also seems to compress the plot, even if the legend does not take more space.
I believe it is due to the font size being bigger in editing mode when clicking on a legend entry.
Here is an example, where two curves are added to a graph on a button click:

from dash import Dash, html, dcc, Input, Output, State, no_update
import plotly.graph_objects as go

figure = go.Figure()
figure.add_scatter(
    x=[1,2,3], y=[1,2,3], 
    name="<b>result.id</b>: 5408<br><b>result.name</b>: OpticalPower")
figure.add_scatter(
    x=[1,2,3], y=[3,2,1], 
    name="<b>result.id</b>: 5414<br><b>result.name</b>: OpticalPower")

app = Dash(__name__)


app.layout = html.Div([
    html.Button("Toggle curves", id="toggle_btn"),
    dcc.Graph(
        id="graph", 
        figure=figure,
        config={"edits":{"legendText":True}} # Removing this line corrects the bug
        )
], style={"width":"50%"})

@app.callback(Output("graph", "figure"),
              Input("toggle_btn", "n_clicks"),
              State("graph", "figure"))
def toggle_curves(n_clicks, fig):
    if not n_clicks:
        return no_update
    fig = go.Figure(fig)
    if n_clicks % 2 == 0:
        fig.data = fig.data[:2]
    else:
        fig.add_scatter(x=[1,2,3], y=[2,2,2], name="Polyfit Deg 5 - Result 5408")
        fig.add_scatter(x=[1,2,3], y=[3,3,3], name="Polyfit Deg 5 - Result 5414")
    return fig

if __name__ == "__main__":
    app.run(port=8054, debug=True)

Here is what happens when we click on the button to add the two extra curves:
plotlybug

This does not happen when we set "edits":{"titleText:False} in the config options of the graph.

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 by running the supplied Dash example with dcc.Graph and the config option enabling editable legend text, then inspect the graph rendering path used for legend editing. Done means adding curves no longer compresses the plot when legend editing is enabled, while the existing editable-legend behavior remains intact and a regression test covers the example.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.