plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

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

Open avant1 opened this issue 3 years ago • 0 comments

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.

avant1 avatar Jun 29 '22 11:06 avant1