Skip to content

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

Description

@skolb2

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.

Activity

  1. 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
  2. alexcjohnson commented on May 17, 2018

    @alexcjohnson
    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.

  3. ChristofferHS commented on May 17, 2018

    @ChristofferHS

    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.

  4. etpinard commented on May 17, 2018

    @etpinard
    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.

  5. connorferster commented on Dec 18, 2018

    @connorferster

    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.

  6. alexcjohnson commented on Dec 18, 2018

    @alexcjohnson
    Collaborator

    they both show that they support the showlegend property

    Oof, sorry for the confusion! related: #3058

  7. antoinerg commented on Dec 18, 2018

    @antoinerg
    Contributor

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

  8. marcotama commented on May 1, 2019

    @marcotama

    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!

  9. giiyms commented on Jul 23, 2019

    @giiyms

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

  10. jordan-melendez commented on Sep 23, 2019

    @jordan-melendez

    @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
  11. nicolaskruchten commented on Sep 26, 2019

    @nicolaskruchten
    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 !

  12. added this to the v1.52.0 milestone on Oct 31, 2019
  13. self-assigned this
    on Nov 8, 2019
  14. archmoj commented on Nov 14, 2019

    @archmoj
    Contributor

    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?

  15. archmoj commented on Nov 14, 2019

    @archmoj
    Contributor

    Nevermind. I found a simple way to add them.

  16. etpinard commented on Nov 18, 2019

    @etpinard
    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.

  17. nicolaskruchten commented on Nov 18, 2019

    @nicolaskruchten
    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.

  18. HessTobias commented on Sep 15, 2021

    @HessTobias

    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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions