Skip to content

Instantly share code, notes, and snippets.

@eherrerosj
Created July 4, 2018 21:34
Show Gist options
  • Save eherrerosj/fa1bac2efd5b0714a65ce4470e1fbcb5 to your computer and use it in GitHub Desktop.
Save eherrerosj/fa1bac2efd5b0714a65ce4470e1fbcb5 to your computer and use it in GitHub Desktop.
Plot using dash. Scatter with Slider values
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go
from dash.dependencies import Input, Output
app = dash.Dash()
df = pd.read_csv(
'https://gist.githubusercontent.com/chriddyp/' +
'5d1ea79569ed194d432e56108a04d188/raw/' +
'a9f9e8076b837d541398e999dcbac2b2826a81f8/'+
'gdp-life-exp-2007.csv')
slider = dcc.Slider(id='slider', value=2, min=2, max=20, step=1,
marks={key: str(key) for key in range(20)})
app.layout = html.Div([dcc.Graph(id='life-exp-vs-gdp'),
slider])
@app.callback(Output('life-exp-vs-gdp', 'figure'),
[Input('slider', 'value')])
def make_figure(slider):
figure = {
'data': [
go.Scatter(
x=df[df['continent'] == i]['gdp per capita'],
y=df[df['continent'] == i]['life expectancy'],
text=df[df['continent'] == i]['country'],
mode='markers',
opacity=0.7,
marker={
'size': slider,
'line': {'width': 0.5, 'color': 'white'}
},
name=i
) for i in df.continent.unique()
],
'layout': go.Layout(
xaxis={'type': 'log', 'title': 'GDP Per Capita'},
yaxis={'title': 'Life Expectancy'},
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
legend={'x': 0, 'y': 1},
hovermode='closest'
)
}
return figure
if __name__ == '__main__':
app.run_server()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment