Day 33 of 50 Days of Python: Interactive Visualisations with Plotly
Part of Week 5: Data Analysis and Visualisation
Welcome to Day 33! We are closing in on the end of week 5. Hope you’re enjoying it so far. Let’s get down to business by exploring Plotly, a powerful Python library that enables the creation of interactive and visually appealing data visualisations. Building upon our previous work with Seaborn, Plotly offers enhanced interactivity, making it ideal for dynamic data exploration.
Python Setup
By now you know the drill, but this is what you’ll need package wise to get through and utilise Plotly:
pip install plotly pandas numpy
What is Plotly?
Plotly is an open-source Python library that allows for the creation of interactive charts and dashboards. It supports a wide range of chart types, including statistical, scientific, financial, and geographical visualisations. It has features such as zooming, panning, and tooltips, make it a valuable tool for data analysis and presentation. Which makes it a prime candidate for delivering exciting metrics to the higher-ups.
Interactive Line Chart: Stock Prices Over Time
Line charts are ideal for visualising trends over time. Let's simulate stock price data and create an interactive line chart:
import plotly.express as px
import pandas as pd
import numpy as np
# Simulate stock price data
np.random.seed(42)
dates = pd.date_range(start='2022-01-01', periods=100)
prices = np.cumsum(np.random.randn(100)) + 100 # Simulated stock prices
df = pd.DataFrame({'Date': dates, 'Price': prices})
# Create interactive line chart
fig = px.line(df, x='Date', y='Price', title='Simulated Stock Prices Over Time')
fig.update_traces(line_color='blue')
fig.show()
Explanation of key components:
px.line(): Creates a line chart with interactive features.
update_traces(): Customises the line colour.
fig.show(): Displays the interactive chart.
Interactive Bar Chart: Sales by Product Category
Bar charts are effective for comparing categorical data. Let's visualise sales across different product categories.
# Sample sales data
categories = ['Electronics', 'Furniture', 'Clothing', 'Books']
sales = [15000, 12000, 10000, 8000]
df = pd.DataFrame({'Category': categories, 'Sales': sales})
# Create interactive bar chart
fig = px.bar(df, x='Category', y='Sales', title='Sales by Product Category', color='Category')
fig.update_layout(showlegend=False)
fig.show()
Explanation:
px.bar(): Creates a bar chart with interactive tooltips.
color='Category': Assigns different colours to each category.
update_layout(): Hides the legend for clarity.
Interactive Scatter Plot: Exam Scores vs. Study Hours
Scatter plots are useful for identifying relationships between two variables. Let's simulate data for study hours and exam scores.
# Simulate data
np.random.seed(42)
study_hours = np.random.uniform(1, 10, 50)
exam_scores = study_hours * 10 + np.random.normal(0, 5, 50)
df = pd.DataFrame({'Study Hours': study_hours, 'Exam Score': exam_scores})
# Create interactive scatter plot
fig = px.scatter(df, x='Study Hours', y='Exam Score', title='Exam Scores vs. Study Hours',
trendline='ols', color='Study Hours')
fig.show()
Explanation:
px.scatter(): Creates a scatter plot with interactive features.
trendline='ols': Adds a linear regression trendline.
color='Study Hours': Colours points based on study hours.
Interactive Heatmap: Correlation Matrix
Heatmaps are excellent for visualising correlations between variables. Let's compute and display a correlation matrix.
# Sample dataset
df = px.data.iris()
# Compute correlation matrix
corr_matrix = df.corr()
# Create interactive heatmap
fig = px.imshow(corr_matrix, text_auto=True, title='Correlation Matrix Heatmap')
fig.show()
Explanation:
df.corr(): Computes the correlation matrix.
px.imshow(): Creates a heatmap with annotations.
text_auto=True: Automatically displays correlation coefficients.
Customising Visualisations
Plotly allows extensive customisation to enhance the aesthetics and functionality of your charts.
Changing Themes
fig.update_layout(template='plotly_dark')
Applies a dark theme to the chart.
Adding Annotations
fig.add_annotation(x=5, y=50, text="Important Point", showarrow=True, arrowhead=1)
Adds an annotation with an arrow pointing to a specific data point.
Exporting Charts
fig.write_html("chart.html")
Saves the interactive chart as an HTML file for sharing or embedding.
Next Up: Day 34: Working with Jupyter Notebooks and Markdown for Reporting.
In Day 34, we'll explore how to leverage Jupyter Notebooks and Markdown to create clear, structured, and shareable data reports. By combining code, narrative text, and visualisations, you can produce comprehensive documents that are both informative and easy on the eye.
So join me for Day 34 and until then… Happy coding.
There may be a small typo with the "colour" argument in the bar plots section, it should read "color".
Nonetheless, I dream for the day Substack supports plotly natively