plotly / plotly/plotly.py

color_continuous_scale causes a bug when the last item is not 1.0

Open
#4,267 0 comments 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

import plotly.express as px
import numpy as np

df = px.data.gapminder().query("year == 2007")
fig = px.treemap(df, path=[px.Constant("world"), 'continent', 'country'], values='pop',
                  color='lifeExp', hover_data=['iso_alpha'],
                  color_continuous_scale=[(0, 'red'), (0.4, 'yellow'), (1.0, 'green')],
                  color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))
fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
fig.show()

This gives me the result I want.

However, something like:


                  color_continuous_scale=[(0, 'red'), (0.4, 'yellow'), (0.8, 'green')],

creates a whole separate treemap.

This fixes the issue:


                  color_continuous_scale=[(0, 'red'), (0.4, 'yellow'), (0.8, 'green')], (1.0, 'green')],

I'm not sure if there's anything to even technically "fix" here, but it was expected behaviour that I didn't get.

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 treemap reproduction and inspect how color_continuous_scale is handled for treemaps. Confirm the behavior when the final scale position is below 1.0, then add a regression test showing that it does not create a separate treemap and preserves the expected coloring.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.