3

I want to create a dynamic chart with Plotly in Python -> change the style options via buttons in the graph itself. Therefore, plotly offers buttons or drowdownmenus (updatemenus). My current problem is, that I would like to hide some of these buttons depending on which one is active.

My example is a plot where I can change my graph from lines into markers (first button set) and as special option for the lines I offer dotted, dashed and solid line (second button set). Obviously, offering the second button set when "Markers" is active doesn't make sense. So I would like to hide the second set. But until now I didn't find a solution. I am using the offline version of plotly.

I would appreciate any suggestions, thank you.

My mini example:

import plotly as py
import plotly.graph_objs as go

x_ = [1, 2, 3, 4, 5, 6, 7, 8 , 9, 10]
y_ = [1, 1.2, 1.4, 1.5, 1.6, 1.5, 1.4, 1.4, 1.35, 1.3]

trace_ = go.Scatter(
    x = x_,
    y = y_,
    mode = "lines",
    line = dict(
        dash = "dot"
    )
)

layout_ = go.Layout(
    autosize = True
)

updatemenus_ = list([
    dict(
        buttons = list([
            dict(
                args=['mode', 'lines'],
                label='Lines',
                method='restyle'
            ),
            dict(
                args=['mode', 'markers'],
                label='Markers',
                method='restyle'
            )
        ]),
        direction = 'left',
        pad = {'r': 10, 't': 10},
        showactive = True,
        type = 'buttons',
        x = 0.1,
        xanchor = 'right',
        y = 1.1,
        yanchor = 'top'
    ),
    dict(
        buttons = list([
            dict(
                args=['line', {'dash': 'dot'}],
                label='Dotted Line',
                method='restyle'
            ),
            dict(
                args=['line', {'dash': 'dash'}],
                label='Dashed Line',
                method='restyle'
            ),
            dict(
                args=['line', {'dash': 'solid'}],
                label='Solid Line',
                method='restyle'
            )
        ]),
        direction = 'right',
        pad = {'r': 10, 't': 10},
        showactive = True,
        type = 'buttons',
        x = 0.5,
        xanchor = 'right',
        y = 1.1,
        yanchor = 'top'
    ),
])

layout_['updatemenus'] = updatemenus_

fig = dict(data=[trace_], layout=layout_)

py.offline.plot(fig, filename='MiniExample.html')

1 Answer 1

0

If you want to update the visibility of the buttons, you need to rely on "Dash" module. However, in the current example, I suggest to simply use "dropdown" type to toggle between different visualization of the graph. A quick example based on your need:

x_ = [1, 2, 3, 4, 5, 6, 7, 8 , 9, 10]
y_ = [1, 1.2, 1.4, 1.5, 1.6, 1.5, 1.4, 1.4, 1.35, 1.3]

f = go.Scatter(
    x = x_,
    y = y_,
    mode = "markers",
    )

fig = go.Figure(f)

buttons = [{"method":'restyle', "args": [{"mode": "markers"}], "label": "Markers"},
           {"method": "restyle", "args":[{"mode": "lines",  "line.dash": "solid"}], "label": "solid_line"},
           {"method": "restyle", "args":[{"mode": "lines", "line.dash": "dot"}], "label": "dotted_line"}, 
           {"method": "restyle", "args":[{"mode": "lines", "line.dash": "dash"}], "label": "dashed_line"},
           ]


updatemenus = [{"type": "dropdown", "buttons":buttons, "x":0.12, "y": 1.2, "showactive":True}]
 
annotations=[dict(text="Graph format:", showarrow=False, x=1, y=1.3, yref="paper", align="right")]

fig.update_layout(updatemenus=updatemenus, annotations=annotations)
fig.show()

The outcome of the script I hope you find it helpful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.