-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
color_continuous_scale incompatible with marginal distributions in density_heatmap #4377
Comments
Can confirm. Here's simple example to see difference: from plotly import express as px
import pandas as pd
df = pd.DataFrame({"x": [1, 2], "y": [3,4]})
# works
px.density_heatmap(df)
# works
px.density_heatmap(df, color_continuous_scale="Viridis")
# works
px.density_heatmap(df, marginal_y="histogram", marginal_x="histogram")
# raises error
px.density_heatmap(df, marginal_y="histogram", marginal_x="histogram", color_continuous_scale="Viridis") |
I can confirm the same error message.
It's as if it's trying to apply to the color scale to the marginal histogram as well... I'll mark this as a bug for now, and we'll see what our engineers think once they have more time to take a deeper look. |
To understand what happens we have to study the "anatomy" of this fig:
Let us inspect:
and
What we see? The values in the |
I don't think that the intention here was to make the marginal histogram of the colorscale you selected for the entire heatmap, but rather to just add a marginal histogram, in addition to main focus - the heatmap. I was going to try a workaround with converting Viridis to 2d list, but i see that it might not happen so easily... |
I explained how this kind of px. density_heatmap is designed. You can change the default settings performing trace updates ( for the color of the marginal histograms), respectively, layout update: |
Example that still works: # custom color scale definition
cscale = [
[0, '#FFFF66'],
[0.05, '#FFFF66'],
[0.7, '#ff0000'],
[1, '#ff0000']
]
# no error
px.density_heatmap(df, marginal_y="histogram", marginal_x="histogram", color_continuous_scale=cscale) What is strange to me is that I can set a custom colorscale, and it still works. And histogram won't try to be colorful in this case. Judging by the fact that this issue is brought up only now (I have some vague memory of this same error from a few months ago) - people are not using these 2 features together. I appreciate workaround you provided, but I don't see OP intention to apply colorscale to marginal histogram, nor do I want it - it's simply that using colorscale by name for heatmap with marginal histogram (regardless of colors of the marginal histogram) is not working is surprising and the resulting error is poorly communicated. I suggest letting the marginal histogram ignore the |
This code snippet from ChatGPT3.5 is not raising error: import numpy as np
import plotly.colors as pc
# Sample values (from 0 to 1)
values = np.linspace(0, 1, 10) # Replace with your values
# Get the Viridis colorscale
viridis_colorscale = pc.sequential.Viridis
# Map values to colors based on the Viridis colorscale
colors_for_values = [[value, viridis_colorscale[int(value * (len(viridis_colorscale) - 1))]] for value in values]
# does not raise error
px.density_heatmap(df, marginal_y="histogram", marginal_x="histogram", color_continuous_scale=colors_for_values) |
The solution given by ChatGPT is a workaround that redefines an existing colorscale to give access to its first color. You can avoid this redefinition, as I said in the previous post, by updates:
|
I see the issue doesn't progress... Could my suggestion be implemented?
|
Hi - we are tidying up stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for a while, I'm going to close it; if you'd like to submit a PR, we'd be happy to prioritize a review, and if it's a request for tech support, please post in our community forum. Thank you - @gvwilson |
I was trying out the examples from https://plotly.com/python/2D-Histogram/ and wanted to use the density heat map with marginal distribution on x and y but wanted to change the colour map to align with some previous work.
I found when combining the two subsequent examples from the 2D-Histogram page I got an error. If the marginal distribution and color_continuous_scale aren't meant to be used together there should at least be a better error message.
I btw also test other kinds of distributions like box, violin and rug with the same result.
plotly Version: 5.17.0
My code:
Traceback:
The text was updated successfully, but these errors were encountered: