Render matplotlib path collections (violin plots, pcolor, stems, etc.) in plotly - #5702
Render matplotlib path collections (violin plots, pcolor, stems, etc.) in plotly#5702robertoffmoura wants to merge 1 commit into
Conversation
camdecoster
left a comment
There was a problem hiding this comment.
Thanks for the PR! Overall this is in good shape but needs a couple of changes. Could you also please add a changelog entry?
There was a problem hiding this comment.
Could you run the ruff formatter on this file?
| facecolors = mpltools.convert_rgba_array(props["styles"]["facecolor"]) | ||
| edgecolors = mpltools.convert_rgba_array(props["styles"]["edgecolor"]) | ||
| linewidths = mpltools.convert_linewidth_array(props["styles"]["linewidth"]) | ||
| alpha = props["styles"]["alpha"] |
There was a problem hiding this comment.
This adds the opacity twice. convert_rgba_array already adds the alpha value. We don't handle this consistently right now, but I think this should be removed to avoid making the plot too transparent.
| isinstance(c, str) for c in color | ||
| ): | ||
| return [_export_color(c) for c in color] | ||
| bgcolor = export_color(color) |
There was a problem hiding this comment.
Should this be _export_color? If this should be export_color, you'll need to import it from plotly/matplotlylib/mplexporter/utils.py.
| n = len(colors) | ||
| except TypeError: | ||
| return colors | ||
| return colors[min(i, n - 1)] if n else default |
There was a problem hiding this comment.
If i is greater than or equal to n, you'll get the same color for every call to per_path. To match the existing matplotlib behavior for the case of path length being greater than colors length, you could switch this logic a bit. This likely wouldn't happen much in practice, but this would cover us if it did come up.
| return colors[min(i, n - 1)] if n else default | |
| return colors[i % n] if n else default |
| linewidth = per_path(linewidths, i, 0) | ||
| self.plotly_fig.add_trace( | ||
| go.Scatter( | ||
| x=[v[0] for v in verts], |
There was a problem hiding this comment.
This line needs to handle the case where x values are dates. You can see how this is handled for the same situation on line 456. This is actually done in a few places. If you want to extract a helper function, this could be used in all those locations.
mpl_to_plotly silently drops several common plot types. Violin plots, pcolor, event plots, stack plots, fill_between and stem plots all produce PolyCollection/LineCollection artists in data coordinates, which the renderer skipped with "Dang! That path collection is out of this world...". The converted figure ends up with zero traces (except for stem, which gets partially drawn).
Fix: data-coordinate path collections are now drawn:
Before: plots converts to 0 traces (empty figures).
After: plots render correctly.
Snippet to reproduce: