Repository navigation
Implement legend entries for surface (and others like heatmap, contour) traces #2642
Description
Activity
- changed the title
[-]Showlegend broken for 3D Ribbon and 3D Surface plots[/-][+]Implement legend entries for surface (and others like heatmap, contour) traces[/+]on May 17, 2018 Surface plots (and these ribbon plots are just specialized surface plots) are like heatmap and contour maps, they generally get colorscales (controlled by
showscale) rather than legend entries. We've never implemented legend items for any of these traces, so they're not broken, but you could make a case for us to add them. I guess in the special case of multiple single-color surfaces a legend entry could be useful to label them, and even colorscaled items could benefit from a legend entry to allow you to toggle them on and off.Reacted by aivision2020, nannau, zlang19 and Robin De Scheppereven colorscaled items could benefit from a legend entry to allow you to toggle them on and off
It would also enable the user to group such traces, such that their visibility can be toggled on and off along with associated traces, even if they don't show up in the legend themselves.
While the filling option for scatter3d is missing (#2352), surfaces are a viable substitute, but one would then need to use events to toggle their visibility when the visibility of their associated lines are toggled via the legend.
Reacted by Alex Johnson, Marco, nannau and zlang19but one would then need to use events to toggle their visibility when the visibility of their associated lines are toggled via the legend.
That could be done using (our recently added #2581) a custom legend handler.
I also request this feature to be added.
After hours of searching, I finally found a community post that explicitly stated that Mesh3d and Surface traces do not support legends: how to name axis and show legend in mesh3d. However, this is not described anywhere in the documentation for either Mesh3d or Surface (they both show that they support the showlegend property).
The use case is as described above regarding having multiple surfaces or mesh3d traces with a constant color such as when you want to use a "fill" on a scatter3d plot.
they both show that they support the showlegend property
Oof, sorry for the confusion! related: #3058
@alexcjohnson In the meantime, should I prune the legend-related attributes for traces that don't support them?
I would also love to have legend entries for Surfaces.
I am using Surfaces to show an image in a 3D plot (via dark wizardry), and I have several other traces in the plot. I would love to be able to only see the image and one of the traces, but if I double click on the legend entry of the trace I want to see, all other traces are disabled, including my image, and there is no way to turn it back on other than double click on the legend entry again, but that also re-enables all the other traces. Not sure if this makes as much sense to you as it does in my head!
Is there any hack or custom legend overlay that can be done until a proper implementation is found?
@giiyms For python, a modified version of this worked for me, though it requires a running python kernel so cannot be exported. Basically the idea is to create another trace that has a legend and link them with an event. I agree it would be nice to have a proper solution that doesn't require a running kernel.
import plotly.graph_objects as go import numpy as np x = np.linspace(-1, 1, 10) y = np.linspace(-1, 1, 10) z = np.sin(x[:, None]) * np.sin(y) f = go.FigureWidget([ go.Surface(x=x, y=y, z=z, name='c0', showscale=False), # Real data go.Scatter3d(x=[None], y=[None], z=[None], mode='markers', name='c1', showlegend=True), # Placeholder go.Surface(x=x, y=y, z=-z, name='c2', showscale=False), # More real data ]) surface = f.data[0] placeholder = f.data[1] f.layout.hovermode = 'closest' def update_visibility(trace, visible): surface.visible = visible placeholder.on_change(update_visibility, 'visible') f
I think all traces that can be plotted on shared subplots should honor
showlegend, including traces likeheatmap,surface,choroplethetc. For traces that only support continuous color, the marker in the legend could be a tiny colorbar gradient.This would actually provide a path forward for cases like #3468 !
Now with traces that support
showlegendand do not display it by default, the logic forshowlegenddefault is getting quite complicated. Any objection if we moveshowlegendattribute fromplotbase to eachtraceandcoerceit in trace defaults?Nevermind. I found a simple way to add them.
Any objection if we move showlegend attribute from plot base to each trace and coerce it in trace defaults?
you mean moving
Lines 1293 to 1299 in 8951847
if(Registry.traceIs(traceOut, 'showLegend')) { traceOut._dfltShowLegend = true; coerce('showlegend'); coerce('legendgroup'); } else { traceOut._dfltShowLegend = false; } to each trace default module? Could we simply make:
// in plots.supplyTraceDefaults var show = coerce('showlegend', Registry.traceIs(traceOut, 'showLegend')); if(show) coerce('legendgroup');
that is the
showLegendmodule category would now be used to set the traceshowlegenddefault? If the logic is more complicated than this, then yeah we can move theshowlegendlogic to the trace modules.
Moreover, for traces that currently have
showscale: trueby default, should they haveshowscale: falsewhenshowlegendis set totrue? That is, should we show both the legend item and colorscale or just one of them? If we choose to show both the legend item and colorscale then it might be a good idea to tweak the legend / colorscale positioning so that they don't overlap.If we choose to show both the legend item and colorscale then it might be a good idea to tweak the legend / colorscale positioning so that they don't overlap.
This would be a good idea globally if we could do it! If you have two scatter traces on top of a heatmap, say, right now the legend and colorbar overlap.
Hi,
as far as I understood imshow should now support categorical legends. I havent figured out how to implement that in python yet. Can someone pleas provide the according code snipplets? I want to plot bin (good, bad, ) information of semiconductor wafers...https://stackoverflow.com/questions/69168093/plotly-marker-size-relative-to-data-to-plot-interactive-wafer-map/69170425#69170425
Hi,
I'm using plotly.js 1.37.1 in an Angular 5 application and the showlegend attribute seems to be broken for 3D Ribbon and 3D Surface charts.
Changing the attribute and calling relayout has no effect on the chart.
The problem also appears to happen in any of the example charts in the documentation (for example this one from the documentation: Ribbon Plot example or this one: 3D Surface Plot example) Opening them in Codepen and changing the showlegend attribute has no effect on the legend.