Line/bar/histogram
Line charts show trends, bar charts compare categories and histograms show how values are distributed.
In this page:
Syntax
df.plot(kind="line")
df.plot(kind="bar")
df["column"].plot(kind="hist", bins=n)
Line/bar/histogram
kind="line" suits ordered data such as time. kind="bar" (or .plot.bar()) compares categories, and kind="hist" bins numeric values. bins controls the histogram resolution. Use kind="barh" for horizontal bars.
Note:
value_counts().plot.bar() is a quick chart of category frequencies.
Example: Line/bar/histogram
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import pandas as pd
s = pd.Series([3, 5, 5, 6, 8, 8, 8, 9])
kinds = []
for kind in ["line", "bar", "hist"]:
fig, ax = plt.subplots()
s.plot(kind=kind, ax=ax)
kinds.append((kind, len(ax.patches) or len(ax.lines)))
plt.close(fig)
print(kinds)
# Output:
# [('line', 1), ('bar', 8), ('hist', 10)]
Related Topics
Common Mistakes
- Using a line chart for unordered categories
- Too many histogram bins
- Plotting unsorted categories
Chapter Summary
- line for trends
- bar for categories
- hist for distributions
- bins sets resolution
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: