Lesson 40 · Pandas Full Course
Make Charts Straight from Pandas with .plot() (Line, Bar, Hist, Scatter) | Pandas Course #40
In this lesson you'll be able to: Turn a Series or DataFrame into a chart in one line Choose the chart with kind=: line, bar, hist, box, scatter, pie Add…
- CoursePandas Full Course
- Lesson40 of 65
- FormatJupyter notebook · 40 code cells
- Data2 datasets
What you'll learn
Datasets used in this lesson
Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.
📓 Full notebook
Download .ipynbPandas 40: Quick Visualizations using Pandas plot()#
In this lesson you'll be able to:
- Turn a Series or DataFrame into a chart in one line
- Choose the chart with kind=: line, bar, hist, box, scatter, pie
- Add titles, labels, size and colour
- Plot several columns, side by side or in panels
- Combine charts in one figure and save them as files Data: New York taxi trips and restaurant tips
Warm-up: trips per hour, on the right clock#
Lesson 39 attached the New York time zone to every pickup.
import pandas as pd
taxis = pd.read_csv("data/pandas/taxis.csv",
parse_dates=["pickup", "dropoff"])
ny = taxis["pickup"].dt.tz_localize("America/New_York")
by_hour = taxis.groupby(ny.dt.hour).size()
by_hour.head(4)
Your first chart in one line#
Problem: 24 numbers are hard to read. A picture isn't.
import matplotlib.pyplot as plt
by_hour.plot()
plt.show()
Quick check: which hour is quietest? Read it off the chart.
print(by_hour.idxmin(), by_hour.min())
print(by_hour.idxmax(), by_hour.max())
by_hour.plot(title="Taxi pickups by hour, March 2019",
xlabel="Hour of day (New York)",
ylabel="Trips", figsize=(8, 4))
plt.show()
Common mistake: plotting a text column
taxis["payment"].plot()
pay = taxis["payment"].value_counts()
print(pay)
pay.plot(kind="bar", title="How taxi riders pay", rot=0)
plt.show()
by_hour.plot(marker="o", grid=True, xticks=range(0, 24, 3),
title="Taxi pickups by hour", xlabel="Hour",
ylabel="Trips", figsize=(8, 4))
plt.show()
print(by_hour.loc[[7, 8, 9]])
Your turn#
Plot taxi trips per weekday as a bar chart, Monday first.
week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"]
per_day = ny.dt.day_name().value_counts().reindex(week)
per_day.plot(kind="bar", rot=45, title="Taxi trips by weekday",
ylabel="Trips")
plt.show()
print(per_day.idxmax(), per_day.max())
Pick the chart with kind=#
Problem: different questions need different chart shapes.
- line: trends | bar, barh: compare groups
- hist, box: spread of values | scatter: two numbers together
- pie, area, hexbin: shares, totals, dense scatters
tips = pd.read_csv("data/pandas/tips.csv")
print(tips.shape)
tips.head(3)
tips["total_bill"].plot(kind="hist", bins=20,
title="Restaurant bills",
xlabel="Total bill ($)")
plt.show()
print(tips["total_bill"].median())
order = ["Thur", "Fri", "Sat", "Sun"]
avg_tip = tips.groupby("day")["tip"].mean().reindex(order)
print(avg_tip.round(2))
avg_tip.plot(kind="bar", rot=0, title="Average tip by day",
ylabel="Tip ($)")
plt.show()
Common mistake: a scatter plot with no columns named
tips.plot(kind="scatter")
tips.plot(kind="scatter", x="total_bill", y="tip", alpha=0.6,
title="Bill vs tip", xlabel="Bill ($)", ylabel="Tip ($)")
plt.show()
print(tips["total_bill"].corr(tips["tip"]).round(2))
tips[["total_bill", "tip"]].plot(kind="box", title="Bills and tips",
ylabel="Dollars")
plt.show()
print(tips["tip"].quantile([0.25, 0.5, 0.75]))
tips["tip"].plot.hist(bins=15, title="Tips", xlabel="Tip ($)")
plt.show()
print(tips["tip"].mode().iloc[0])
trips = ny.dt.date.value_counts().sort_index()
running = trips.cumsum()
running.plot(kind="area", alpha=0.5, title="Trips so far",
xlabel="Date", ylabel="Total trips", figsize=(8, 4))
plt.show()
print(running.iloc[-1])
Your turn#
Draw a horizontal bar chart of taxi pickups per borough.
boro = taxis["pickup_borough"].value_counts()
boro.plot(kind="barh", title="Pickups by borough",
xlabel="Trips")
plt.show()
print(boro)
Label and style a chart#
Problem: a chart nobody can read is a chart nobody uses.
ax = tips["tip"].plot(kind="hist", bins=15, color="teal",
edgecolor="white", figsize=(7, 4))
ax.set_title("How much do diners tip?")
ax.set_xlabel("Tip ($)")
ax.set_ylabel("Number of bills")
plt.show()
share = tips["time"].value_counts()
share.plot(kind="pie", autopct="%1.0f%%", ylabel="",
title="Bills by time of day")
plt.show()
print(share)
Common mistake: a pie chart of a whole table
smokers = tips.groupby("smoker")[["tip", "total_bill"]].sum()
smokers.plot(kind="pie")
smokers.plot(kind="pie", y="tip", autopct="%1.0f%%", ylabel="",
legend=False, title="Share of all tip money")
plt.show()
print(smokers)
Quick check: which is easier to compare, two pie slices or two bars?
ax = share.plot(kind="barh", color=["steelblue", "orange"],
title="Bills by time of day")
ax.set_xlabel("Bills")
plt.show()
Your turn#
Plot a labelled histogram of taxi fares, 30 bins, in any colour.
ax = taxis["fare"].plot(kind="hist", bins=30, color="slateblue")
ax.set_title("Taxi fares, March 2019")
ax.set_xlabel("Fare ($)")
ax.set_ylabel("Trips")
plt.show()
print(taxis["fare"].median(), taxis["fare"].max())
Plot several columns at once#
Problem: compare columns without drawing each one by hand.
march = ny.dt.month == 3
daily = taxis[march].groupby(ny[march].dt.date)[["fare", "tip"]].mean()
print(daily.round(2).head(3))
daily.plot(title="Average fare and tip per day", ylabel="$",
figsize=(9, 4))
plt.show()
Quick check: fares sit near 13 and tips near 2. Can you see the tip line moving?
daily.plot(subplots=True, figsize=(9, 5), legend=False,
title=["Average fare ($)", "Average tip ($)"])
plt.show()
print(daily["tip"].min().round(2), daily["tip"].max().round(2))
ax = daily.plot(secondary_y="tip", figsize=(9, 4),
title="Fare (left) and tip (right) per day")
ax.set_ylabel("Average fare ($)")
ax.right_ax.set_ylabel("Average tip ($)")
plt.show()
print(daily["fare"].corr(daily["tip"]).round(2))
counts = pd.crosstab(tips["day"], tips["time"]).reindex(order)
print(counts)
counts.plot(kind="bar", stacked=True, rot=0, ylabel="Bills",
title="Bills by day, lunch vs dinner")
plt.show()
taxis.plot(kind="hexbin", x="distance", y="fare", gridsize=25,
cmap="Blues", title="Distance vs fare",
xlabel="Miles", ylabel="Fare ($)")
plt.show()
print(taxis["distance"].corr(taxis["fare"]).round(2))
Combine charts and save them#
Problem: show charts side by side, and keep them as files.
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
tips["tip"].plot(kind="hist", bins=15, ax=axes[0],
title="Tips", xlabel="Tip ($)")
tips.plot(kind="scatter", x="size", y="tip", ax=axes[1],
title="Party size vs tip")
plt.tight_layout()
plt.show()
print(tips.groupby("size")["tip"].mean().round(2))
Common mistake: saving from the Axes
import os
os.makedirs("output/pandas", exist_ok=True)
ax = tips["total_bill"].plot(kind="hist", bins=20, title="Bills")
ax.savefig("output/pandas/l40_bills.png")
ax = tips["total_bill"].plot(kind="hist", bins=20, title="Bills")
ax.set_xlabel("Total bill ($)")
fig = ax.get_figure()
fig.savefig("output/pandas/l40_bills.png", dpi=100)
plt.close(fig)
img = plt.imread("output/pandas/l40_bills.png")
print(os.path.exists("output/pandas/l40_bills.png"))
print(img.ndim, img.shape[2])
Your turn#
Put the borough bars above the fare histogram, then save the figure.
fig, axes = plt.subplots(2, 1, figsize=(7, 7))
boro.plot(kind="barh", ax=axes[0], title="Pickups by borough")
taxis["fare"].plot(kind="hist", bins=30, ax=axes[1],
title="Taxi fares", xlabel="Fare ($)")
fig.tight_layout()
fig.savefig("output/pandas/l40_taxis.png", dpi=100)
print(os.path.exists("output/pandas/l40_taxis.png"))
Mini-project: do diners tip more at dinner?#
Plan: tip as a % of the bill, compare lunch and dinner, chart it.
tips["pct"] = tips["tip"] / tips["total_bill"] * 100
tips.groupby("time")["pct"].describe().round(1)
axes = tips.plot.box(column="pct", by="time", figsize=(7, 4),
ylabel="Tip (% of bill)")
plt.show()
high = tips[tips["pct"] > 30]
high[["total_bill", "tip", "size", "time", "pct"]].round(1)
by_day = tips.groupby("day")["pct"].median().reindex(order)
by_day.plot(kind="bar", rot=0, ylabel="Median tip (%)",
title="Median tip % by day")
plt.show()
print(by_day.round(1))
typical = tips[tips["pct"] <= 30]
med = typical.groupby("time")["pct"].median().round(1)
med.plot(kind="bar", rot=0, ylabel="Median tip (%)",
title="Median tip %: lunch vs dinner")
plt.show()
print(med)
lunch, dinner = med["Lunch"], med["Dinner"]
print(f"Median tip: lunch {lunch}%, dinner {dinner}%, "
f"a gap of {abs(dinner - lunch):.1f} points.")
Recap#
- .plot() turns any Series or DataFrame into a chart
- kind= picks the shape: line, bar, barh, hist, box, scatter, pie
- Count text columns first; scatter needs x= and y=
- Keep the Axes to add titles and labels
- ax= combines charts; save the figure, not the Axes
Challenges#
- Plot average taxi tip by payment type as a bar chart.
- Draw a pie chart of smokers vs non-smokers in the tips data.
- Put a histogram of party size next to a box plot of tip %.
What's next#
Lesson 41: Creating Line, Bar, Histogram, and Box Plots Go deeper on the four workhorse charts and when to use each.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



