Plotly update nested argument via updatemenus - TagMerge
2Plotly update nested argument via updatemenusPlotly update nested argument via updatemenus

Plotly update nested argument via updatemenus

Asked 5 months ago
2
2 answers

There is probably more than one way to do this, but this works. For layout updates, you're usually going to use the method relayout. You need to provide all of the information, as well (i.e., x, y, xref, etc.).

library(plotly)

img1 <- "https://www.gravatar.com/avatar/dc04a25fbabbffc1f1b7c6a3362bfae4?s=256&d=identicon&r=PG"
img2 <- "https://www.gravatar.com/avatar/bc680ed8d2555accacebc0fe2d8c9691?s=256&d=identicon&r=PG"

imgOne = list( # create image expectations
  list(
    source =  img1,
    xref = "x", yref = "y",
    x = 3, y = 4,
    sizex = 2, sizey = 2))

imgTwo = list( # create image expectations
  list(
    source =  img2,
    xref = "x", yref = "y",
    x = 3, y = 4,
    sizex = 2, sizey = 2))

plot_ly(x = 1:5, y = 1:5, type = 'scatter', mode = 'markers') %>% 
  layout(
    images = imgOne,
    updatemenus = list(
      list(
        y = 1,
        active = 0,
        buttons = list(
          list(label = "None",
               method = "relayout",
               args = list(list(images = c()))),
          list(label = "img1",
               method = "relayout",
               args = list(list(images = imgOne))),
          list(label = "img2",
               method = "relayout",
               args = list(list(images = imgTwo)))
        ))))

There are a few method options for updatemenus. You can read more about that here.

enter image description here

Source: link

0

fig = dict({
    "data": [{"type": "bar",
              "x": [1, 2, 3],
              "y": [1, 3, 2]}],
    "layout": {"title": {"text": "A Figure Specified By Python Dictionary"}}
})

# To display the figure defined by this dict, use the low-level plotly.io.show function
import plotly.io as pio

pio.show(fig)
import plotly.graph_objects as go

fig = go.Figure(
    data=[go.Bar(x=[1, 2, 3], y=[1, 3, 2])],
    layout=go.Layout(
        title=go.layout.Title(text="A Figure Specified By A Graph Object")
    )
)

fig.show()
import plotly.graph_objects as go

dict_of_fig = dict({
    "data": [{"type": "bar",
              "x": [1, 2, 3],
              "y": [1, 3, 2]}],
    "layout": {"title": {"text": "A Figure Specified By A Graph Object With A Dictionary"}}
})

fig = go.Figure(dict_of_fig)

fig.show()
import plotly.graph_objects as go

fig = go.Figure(
    data=[go.Bar(x=[1, 2, 3], y=[1, 3, 2])],
    layout=go.Layout(height=600, width=800)
)

fig.layout.template = None # to slim down the output

print("Dictionary Representation of A Graph Object:\n\n" + str(fig.to_dict()))
print("\n\n")
print("JSON Representation of A Graph Object:\n\n" + str(fig.to_json()))
print("\n\n")
Dictionary Representation of A Graph Object:

{'data': [{'x': [1, 2, 3], 'y': [1, 3, 2], 'type': 'bar'}], 'layout': {'height': 600, 'width': 800}}



JSON Representation of A Graph Object:

{"data":[{"x":[1,2,3],"y":[1,3,2],"type":"bar"}],"layout":{"height":600,"width":800}}

Source: link

Recent Questions on r

    Programming Languages