Data Visualization
Plotly
What is Plotly?
Plotly is a Python visualization library designed for interactive charts.
Unlike Matplotlib and Seaborn (which create static images), Plotly charts allow users to zoom, pan, hover, and interact with the data.
Python
# Install (if needed)
# !pip install plotly
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
plotly.express → high-level, quick chartsplotly.graph_objects → low-level, full controlBoth create interactive HTML charts that work in Jupyter and browsers.
Why Do We Need Interactive Charts?
Imagine you are showing bank management a chart of Age vs Account Balance.
With a static chart (Matplotlib/Seaborn):
- You cannot zoom into a specific age group
- You cannot hover to see exact values
- You cannot filter by subscription status easily
With Plotly:
- Hover to see exact customer details
- Zoom into interesting regions
- Pan and explore the data freely
- Double-click to reset the view
Interactive charts are especially useful for presentations, dashboards and exploratory data analysis.
Plotly Express vs Graph Objects
| Feature | plotly.express (px) | plotly.graph_objects (go) |
|---|---|---|
| Ease of use | Very easy (high-level) | More code required |
| Control | Limited customization | Full control |
| Analogy | Ordering biryani from a restaurant | Entering the kitchen and cooking yourself |
| Best for | Quick charts | Complex / customized charts |
Start with
plotly.express for most tasks. Use graph_objects only when you need fine control over every element of the chart.
Basic Bar Chart with Plotly Express
Python
import plotly.express as px
students = ["Arun", "Priya", "Rahul", "Sneha"]
marks = [70, 92, 65, 85]
fig = px.bar(
x=students,
y=marks,
title="Student Marks"
)
fig.show()
Graph – Interactive Bar Chart (static preview)
Hover over bars in the real Plotly chart to see exact values
fig.show() opens an interactive chart in the browser or notebook.You can hover on each bar to see the exact marks.
Interactive Scatter Plot
Python
fig = px.scatter(
bank,
x="age",
y="balance",
color="y",
title="Age vs Balance by Subscription",
hover_data=["job", "education"]
)
fig.show()
Graph – Scatter (preview)
color="y" automatically colors points by subscription status.hover_data shows extra columns (job, education) when you hover over a point.
Line Chart
Python
fig = px.line(
x=[1, 2, 3, 4, 5],
y=[10, 15, 13, 17, 20],
title="Sales Over Time",
markers=True
)
fig.show()
Graph – Line Chart
markers=True adds dots on each data point so the chart is easier to read.
Pie Chart
Python
fig = px.pie(
names=["Subscribed", "Not Subscribed"],
values=[5289, 39922],
title="Subscription Status"
)
fig.show()
Graph – Pie Chart
~12% subscribed, ~88% not subscribed
Histogram
Python
fig = px.histogram(
bank,
x="age",
color="y",
nbins=30,
title="Age Distribution by Subscription"
)
fig.show()
nbins=30 controls the number of bins (bars).color="y" stacks or groups the histogram by subscription status.
Box Plot
Python
fig = px.box(
bank,
x="job",
y="balance",
color="y",
title="Balance Distribution by Job"
)
fig.show()
Box plots are excellent for comparing the distribution of a numeric variable across categories (jobs) and groups (subscribed / not subscribed).
3D Scatter Plot
Plotly makes 3D visualizations easy and interactive.
Python
fig = px.scatter_3d(
bank,
x="age",
y="balance",
z="duration",
color="y",
title="Age vs Balance vs Duration"
)
fig.show()
You can rotate the 3D plot with the mouse to explore the data from different angles.
This is one of the strongest features of Plotly compared to Matplotlib/Seaborn.
This is one of the strongest features of Plotly compared to Matplotlib/Seaborn.
Customizing the Layout
Python
fig = px.bar(
x=["Arun", "Priya", "Rahul", "Sneha"],
y=[70, 92, 65, 85],
title="Student Marks"
)
fig.update_layout(
xaxis_title="Student Name",
yaxis_title="Marks",
title_font_size=20,
template="plotly_white"
)
fig.show()
update_layout() lets you change titles, axis labels, fonts, background theme and many other visual properties.
Saving and Sharing Charts
Python
# Save as interactive HTML file
fig.write_html("student_marks.html")
# Save as static image (requires kaleido)
# fig.write_image("student_marks.png")
The HTML file can be opened in any browser and remains fully interactive.
This is the easiest way to share Plotly charts with others.
This is the easiest way to share Plotly charts with others.
Plotly — Interactive Data Visualization
Create beautiful interactive charts that users can explore, zoom, and hover over.
Select any topic from the left sidebar to begin.
Start Learning →
