Lesson 41 · Python for Data Science
1 - Matplotlib- Line, Bar, Scatter, Histogram
In this lesson, you will learn how to use Matplotlib to visualize data with line plots, bar charts, scatter plots, and histograms. Data visualization helps…
- CoursePython for Data Science
- Lesson41 of 38
- Video15 min
- FormatJupyter notebook · 18 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynb
Welcome to Matplotlib: Exploring Line, Bar, Scatter, and Histogram Plots!#
In this lesson, you will learn how to use Matplotlib to visualize data with line plots, bar charts, scatter plots, and histograms.
Data visualization helps you find patterns, spot trends, and tell a story with numbers.
Even if you have never coded before, do not worry!
By the end, you will build your own mini-project to practice your new skills.
Let us get started.
What is Matplotlib?#
Matplotlib is a popular Python library for making charts and graphs.
With just a few lines of code, you can turn simple data into colorful, easy-to-read images.
Let us set it up.
# First, import matplotlib.pyplot. This gives us access to plotting functions.
import matplotlib.pyplot as plt
Our First Line Plot#
A line plot draws a line connecting points in order. It is great for showing trends over time.
Let us plot some simple data.
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.title("Simple Line Plot")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.show()
Customizing the Line#
You can change the color, style, and width of your line.
Let us try making a dashed red line.
plt.plot(x, y, color="red", linestyle="--", linewidth=2)
plt.title("Dashed Red Line")
plt.show()
Making a Bar Chart#
Bar charts are perfect for comparing categories, like the number of sales each day.
Let us try one.
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
sales = [5, 7, 6, 4, 8]
plt.bar(days, sales, color="skyblue")
plt.title("Sales by Day")
plt.xlabel("Day of Week")
plt.ylabel("Number of Sales")
plt.show()
# Try making a bar chart with your favorite fruits.
fruits = ["Apple", "Banana", "Cherry"]
counts = [7, 3, 5]
plt.bar(fruits, counts, color="orange")
plt.title("Favorite Fruits in Class")
plt.ylabel("Number of Votes")
plt.show()
Drawing a Scatter Plot#
A scatter plot shows points instead of lines or bars.
Each dot represents a pair of values, like height and weight.
Let us make one.
heights = [150, 160, 165, 170, 175]
weights = [55, 60, 62, 72, 80]
plt.scatter(heights, weights, color="purple", marker="o")
plt.title("Height vs Weight")
plt.xlabel("Height (cm)")
plt.ylabel("Weight (kg)")
plt.show()
# Let us add labels to each point using the text function.
names = ["Anna", "Ben", "Eva", "Leo", "Mia"]
plt.scatter(heights, weights, color="green")
for i in range(len(names)):
plt.text(heights[i], weights[i], names[i])
plt.title("Heights and Weights with Labels")
plt.xlabel("Height (cm)")
plt.ylabel("Weight (kg)")
plt.show()
What is a Histogram?#
A histogram groups data into buckets called bins.
It lets you see how many values fall into each range.
You use histograms to spot patterns, like most people being average height.
Let us try a simple one.
scores = [55, 60, 65, 68, 72, 75, 80, 81, 82, 85, 85, 88, 92]
plt.hist(scores, bins=5, color="gold")
plt.title("Test Score Distribution")
plt.xlabel("Score Range")
plt.ylabel("Number of Students")
plt.show()
User Input for Custom Charts#
Let us make a chart based on your choices.
You will enter data, and we will plot it together!
category1 = input("Type a category name: ")
category2 = input("Type another category name: ")
value1 = int(input("Enter a number for " + category1 + ": "))
value2 = int(input("Enter a number for " + category2 + ": "))
plt.bar([category1, category2], [value1, value2], color=["blue", "pink"])
plt.title("Your Custom Bar Chart")
plt.show()
# Let us see what happens if you enter text instead of a number.
try:
bad_value = int(input("Type a number (just digits): "))
print("Good job! You entered:", bad_value)
except ValueError:
print("That is not a number. Please use digits only.")
Trying Out More Chart Types#
Matplotlib can make other charts too, like pie charts and box plots.
We will focus on the four basics today.
Let us look at a few more customizations.
# Change figure size and add a grid to a line plot.
plt.figure(figsize=(8, 4))
plt.plot(x, y, marker="s", color="brown", label="Sample Data")
plt.grid(True)
plt.legend()
plt.title("Line Plot with Grid and Legend")
plt.show()
# Save your chart as a picture file.
plt.bar(days, sales)
plt.title("Save This Chart")
plt.savefig("chart.png")
plt.show()
Mini-Project Part 1: Prize Winners#
Suppose you run a contest and want to show the number of prizes each person won.
Create lists for names and prizes, then make a bar chart.
Ready? Let us try it together!
names = ["Jamie", "Ava", "Sam", "Leo"]
prizes = [3, 1, 4, 2]
plt.bar(names, prizes, color=["blue", "red", "green", "purple"])
plt.title("Contest Prizes Won")
plt.xlabel("Person")
plt.ylabel("Prizes")
plt.show()
Mini-Project Part 2: Data Explorer#
Pick your favorite chart and fill it with your own made-up data.
Change the colors and labels.
Save it as a picture and share it with a friend or family member!
You are now a data explorer.
# For best practice, always call plt.clf() to clear the figure before a new plot.
plt.clf()
# Common mistake: forgetting to call plt.show().
plt.plot(x, y, color="teal")
# plt.show() is missing on purpose.
# The chart will not appear until plt.show() is called.
Challenge! Make Your Own Chart#
Pick a kind of chart: line, bar, scatter, or histogram.
Invent some data (real or pretend).
Make your chart. Add a title, labels, and colors.
Try using plt.savefig to store your masterpiece.
Recap and What Comes Next#
You learned how to make line, bar, scatter, and histogram charts with Matplotlib.
You practiced customizing, labeling, and even handling mistakes.
Data visualization is a powerful skill for telling any story with numbers.
Keep exploring and experimenting!
Thank you for learning with me.
If You Enjoyed This Video...#
Please like, comment, and subscribe for more beginner Python tutorials.
Share this lesson with a friend who wants to make data more fun and visual.
See you in the next one!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



