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()plt.show()
---
## 4. Histogram (Frequency Distribution)
```python
import matplotlib.pyplot as plt
# Marks of 20 students
marks = [55, 60, 65, 68, 70, 72, 75, 78, 80, 82, 85, 88, 90, 92, 95, 96, 98, 99, 100]
plt.hist(marks, bins=5, color="teal", edgecolor="black") # bins=5 divides data into 5 intervals
plt.title("Student Marks Distribution")
plt.xlabel("Marks Range")
plt.ylabel("Number of Students")
plt.show()
| Chart Type | Best Use Case | Syntax |
|---|---|---|
| Line Plot | Continuous trends (Time series) | plt.plot(x, y) |
| Bar Chart | Categorical comparisons | plt.bar(categories, values) |
| Scatter Plot | Relationship/Correlation between X and Y | plt.scatter(x, y) |
| Histogram | Data distribution & frequency ranges | plt.hist(data, bins=10) |
| Pie Chart | Proportions & percentage breakdown | plt.pie(values, labels=names) |
Task: Create a Bar Chart showing sales of 3 smartphone brands: "iPhone": 50, "Samsung": 70, "OnePlus": 40.
💡 Click to See Solution
import matplotlib.pyplot as plt
brands = ["iPhone", "Samsung", "OnePlus"]
sales = [50, 70, 40]
plt.bar(brands, sales, color="skyblue", edgecolor="darkblue")
plt.title("Smartphone Brand Sales")
plt.xlabel("Brand")
plt.ylabel("Units Sold")
plt.show()