Chapter 1
Chapter 1
Figure
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 is Plotly?
print(fig)
Type 'bar'
import plotly.graph_objects as go
figure_config = dict({ "data": [{"type": "bar",
"x": ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"],
"y": [28, 27, 25, 31, 32, 35, 36]}],
"layout": {"title": {"text": "Temperatures of the week",
"x": 0.5, "font": {'color': 'red', 'size': 15}}}})
fig = go.Figure(figure_config)
fig.show()
Alex Scriven
Data Scientist
Our approach
1. plotly.express
Specify a DataFrame and its columns as arguments
Bar chart
Histogram
Box plot
Density plots
import plotly.express as px
weekly_temps = pd.DataFrame({
'day': ['Monday', 'Tuesday',
'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday'],
'temp': [28, 27, 25, 31, 32, 35, 36]})
fig = px.bar(data_frame=weekly_temps, x='day', y='temp')
fig.show()
Histograms have:
fig = px.histogram(
data_frame=penguins,
x='Body Mass (g)',
nbins=10)
fig.show()
fig = px.box(data_frame=penguins,
y="Flipper Length (mm)")
fig.show()
Alex Scriven
Data Scientist
Customization in general
Can also express (basic) colors as strings such as 'red' , 'green' etc.
fig = px.bar(
data_frame=student_scores,
x='student_name', y='score',
title="Student Scores by Student",
color_discrete_map={
'Melbourne': 'rgb(0,0,128)',
'Sydney': 'rgb(235, 207, 52)'},
color='city')
fig = px.bar(data_frame=weekly_temps,
x='day', y='temp',
color='temp',
color_continuous_scale='inferno')
fig.show()