Lesson
Understanding the structure of Plotly graphs
Learn Understanding the structure of Plotly graphs in SQLPad's Data Science in Action course with practical examples and guided lessons.
In this lesson, we will learn about the structure of Plotly graphs and how to create basic graphs using built-in datasets from Plotly and Pandas.
Plotly Graph Structure
Plotly graphs are built using a hierarchical structure composed of three main components:
- Data: This is where we define the type of graph (scatter, bar, pie, etc.) and the data used to create it. Data is represented as a list of traces, where each trace is a dictionary containing the properties of a specific graph.
- Layout: This is where we define the appearance of the graph, such as axis labels, title, legend, and more. It is represented as a dictionary containing the properties of the graph's layout.
- Figure: This is the top-level container that combines the data and layout components to create the final graph. It is represented as a dictionary containing the data and layout properties.
Now, let's create a simple bar chart to understand the structure of Plotly graphs better.
Example - Bar Chart
We will use the following built-in dataset from Plotly to create a bar chart representing the number of tips received for different days of the week:
import plotly.express as px
data = px.data.tips()
print(data.head())
The dataset contains the following columns:
- total_bill: Total bill amount
- tip: Tip amount
- sex: Gender of the person paying the bill
- smoker: If the person is a smoker or not
- day: Day of the week
- time: Time of the day (Lunch or Dinner)
- size: Size of the party
Now, let's create a bar chart showing the total tips received for each day of the week.
import plotly.graph_objects as go
# Data
trace = go.Bar(
x=data['day'].value_counts().index,
y=data['day'].value_counts().values,
text=data['day'].value_counts().values,
textposition='auto'
)
# Layout
layout = go.Layout(
title="Total Tips by Day of the Week",
xaxis_title="Day of the Week",
yaxis_title="Number of Tips",
font=dict(size=14)
)
# Figure
fig = go.Figure(data=[trace], layout=layout)
fig.show()
In the code above, we first define the trace which is a bar chart with x-axis representing the days of the week and y-axis representing the number of tips. We also add labels to each bar using the text and textposition properties.
Next, we define the layout to set the title, axis labels, and font size.
Finally, we create a Figure object by combining the trace and layout, and use the show() method to display the graph.
Using Plotly Express
Plotly Express is a high-level interface for creating Plotly graphs. It provides a simpler syntax and automatically handles the creation of traces, layout, and figure.
Let's recreate the same bar chart using Plotly Express:
import plotly.express as px
fig = px.bar(data, x=data['day'].value_counts().index, y=data['day'].value_counts().values, text=data['day'].value_counts().values)
fig.update_layout(
title="Total Tips by Day of the Week",
xaxis_title="Day of the Week",
yaxis_title="Number of Tips",
font=dict(size=14)
)
fig.show()
As you can see, the code is shorter and more concise. Plotly Express automatically handles the creation of the trace and figure, and we only need to update the layout properties.
In the next lessons, we will explore more graph types and customization options using both Plotly Graph Objects and Plotly Express.
Exercises
1. Understanding the Structure of Plotly Graphs
Instruction
Create a bar chart using Plotly Express to display the total tips received for each day of the week using the data dataset. Set the title of the chart to 'Total Tips by Day of the Week', the x-axis label to 'Day of the Week', and the y-axis label to 'Number of Tips'. Set the font size to 14.
My Solution
# Your solution goes here
Hint
- Import Plotly Express as
px. - Use
px.bar()to create a bar chart with the x-axis asdata['day'].value_counts().indexand the y-axis asdata['day'].value_counts().values. Add thetextparameter asdata['day'].value_counts().values. - Use
fig.update_layout()to set the title, axis labels, and font size. - Call
fig.show()to display the chart.
Solution
import plotly.express as px
data = px.data.tips()
fig = px.bar(data, x=data['day'].value_counts().index, y=data['day'].value_counts().values, text=data['day'].value_counts().values)
fig.update_layout(
title="Total Tips by Day of the Week",
xaxis_title="Day of the Week",
yaxis_title="Number of Tips",
font=dict(size=14)
)
fig.show()