Add continuous color scale to strip, box, and violin plots
See original GitHub issueCurrently, strip/box/violin plots only work with categorical/discrete colormaps. This is sufficient for the violins and boxes but for the points that can be included in all three plots, it would be useful to color by a continuous feature and to add a colorbar to the plot. I looked around a bit and it seems like the go.Box constructor needs to have the coloraxis attrbute added to it (and I suspect something would need to be added to avoid coloring the boxes/violins in the same colors as the points, since they are colored the same for categorical variables).
The example below illustrates the issue by trying to color by the sepal_width variable:
import plotly.express as px
iris = px.data.iris()
px.strip(iris, 'species', 'sepal_length', 'sepal_width', stripmode='overlay')
The resulting plot is not meaningful since it colors a continuous variable with a discrete colormap.
So far, I have not found a prettier workaround that this (which is imperfect since it uses the legend instead of a colorbar):
import matplotlib as mpl
sw = iris['sepal_width'].sort_values()
sw_01 = (sw - sw.min()) / (sw.max() - sw.min())
sw_colors = {n: mpl.colors.rgb2hex(c) for n, c in zip(sw, mpl.cm.viridis(sw_01))}
px.strip(iris, 'species', 'sepal_length', 'sepal_width',
stripmode='overlay', category_orders={'sepal_width': sw.to_list()[::-1]},
color_discrete_map=sw_colors)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:14 (4 by maintainers)
Top GitHub Comments
Hello! The same is for me. I’m trying to create a new feature importance visualization and I will need this. Hoper there will be a fix soon. Thanks
Same! It’s a shame.