Chapter 4 Nfksksks
Chapter 4 Nfksksks
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H P L O T LY I N P Y T H O N
Alex Scriven
Data Scientist
What can custom buttons do?
direction : Bu on orientation
Bu ons can be beside ( left ) or on top of ( down ) each other
showactive : True / False to show the active (index of bu on) as pressed or not.
The active bu on is the currently selected one.
fig = px.bar(
data_frame=revenues,
x='Industry', y='Revenue',
color='Industry')
fig.show()
my_buttons = [
{'label': "Bar plot",
'method': "update",
'args': [{"type": "bar"}]},
{'label': "scatterplot",
'method': "update",
'args': [{"type": "scatter", 'mode': 'markers'}]}
]
dir(fig.layout)
dir(fig.data[0])
Some are familiar and some are worth noting for later!
fig.update_layout({
'updatemenus': [{'type': "buttons",
'direction': 'down',
'x': 1.3, 'y': 0.5,
'showactive': True,
'active': 0,
'buttons': my_buttons}]
})
fig.show()
Alex Scriven
Data Scientist
What is a dropdown?
fig = go.Figure()
for suburb in ['Ashfield', 'Lidcombe', 'Bondi Junction']:
df = syd_houses[syd_houses.Suburb == suburb]
fig.add_trace(go.Bar(x=df['Year'], y=df['Median House Price'], name=suburb))
fig.update_layout({
'updatemenus':[{
'type': "dropdown",
'x': 1.3,
'y': 0.5,
'showactive': True,
'active': 0,
'buttons': dropdown_buttons}]
})
fig.show()
Alex Scriven
Data Scientist
What are sliders?
fig.update_layout({
'yaxis': {'range': [0, 500000]},
'xaxis': {'range': [-100000, 2500000]}
})
fig['layout'].pop('updatemenus')
fig.show()
fig['layout']['sliders'][0].steps[0]['method']
animate
With plotly.express , you can't update data or layout — only animate the same data
point over di erent 'frames'.
fig = go.Figure()
for island in ['Torgersen', 'Biscoe', 'Dream']:
df = penguins[penguins.Island == island]
fig.add_trace(go.Scatter(
x=df["Culmen Length (mm)"],
y=df["Culmen Depth (mm)"], mode='markers', name=island))
sliders = [
{'steps':[
{'method': 'update', 'label': 'Torgersen',
'args': [{'visible': [True, False, False]}]},
{'method': 'update', 'label': 'Bisco',
'args': [{'visible': [False, True , False]}]},
{'method': 'update', 'label': 'Dream',
'args': [{'visible': [False, False, True]}]}
]}
]
fig.update_layout({'sliders': sliders})
fig.show()
fig.update_layout({'sliders': sliders})
fig.show()
Alex Scriven
Data Scientist
Chapter 1
Bivariate visualizations such as sca erplots Recall seeing company age (another
and bar plots variable) in the hover!
Annotations
Advanced customization
Subplots of same or di erent types
Bu ons
Dropdowns
Sliders