Basic Data Visualization with Matplotlib
Charts help you see patterns that numbers alone can hide. This topic covers the most common Matplotlib charts and how to make them clear and easy to read.
a. Introduction to Matplotlib
Matplotlib is the most widely used library for making charts in Python. It can create line charts, bar charts, pie charts, histograms, and many more. Charts make patterns easy to see at a glance.
You usually import its plotting module as plt. Then you create a chart and call plt.show() to display it.
import matplotlib.pyplot as plt
months = ["Jan", "Feb", "Mar"]
sales = [200, 250, 300]
plt.plot(months, sales)
plt.show()In Jupyter Notebook, charts appear right below the cell. If a chart does not show, add %matplotlib inline at the top of your notebook.
b. Line Charts and Bar Charts
A line chart is best for showing change over time, like monthly sales or daily temperatures. You use plt.plot() and pass the x values and y values.
A bar chart is best for comparing categories, like sales by product or marks by subject. Use plt.bar() for this.
products = ["A", "B", "C"]
units = [50, 80, 65]
plt.bar(products, units)
plt.show()Pick the chart type based on your question. If you want to see a trend, use a line chart. If you want to compare groups, use a bar chart. The right chart makes your message clear.
c. Pie Charts and Histograms
A pie chart shows how a whole is divided into parts. It works well when you have a few categories and want to show their share, like market share by brand.
labels = ["Mobile", "Laptop", "Tablet"]
share = [60, 30, 10]
plt.pie(share, labels=labels, autopct="%1.1f%%")
plt.show()A histogram shows how values are spread out. It groups numbers into ranges called bins and counts how many fall into each one. Use plt.hist() for this.
Histograms help you see if data is balanced or skewed. Avoid pie charts with too many slices, as they become hard to read.
d. Adding Titles, Labels, and Legends
A good chart explains itself. Add a title, axis labels, and a legend so viewers know what they are looking at. Without these, even a nice chart can confuse people.
plt.plot(months, sales, label="2026 Sales")
plt.title("Monthly Sales")
plt.xlabel("Month")
plt.ylabel("Sales (in thousands)")
plt.legend()
plt.show()You can also change colors, line styles, and markers. Use plt.figure(figsize=(8, 5)) to control the chart size. Add plt.grid(True) to make values easier to read.
Keep your charts simple and clean. Use clear labels and avoid too many colors. To save a chart as an image, use plt.savefig("chart.png") before plt.show().
Data Analysis with Python Handbook
Wesline Rinisha








