Data Visualization

Plotly

01 · What is Plotly? File · Plotly · 1 of 12

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 charts
plotly.graph_objects → low-level, full control
Both create interactive HTML charts that work in Jupyter and browsers.
02 · Why Interactive Charts? File · Plotly · 2 of 12

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.
03 · Express vs Graph Objects File · Plotly · 3 of 12

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.
04 · Basic Bar Chart File · Plotly · 4 of 12

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)
Student Marks Arun Priya Rahul Sneha 70 92 65 85
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.
05 · Scatter Plot File · Plotly · 5 of 12

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)
Age vs Balance by Subscription age balance no yes
color="y" automatically colors points by subscription status.
hover_data shows extra columns (job, education) when you hover over a point.
06 · Line Chart File · Plotly · 6 of 12

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
Sales Over Time
markers=True adds dots on each data point so the chart is easier to read.
07 · Pie Chart File · Plotly · 7 of 12

Pie Chart

Python
fig = px.pie(
    names=["Subscribed", "Not Subscribed"],
    values=[5289, 39922],
    title="Subscription Status"
)
fig.show()
Graph – Pie Chart
Subscription Status Not Sub Sub
~12% subscribed, ~88% not subscribed
08 · Histogram File · Plotly · 8 of 12

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.
09 · Box Plot File · Plotly · 9 of 12

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).
10 · 3D Scatter File · Plotly · 10 of 12

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.
11 · Updating Layout File · Plotly · 11 of 12

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.
12 · Saving & Sharing File · Plotly · 12 of 12

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.
Home Plotly

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 →