Rana Maninder Singh
Lesson 11 of 12Data Analysis with Python

Plotly

Lesson opener

In one line: Plotly Express turns a DataFrame into an HTML chart you can hover — 770 sales rows, ₹68,021.85 revenue, 14 months from 2022-02 to 2023-03 — without standing up a server.

Why it matters: A PNG is a snapshot. A VP who wants "what happened in 2022-04" needs tooltips. fig.write_html is that file. The numbers in this lesson are from the bundled data/sales.csv, not a made-up series.

Before this lesson: Seaborn. Pins: pandas==2.3.3. Plotly is used only in the tagged block that writes HTML.

Practitioner focus: First prove the aggregate in pandas. Only then draw it. If the printed 68,021.85 is wrong, the chart is wrong.

What you will learn

By the end of this lesson you will be able to:

  1. Load data/sales.csv and print 770 rows and 68,021.85 total revenue.
  2. Group revenue by month and print the first three months.
  3. Count channels (web 261, email 260, mobile 249).
  4. Call px.line on the monthly table (HTML write is tagged; open it locally).
  5. Pick line vs bar vs box for month, channel, and spread.
  6. Refuse to chart before the printed totals match.

Worked example: totals first, picture second

Step 1 — The file contract.

import pandas as pd

sales = pd.read_csv("data/sales.csv")
print(len(sales))
print(round(float(sales["revenue"].sum()), 2))
print(sales["month"].min(), sales["month"].max())

Expected output:

770
68021.85
2022-02 2023-03

Step 2 — Monthly revenue, first three months.

import pandas as pd

sales = pd.read_csv("data/sales.csv")
monthly = (
    sales.groupby("month", as_index=False)["revenue"]
    .sum()
    .sort_values("month")
)
print(len(monthly))
print(monthly.head(3).round(2).to_string(index=False))

Expected output:

14
  month  revenue
2022-02  1318.83
2022-03  2687.46
2022-04  3808.99

Step 3 — Channel mix.

import pandas as pd

sales = pd.read_csv("data/sales.csv")
print(sales["channel"].value_counts().sort_index().to_string())

Expected output:

channel
email     260
mobile    249
web       261

Step 4 — Draw the monthly line. This writes HTML. Run it on your machine and open the file. The test runner skips it (needs a browser to inspect hover).

# requires: browser
import pandas as pd
import plotly.express as px

sales = pd.read_csv("data/sales.csv")
monthly = (
    sales.groupby("month", as_index=False)["revenue"]
    .sum()
    .sort_values("month")
)
fig = px.line(monthly, x="month", y="revenue", title="Monthly revenue")
fig.write_html("outputs/ds12_line.html")

Line = one series over time. Bar = compare channels. Box = spread of order revenue. Do not start with 3D.

Pause and think

Q1. Why print 68,021.85 before px.line?

If the sum is wrong, hover is a pretty lie. The table is the contract.

Q2. Why 14 months, not 12?

The file runs 2022-02 through 2023-03. A "year" chart would hide two extra months.

Q3. Does the VP need a server to hover?

No. write_html is a standalone file.

Guided practice: 2022-04 revenue

Print April 2022's monthly total. You already saw 3808.99 in Step 2.

import pandas as pd

sales = pd.read_csv("data/sales.csv")
monthly = sales.groupby("month")["revenue"].sum()
print(round(float(monthly.loc["2022-04"]), 2))

Expected output:

3808.99

Independent practice: Electronics revenue

Print total revenue where product_category == "Electronics".

import pandas as pd

sales = pd.read_csv("data/sales.csv")
print(round(float(sales.loc[sales["product_category"] == "Electronics", "revenue"].sum()), 2))

Expected output:

14928.42

Common mistake

The deliberate mistake

Symptom: a beautiful line that does not add up to 68,021.85.

Diagnosis: you charted a filtered slice (one channel) and titled it "all revenue."

Correction: print sum on the same DataFrame you pass to px.line.

Prevention: the title states the filter. No filter, say "all 770 orders."

Where this connects

What you can now do:

  • Prove 770 / 68,021.85 / 14 months in pandas.
  • Hand a hoverable monthly line to someone who will not open a notebook.

Next lesson: NLP basics. Charts for numbers. Text is the next column type.