Line charts are only one way to display data. Depending on your data structure, you might want to show correlations, compare categories, view frequency distributions, or inspect proportions. Matplotlib offers simple APIs to generate all standard plot types.
Scatter plots display individual data points as dots. They are used to explore relationships (correlations) between two variables.
Input:
import matplotlib.pyplot as plt
# Height (in cm) vs Weight (in kg)
heights = [150, 160, 165, 170, 175, 180, 185]
weights = [55, 60, 62, 68, 75, 80, 85]
# Create scatter plot
plt.scatter(heights, weights, color="darkblue", marker="D", s=50) # s controls size
plt.title("Height vs. Weight Correlation")
plt.xlabel("Height (cm)")
plt.ylabel("Weight (kg)")
plt.grid(True)
plt.show()Bar charts display comparison values across distinct categories (like product names or departments).
plt.bar(): Vertical bars.plt.barh(): Horizontal bars (useful when category labels are long).
Input:
import matplotlib.pyplot as plt
# Categories and their values
languages = ["Python", "Java", "JavaScript", "C++", "Go"]
popularity = [90, 70, 85, 60, 50]
# 1. Vertical Bar Chart
plt.bar(languages, popularity, color="skyblue", edgecolor="blue")
plt.title("Programming Language Popularity (Vertical)")
plt.xlabel("Language")
plt.ylabel("Score")
plt.show()
# 2. Horizontal Bar Chart
plt.barh(languages, popularity, color="lightgreen")
plt.title("Programming Language Popularity (Horizontal)")
plt.xlabel("Score")
plt.ylabel("Language")
plt.show()Histograms are used to visualize the distribution of continuous numerical data. The values are grouped into ranges called bins, and the height of the bar shows the frequency (how many data points fall inside each bin).
Input:
import matplotlib.pyplot as plt
# Ages of attendees at a conference
ages = [18, 21, 25, 26, 30, 32, 33, 35, 40, 42, 45, 52, 55, 60]
# Generate histogram with 5 bins
plt.hist(ages, bins=5, color="purple", edgecolor="black")
plt.title("Age Distribution of Conference Attendees")
plt.xlabel("Age Range (Bins)")
plt.ylabel("Frequency")
plt.show()Pie charts represent proportions of a whole. Each category is displayed as a wedge representing its percentage of the total.
labels: List of text labels for each wedge.autopct: Formatting string to display percentages inside the slices (e.g.'%1.1f%%').explode: Tuple specifying which slices to offset/pull out slightly.
Input:
import matplotlib.pyplot as plt
# Share of sales by product category
categories = ["Electronics", "Clothing", "Home Decor", "Books"]
shares = [45, 25, 18, 12]
# Pull the first slice (Electronics) out slightly
explode_effect = (0.1, 0, 0, 0)
plt.pie(shares,
labels=categories,
autopct="%1.1f%%",
startangle=90, # Rotates the start of the pie
explode=explode_effect,
colors=["gold", "yellowgreen", "lightcoral", "lightskyblue"])
plt.title("Sales Share by Category")
plt.show()