px.density_contour() ignores color labels for negative histfunc results
Nobody has claimed this yet.
- 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:

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):

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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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