plotly / plotly/plotly.py

px.density_contour() ignores color labels for negative histfunc results

Open
#3,799 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

Consider the following example:

import plotly.express as px
import pandas as pd
import numpy as np
import plotly


np.random.seed(0)

n_samples = 1000
df = pd.DataFrame({
    'x': np.random.randint(low=1, high=5, size=n_samples),
    'y': np.random.randint(low=1, high=4, size=n_samples),
    'z': np.random.normal(loc=-40, scale=20, size=n_samples),
})

df['z'] = df['z'] * df['x']

# uncomment to make some values above 0
# df['z'] = df['z'] + 80

# uncomment to make all values above 0
# df['z'] = df['z'].abs()

fig = px.density_contour(df, x='x', y='y', z='z', histfunc='avg', height=800, width=1200, title=f'Plotly version: {plotly.__version__}')
fig.update_traces(contours_coloring="fill", contours_showlabels=True)
fig.show()

It calculates average of mostly negative values, which results in the following plot:
image

Color bar has correct color range, but all data points use same color - yellow, which is not particularly useful. Similar situation happens when only part of data is negative (uncomment first commented line in the example):

image
Left (positive) section looks fine, while right (negative) is colored with only one color.

There are workarounds, that involve changing of input data, but this makes plot reading much harder.

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 px.density_contour() example with negative and mixed histfunc results, then inspect the density_contour entry point and how contour coloring handles those values. Done means negative and mixed results use the full color range instead of collapsing to one color, while the existing positive case remains correct.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.