Is it possible to show or hide traces in animations?

I’m trying to make an animation with two traces, including a button that hides or shows the specific trace. The button works and the animation works, however, the button only works on a single frame. As soon as the animation activates it shows both traces again. Is it possible to hide or show the traces in all animation frames?

Example data and code:

# Example data
x <- seq(-2*pi, 2*pi, length.out = 100)
frame <- seq(0, pi/2, length.out = 10)

df <- merge(x = x, y = frame, by = NULL)
names(df)[2] <- "frame"
df$y1 <- sin(df$x + df$frame)
df$y2 <- cos(df$x + df$frame)

# Create the animated plot
p <- plot_ly(df, x = ~df$x) %>%
  add_trace(y = ~df$y1
            , frame = ~df$frame
            , name = "Sin"
            , type = 'scatter'
            , mode = 'lines'
  ) %>%
  add_trace(y = ~df$y2
            , name = "Cos"
            , frame = ~df$frame
            , type = 'scatter'
            , mode = 'lines'
  ) %>%
    yaxis = list(title = "Y"),
    xaxis = list(title = "X"),
    updatemenus = list(
        y = 0.7,
        buttons = list(
          list(method = "restyle",
               args = list("visible", list(TRUE, TRUE)),
               label = "Both"),
          list(method = "restyle",
               args = list("visible", list(TRUE, FALSE)),
               label = "Sin"),
          list(method = "restyle",
               args = list("visible", list(FALSE, TRUE)),
               label = "Cos")))


Any luck with this? I’m looking for a solution to a similar issue!

Any news? I want to do the same… :neutral_face:


This is a Python example that could suggest how to define such a R animation. Eventually access the corresponding R code here .

Not sure where this feedback should go, as this will be chart studio related, so just FYI:
There are several issues with the generated R code:

  • args = c(Null, list( needs to be: args = c(NULL, list(
  • layout = lis) should be layout = list()
  • Frames are not supported yet in R API. should be a comment


  • Specifying width/height in layout() is now deprecated.
    Please specify in ggplotly() or plot_ly()
  • No scatter mode specifed:
    Setting the mode to markers
    Read more about this attribute ->
  • A line object has been specified, but lines is not in the mode
    Adding lines to the mode…

can be fixed by using p <- plot_ly(width=layout$width, height=layout$height, type = "scatter", mode = "lines+markers") instead of p <- plot_ly()

However the result won’t work because unfortunately the R API currently doesn’t support “custom” animation sliders.

The R-code is automatically created and saved when an user uploads a figure to the Plotly cloud. I don’t use R Hence I cannot say what is wrong with the R version.

Sure, that’s what I guessed. Nevertheless, the feedback might be of interest for future readers and the plotly chart studio developers.