How to create charts and graphs using Python’s most popular plotting library
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.
pip install matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.show()
plt.plot(x, y)
plt.title("Simple Line Chart")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.show()
labels = ["A", "B", "C"]
values = [5, 7, 3]
plt.bar(labels, values)
plt.show()
plt.scatter(x, y)
plt.show()
sizes = [40, 30, 20, 10]
labels = ["A", "B", "C", "D"]
plt.pie(sizes, labels=labels, autopct="%1.1f%%")
plt.show()
plt.plot(x, y, color="red", linestyle="--", marker="o")
plt.show()
plt.plot(x, y, label="Line 1")
plt.plot(x, [v * 2 for v in y], label="Line 2")
plt.legend()
plt.show()
plt.savefig("chart.png")
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