Skip to content

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

@avant1

Description

@avant1

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3backlogbugsomething broken

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions