plotly / plotly/plotly.py

Order of marginal wrong when using facet_col and setting traces to invisible by default

Open
#4,438 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I am creating a histogram plot using plotly express.histogram, like the following:

df = pd.DataFrame({
    'month': [5] * 30 + [6] * 30 + [7] * 30,
    'value': np.random.randint(0, 100, size=90),
    'Module': ['M', 'P']*45,
})

fig = px.histogram(
    df,
    x='value',
    opacity=0.8,
    color='month',
    category_orders={'month': df['month'].sort_values(ascending=True)},
    marginal='box',
    barmode='overlay',
    facet_col='Module',
)

for trace in fig.data:
    if int(trace.name) not in (7,6):
        trace.visible = "legendonly"

fig.show()

When I then want to show month 5 as well, the box in marginal is placed at the top of the others, giving the wrong order of month: 5, 7, 6 (red from top to bottom).

The problem does not appear when I don't use the facet_col argument, or when i show all month from the beginning (simply comment the part where i set trace.visible to legendonly).

plotly version: 5.18.0

Screenshot 2023-11-22 153608

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 provided plotly.express.histogram example with plotly 5.18.0, comparing facet_col and marginal behavior with traces set to legendonly versus all traces visible. Trace how the marginal box order is produced and verify that revealing month 5 preserves the category order 5, 6, 7.

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.