Modify marker size in legend?

Hi there,

I have a quick and simple question which I couldn’t figure out myself.

Is there a way to modify how the legend displays the markers?
At the moment I am using scattergl with thousands of small points per trace…so I had to set the marker size small, which looks nice within the scatter plot but not in the legends where the markers colors are not distinguishable.

Similar thing: I wanted to color my points, using one color for the filling and one for the outline (the outline would indicate the point’s trace and is hence the only color I would like to display in the legend which displays the scatter traces. Does that work?

I would appreciate your help.


Not at the moment unfortunately.

I’m picking up that topic again, as one thing crossed my mind so I was playing around but no success yet.

Note that I am using the scattergl plot type.
I am wondering if there is a way to use the legendgroup behavior to accomplish different marker styles in my legend?
I tried creating my regular traces as scattergl plus additional “fake traces” in “box” type, which look nice in the legend, linked using the same legendgroup.
I thought by doing so, I could somehow use the box layout in the legend, while my scatter cointains the scattergl traces.
However, it always screws up my axes.

Am I missing something or is it just not possible?
Best regards,

That’s another gl2d limitation. You can’t currently mixed gl2d traces with regular cartesian traces (e.g. box trace). So yeah, that probably messed up the axes.

By the way @silly_lily, you might want to subscribe to for the latest info on our gl2d rewrite. We’re hoping to bring scattergl and gl2d axes to par with cartesian at some point this fall.


was there any improvements in that direction?


Still no way to set a legend marker size in Plotly… imagine a scatter plot with 5000 points…
either giant markers in the plot, or microscopic legend dots… Is there still no simple solution for this? I simple legend(markersize = x) argument sounds so simple to add.

p.s. i’m using plotly in R

1 Like

Hi !
Have you found something ?
I have the same issue in Python.

I have found a very ugly work around


ui <- fluidPage(
                   colourpicker::colourInput(inputId = 'markercolor', label = 'X',
           palette = "limited", 
           showColour = "background", returnName = TRUE),
           selectInput(inputId = 'traceNo', label = 'Trace', choices = c(1:3), selected = 1),
           actionButton(inputId = 'Switch', label = icon('refresh'), style = "color: #f7ad6e;   background-color: white;  border-color: #f7ad6e;
                        height: 40px; width: 40px; border-radius: 6px;  border-width: 2px; text-align: center;  line-height: 50%; padding: 0px; display:block; margin: 2px")

server <- function(input, output, session) {
  # values <- reactiveValues()

  observeEvent(input$Switch, { 
    plotlyProxy("plot1", session) %>%
      plotlyProxyInvoke("restyle", list(marker = list(color = input$markercolor)), list(as.numeric(input$traceNo)-1))

  output$plot1 <- renderPlotly({
    markersize <- 4
    markerlegendsize <- 20
   colors <- c('red', 'blue', 'black')
    p1 <- plot_ly()
    p1 <-  add_trace(p1, data = mtcars, x = ~disp, y = ~mpg, type = 'scatter', mode = 'markers', color = ~as.factor(cyl), colors = colors)
    p1 <- layout(p1, title = 'mtcars group by cyl with switching colors')
    p1 <- plotly_build(p1)

    ## this is a bit of a hack to change the size of the legend markers to not be equal to the plot marker size. 
    ## it makes a list of 1 size value for each marker in de trace in the plot, and another half of with sizes that are a lot bigger.
    ## the legend marker size is effectively the average size of all markers of a trace
    for(i in seq(1, length(sort(unique(mtcars$cyl) )))) { <- nrow(mtcars[which(mtcars$cyl  == sort(unique(mtcars$cyl))[i]), ])
      p1$x$data[[i]]$marker$size <- c(rep(markersize,, rep(c(-markersize+2*markerlegendsize),

shinyApp(ui, server)

Thank you! I’ll try this way.

any update ? this lack of capability is keeping our team from switching from bokeh to plotly :frowning:


I’ve run into this issue many times too. A fix would be fantastic.

Also had problem with scatters of small markers leading to tiny legend. I found workaround:

fig.update_layout(legend= {'itemsizing': 'constant'})

from here:

It makes the size of the legend independent of the marker size.
Solves the underlying problem many seem to have, but not the one OP asked about.


Made an account just to say thank you so much.

You saved me.

edit: idk if this is clear or not but anbjork’s solution works.

If you’re doing this in r just do layout(legend = list(itemsizing = “constant”))

1 Like