Python Data Visualization
In this page:
import matplotlib.pyplot as plt
plt.bar(categories, values)
plt.xlabel("label")
plt.ylabel("label")
plt.show()
Drawing Bar Charts
plt.bar() draws vertical bars (or plt.barh() for horizontal ones) with height proportional to each category's value, making bar charts the natural choice whenever you're comparing a handful of discrete categories against each other.
Example: Drawing Bar Charts
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
plt.bar(["A", "B", "C"], [10, 20, 15]) # bar height proportional to each value
plt.savefig("bar.png")
Drawing Scatter Plots
plt.scatter() plots each data point individually as a dot positioned by its x and y values, without connecting them with a line. This makes scatter plots the standard way to visually check whether two numeric variables appear correlated.
Example: Drawing Scatter Plots
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
plt.scatter([1, 2, 3], [4, 1, 5]) # each point plotted individually, no connecting line
plt.savefig("scatter.png")
Drawing Histograms
plt.hist() sorts continuous numeric data into a set of ranges called bins and draws a bar for each bin's count, revealing the overall shape of a distribution -- whether it's roughly symmetric, skewed, or has multiple peaks -- in a way a raw list of numbers never could.
Example: Drawing Histograms
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
plt.hist([1, 2, 2, 3, 3, 3, 4]) # sorts values into bins and draws a bar per bin
plt.savefig("hist.png")
Drawing Pie Charts
plt.pie() draws each category as a proportional wedge of a circle, sized by its share of the total.
Pie charts work best with a small number of categories that clearly sum to a meaningful whole; too many slices makes the chart cluttered and hard to compare accurately.
Example: Drawing Pie Charts
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
plt.pie([30, 20, 50], labels=["A", "B", "C"]) # each wedge sized by its share of the total
plt.savefig("pie.png")
Creating Subplots
plt.subplots(rows, cols) creates a grid of independent axes within one figure, letting you display several related charts side by side for direct comparison instead of generating separate images that the viewer has to mentally line up.
Example: Creating Subplots
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2) # 1 row, 2 columns of independent charts
axes[0].plot([1, 2, 3])
axes[1].bar(["A", "B"], [5, 10])
plt.savefig("subplots.png")
- Choosing the wrong chart for the data, such as a pie chart with many categories, which is unreadable.
- Passing data lists of different lengths to
plt.bar()orplt.scatter(), which raisesValueError. - Reusing one figure for many plots without
plt.figure()orplt.close(), so charts overlap.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Python NumPy Introduction
- Python NumPy Arrays
- Python Pandas Introduction
- Python Pandas DataFrame
- Python Matplotlib Basics
- Python Data Visualization
- Python Statistics Module
- Python CSV & Data Analysis
- Python requests Module
- Python JSON & APIs
- Python Web Scraping Basics
- Python Flask Introduction
- Python Django Introduction
- Python MongoDB