Lesson
Line charts
Learn Line charts in SQLPad's Data Science in Action: Interactive Visualization with Plotly and Pandas course with practical examples and guided lessons.
Welcome to the lesson on Line charts in the course Data Science in Action: Interactive Visualization with Plotly and Pandas. In this lesson, we will explore how to create simple and interactive line charts using Plotly, a popular data visualization library in Python. Line charts are useful for visualizing changes in data over time or any other continuous variable. By the end of this lesson, you will be able to create, customize, and interpret line charts in Plotly to enhance your data analysis and reporting capabilities.
Creating a Basic Line Chart
In this lesson, we'll learn how to create a basic line chart using the Plotly library in Python. We'll use a built-in dataset from the Plotly library for this example.
Creating the Data
First, let's create a Pandas DataFrame using the built-in Plotly dataset stock. We will then display the first few rows of the DataFrame using the head() method.
import plotly.express as px
import pandas as pd
data = px.data.stocks()
df = pd.DataFrame(data)
print(df.head())
Creating the Line Chart
Now that we have our data in a DataFrame, let's create a basic line chart using the plotly.graph_objects module. We'll plot the date on the x-axis and Apple's AAPL stock price on the y-axis.
import plotly.graph_objects as go
fig = go.Figure(go.Scatter(x=df['date'], y=df['AAPL'], mode='lines+markers'))
fig.show()
Now you have created a basic line chart using Plotly and the built-in dataset. You can try modifying the code to explore different stocks and customize the appearance of the chart.
Customizing Line Style
Here is the second code example for the lesson on Line Charts, focusing on customizing line styles.
First, let's create a pandas dataframe using the built-in dataset from plotly called iris:
import plotly.express as px
import pandas as pd
df = px.data.stocks()
df.head()
Now, let's create a line chart with custom line styles using the plotly.graph_objects module:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['date'], y=df['AAPL'],
mode='lines+markers',
name='Apple',
line=dict(color='firebrick', width=2, dash='dot')))
fig.add_trace(go.Scatter(x=df['date'], y=df['GOOG'],
mode='lines+markers',
name='Google',
line=dict(color='royalblue', width=2, dash='dashdot')))
fig.update_layout(title='Custom Line Styles',
xaxis_title='Date',
yaxis_title='Price',
legend_title='Apple V.S. Google')
fig.show()
In this example, we use the plotly.graph_objects module to create a custom line chart with two traces (Sepal Length and Petal Length). We customize the line color, width, and dash style using the line parameter within the go.Scatter trace. Finally, we update the layout to add a title, axis titles, and a legend title.
Adding Legends and Annotations
In this code example, we will learn how to add legends and annotations to a line chart using Plotly and Pandas. We will use the built-in dataset Iris from Plotly.
Data Preparation
First, let's import the necessary libraries and load the Iris dataset. We will then display the first 5 rows of the dataset using df.head().
import plotly.express as px
import pandas as pd
# Load Iris dataset
df = px.data.iris()
# Display first 5 rows of the dataset
df.head()
Creating Line Chart with Legends and Annotations
Now, let's create a line chart using the Iris dataset. We will add legends and annotations to the chart using Plotly's update_layout() function.
# Create line chart
fig = px.line(df, x='species_id', y='sepal_width', color='species')
# Add legends and annotations
fig.update_layout(
title='Iris Dataset: Sepal Width by Species',
xaxis_title='Species ID',
yaxis_title='Sepal Width (cm)',
legend_title='Species',
annotations=[
dict(
x=1,
y=df[df['species_id']==1]['sepal_width'].mean(),
xref='x',
yref='y',
text='Mean Sepal Width for Setosa',
showarrow=True,
font=dict(
family='Courier New, monospace',
size=14,
color='#ffffff'
),
align='center',
arrowhead=2,
arrowsize=1,
arrowwidth=2,
arrowcolor='#636363',
ax=20,
ay=-30,
bgcolor='#ff7f0e',
bordercolor='#c7c7c7',
borderwidth=2,
borderpad=4,
opacity=0.8
)
]
)
# Display the chart
fig.show()
In this example, we created a line chart using the Iris dataset and added legends and annotations to enhance the chart's readability. Users can easily modify the code to add more legends and annotations as needed.
Exercises
1. Line Chart Exercise: Visualizing Iris Dataset
Instruction
Create a line chart to visualize the relationship between petal length and petal width for different species of iris flowers. Customize the appearance of the chart by using dashed lines and markers for each data point. Set the title and axis labels accordingly.
My Solution
# Your solution goes here
Hint
- Load the iris dataset using
px.data.iris(). - Create a
go.Figureobject. - Iterate through each unique species in the dataset and add a
Linetrace for each one using theadd_tracemethod. - Set the
modeattribute of theLinetrace to'lines+markers'. - Set the
lineattribute to a dictionary with adashkey set to'dash'. - Set the
markerattribute to a dictionary withsymbolandsizekeys. - Set the title and axis labels using the
update_layoutmethod. - Display the figure using the
showmethod.
Solution
import plotly.graph_objects as go
import plotly.express as px
iris = px.data.iris()
# Make sure the data is sorted by the x-axis
iris = iris.sort_values(by='petal_width')
fig = go.Figure()
for species in iris.species.unique():
species_data = iris[iris.species == species]
fig.add_trace(go.Scatter(x=species_data['petal_width'],
y=species_data['petal_length'],
mode='lines+markers',
name=species,
line=dict(dash='dash'),
marker=dict(symbol='circle', size=8)))
fig.update_layout(title='Petal Length vs Petal Width for Iris Species',
xaxis_title='Petal Width',
yaxis_title='Petal Length')
fig.show()