Add continuous color scale to strip, box, and violin plots
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 18.8k
- Forks
- 2.8k
- Avg merge
- 16h 26m
- Merged PRs (30d)
- 21
Description
Currently, 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)

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 with px.strip and the go.Box constructor, focusing on how strip, box, and violin plots currently handle categorical colors. Reproduce the iris example using sepal_width as the color feature, then trace how the coloraxis attribute and plot colorbar are assembled. Done means continuous coloring and a colorbar work for the included points without incorrectly applying point colors to the boxes or violins.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data-visualization
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100