Advertisement
selasley

Dash Reset Axes Range

Jul 12th, 2019
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. import dash
  2. import dash_core_components as dcc
  3. import dash_html_components as html
  4. from dash.dependencies import Input, Output
  5. import plotly.graph_objs as go
  6. import math
  7.  
  8. app = dash.Dash(__name__)
  9. server = app.server
  10. app.title = 'resetScale2d Test'
  11.  
  12.  
  13. _config = {'modeBarButtons': [['resetScale2d']]}
  14.  
  15.  
  16. def make_fig(plot_type='linear', x=range(21), y=range(1, 211, 10)):
  17.     xvals = list(x)
  18.     yvals = list(y)
  19.     xrange = [0, 50]
  20.     yrange = [1, 500]
  21.     if plot_type == 'log':
  22.         yrange = [math.log10(yr) for yr in yrange]
  23.     print(plot_type, xrange, yrange)
  24.     traces = [go.Scatter(x=xvals, y=yvals, marker={'size': 8}, name='Tens')]
  25.  
  26.     layout = go.Layout(
  27.         xaxis=dict(range=xrange),
  28.         yaxis=dict(type=plot_type,
  29.                    range=yrange),
  30.         # uirevision=plot_type
  31.     )
  32.     fig = go.Figure(data=traces, layout=layout)
  33.     return fig
  34.  
  35.  
  36. app.layout = html.Div(
  37.     [
  38.         dcc.Dropdown(
  39.             id='linlog',
  40.             options=[
  41.                 {'label': 'Linear Plot', 'value': 'linear'},
  42.                 {'label': 'Log Plot', 'value': 'log'},
  43.             ],
  44.             value='linear',
  45.         ),
  46.         html.Div(
  47.             [dcc.Graph(id='linlogplot', figure=make_fig(), config=_config)],
  48.             style={'width': '50%', 'margin': '0 auto'},
  49.         ),
  50.     ]
  51. )
  52.  
  53.  
  54. @app.callback(Output('linlogplot', 'figure'), [Input('linlog', 'value')])
  55. def change_type(plot_type):
  56.     return make_fig(plot_type=plot_type)
  57.  
  58.  
  59. if __name__ == '__main__':
  60.     app.run_server(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement