Skip to content

Instantly share code, notes, and snippets.

@naarkhoo
Created December 17, 2019 08:48
Show Gist options
  • Save naarkhoo/99789982e8195529de759acec12f1d17 to your computer and use it in GitHub Desktop.
Save naarkhoo/99789982e8195529de759acec12f1d17 to your computer and use it in GitHub Desktop.
Dash plotly - interactive histogram with fitted line on top with a slider
import plotly.graph_objs as go
import plotly.figure_factory as ff
import numpy as np
data = [30.2049, 51.3693, 51.8094, 30.5573, 56.8151, 45.9166, 56.2858, 55.4125, 72.8778,
39.7996, 34.8203, 36.8362, 39.9959, 54.7995, 52.5158, 40.6841, 61.3939, 48.5818,
41.4944, 71.5607, 49.0913, 57.7985, 70.2787, 45.2226, 55.7449, 69.0475, 33.9624,
51.3384, 53.1948, 46.1682, 66.5202, 37.0265, 48.0182, 44.2164, 56.9336, 69.7961,
48.6728, 44.5923, 33.2164, 41.8337,56.7741, 35.6030, 33.9237, 41.5178, 50.4453,
29.0570, 33.0124, 58.6281, 39.2005, 42.9119]
hist_data = [data]
group_labels = ['distplot']
f = ff.create_distplot(hist_data, group_labels, bin_size=5, show_rug=False)
fig = go.FigureWidget(f) # you can replace FigureWidget with Figure alternatively
fig.data[0].marker.line = dict( color = 'black',width = 2)
fig.data[1].line.color = 'red'
fig.layout.sliders = [dict(
active = 4,
currentvalue = {"prefix": "bin size: "},
pad = {"t": 20},
steps = [dict(label = i, method = 'restyle', args = ['xbins.size', i]) for i in range(1,20)]
)]
fig
Distplot_with_slider
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment