Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable and disable the logarithmic scale as a viewer in Plotly?

I am recently exploring Plotly and I wonder if there is a way for sharing a plot and let the viewer switch between a logarithmic axis and linear axis.

Any suggestion?

like image 588
Stefano Avatar asked Feb 25 '19 18:02

Stefano


People also ask

How do I remove a Modebar in Plotly?

Preventing the Modebar from Appearing If you would like the modebar to never be visible, then set the displayModeBar attribute in the config of your figure to false.

How do I turn off legend in Plotly?

Syntax: For legend: fig. update_traces(showlegend=False)

What is Add_trace in Plotly?

Adding Traces To Subplots If a figure was created using plotly. subplots. make_subplots() , then supplying the row and col arguments to add_trace() can be used to add a trace to a particular subplot. In [12]: from plotly.subplots import make_subplots fig = make_subplots(rows=1, cols=2) fig.

What is Plotly graph_objects in Python?

The plotly. graph_objects module (typically imported as go ) contains an automatically-generated hierarchy of Python classes which represent non-leaf nodes in this figure schema. The term "graph objects" refers to instances of these classes. The primary classes defined in the plotly.


1 Answers

Plotly has a dropdown feature which allows the user to dynamically update the plot styling and/or the traces being displayed. Below is a minimal working example of a plot where the user can switch between a logarithmic and linear scale.

import plotly
import plotly.graph_objs as go


x = [1, 2, 3]
y = [1000, 10000, 100000]
y2 = [5000, 10000, 90000]

trace1 = go.Bar(x=x, y=y, name='trace1')
trace2 = go.Bar(x=x, y=y2, name='trace2', visible=False)


data = [trace1, trace2]

updatemenus = list([
    dict(active=1,
         buttons=list([
            dict(label='Log Scale',
                 method='update',
                 args=[{'visible': [True, True]},
                       {'title': 'Log scale',
                        'yaxis': {'type': 'log'}}]),
            dict(label='Linear Scale',
                 method='update',
                 args=[{'visible': [True, False]},
                       {'title': 'Linear scale',
                        'yaxis': {'type': 'linear'}}])
            ]),
        )
    ])

layout = dict(updatemenus=updatemenus, title='Linear scale')
fig = go.Figure(data=data, layout=layout)

plotly.offline.iplot(fig)

I added two traces to the data list to show how traces can also be added or removed from a plot. This can be controlled by the visible list in updatemenus for each button.

like image 127
daronjp Avatar answered Oct 24 '22 01:10

daronjp