← Back to Pandas Course | Chapter 12: Visualization Basics | Lesson 2 of 6

Line/bar/histogram

Line charts show trends, bar charts compare categories and histograms show how values are distributed.

In this page:

  1. Line/bar/histogram
Syntax
python
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

python
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
  1. Using a line chart for unordered categories
  2. Too many histogram bins
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.