Skip to content
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

Implement legend entries for surface (and others like heatmap, contour) traces #2642

Closed
skolb2 opened this issue May 17, 2018 · 15 comments · Fixed by #4386
Closed

Implement legend entries for surface (and others like heatmap, contour) traces #2642

skolb2 opened this issue May 17, 2018 · 15 comments · Fixed by #4386
Assignees
Labels
feature something new
Milestone

Comments

@skolb2
Copy link

skolb2 commented May 17, 2018

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.

@alexcjohnson alexcjohnson changed the title Showlegend broken for 3D Ribbon and 3D Surface plots Implement legend entries for surface (and others like heatmap, contour) traces May 17, 2018
@alexcjohnson
Copy link
Collaborator

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.

@alexcjohnson alexcjohnson added the feature something new label May 17, 2018
@ChristofferHS
Copy link

even 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.

@etpinard
Copy link
Contributor

but 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.

@connorferster
Copy link

connorferster commented Dec 18, 2018

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.

@alexcjohnson
Copy link
Collaborator

they both show that they support the showlegend property

Oof, sorry for the confusion! related: #3058

@antoinerg
Copy link
Contributor

antoinerg commented Dec 18, 2018

@alexcjohnson In the meantime, should I prune the legend-related attributes for traces that don't support them?

@marcotama
Copy link

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!

@giiyms
Copy link

giiyms commented Jul 23, 2019

Is there any hack or custom legend overlay that can be done until a proper implementation is found?

@jordan-melendez
Copy link

@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

@nicolaskruchten
Copy link
Contributor

I think all traces that can be plotted on shared subplots should honor showlegend, including traces like heatmap, surface, choropleth etc. 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 !

@nicolaskruchten nicolaskruchten added this to the v1.52.0 milestone Oct 31, 2019
@archmoj archmoj self-assigned this Nov 8, 2019
@archmoj
Copy link
Contributor

archmoj commented Nov 14, 2019

Now with traces that support showlegend and do not display it by default, the logic for showlegend default is getting quite complicated. Any objection if we move showlegend attribute from plot base to each trace and coerce it in trace defaults?

@archmoj
Copy link
Contributor

archmoj commented Nov 14, 2019

Nevermind. I found a simple way to add them.

@etpinard
Copy link
Contributor

Any objection if we move showlegend attribute from plot base to each trace and coerce it in trace defaults?

you mean moving

plotly.js/src/plots/plots.js

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 showLegend module category would now be used to set the trace showlegend default? If the logic is more complicated than this, then yeah we can move the showlegend logic to the trace modules.


Moreover, for traces that currently have showscale: true by default, should they have showscale: false when showlegend is set to true? 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.

@nicolaskruchten
Copy link
Contributor

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.

@HessTobias
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature something new
Projects
None yet
Development

Successfully merging a pull request may close this issue.