Lesson 26: Introduction to Data Visualization (matplotlib)

How to create charts and graphs using Python’s most popular plotting library

Why Data Visualization Matters

Data visualization helps you understand patterns, trends, and insights that are difficult to see in raw data. Python’s matplotlib library is the foundation of most visualization tools in the Python ecosystem.

Installing matplotlib

pip install matplotlib

Your First Plot

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.plot(x, y)
plt.show()

Adding Labels and Titles

plt.plot(x, y)
plt.title("Simple Line Chart")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.show()

Bar Charts

labels = ["A", "B", "C"]
values = [5, 7, 3]

plt.bar(labels, values)
plt.show()

Scatter Plots

plt.scatter(x, y)
plt.show()

Pie Charts

sizes = [40, 30, 20, 10]
labels = ["A", "B", "C", "D"]

plt.pie(sizes, labels=labels, autopct="%1.1f%%")
plt.show()

Customizing Colors and Styles

plt.plot(x, y, color="red", linestyle="--", marker="o")
plt.show()

Multiple Lines on One Chart

plt.plot(x, y, label="Line 1")
plt.plot(x, [v * 2 for v in y], label="Line 2")
plt.legend()
plt.show()

Saving Figures

plt.savefig("chart.png")

Why matplotlib Is Important

Next Steps

Now that you can create basic visualizations, you're ready to explore more advanced data analysis in Lesson 27: Introduction to Data Analysis with pandas.

← Back to Lesson Index