Data Visualization with Matplotlib
Discover how to create visualizations to understand your data.
Data Visualization with Matplotlib is a free Learn AI with Python lesson on CoddyKit — lesson 2 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Introduction to Matplotlib
Matplotlib is a popular Python library for creating visualizations, including line plots, bar charts, scatter plots, and more.
In this lesson, you’ll learn how to use Matplotlib to create and customize various types of plots.

2
Installing Matplotlib
To install Matplotlib, use the following command in your terminal:
pip install matplotlib
Once installed, you can import it in your Python scripts:
# Importing matplotlib
import matplotlib.pyplot as plt
print("Matplotlib imported successfully")3
Creating a Simple Line Plot
You can create a basic line plot using Matplotlib’s plot() function:
# Creating a simple line plot
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title("Simple Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()4
Customizing Plots
Matplotlib allows you to customize plots with titles, labels, legends, and different line styles:
# Customizing a plot
plt.plot(x, y, label="Line 1", linestyle="--", color="red")
plt.legend()
plt.title("Customized Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()5
Creating Bar Charts
Bar charts are useful for comparing categories. Use the bar() function to create a bar chart:
# Creating a bar chart
categories = ["A", "B", "C"]
values = [10, 15, 7]
plt.bar(categories, values, color="blue")
plt.title("Bar Chart")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()6
Creating Scatter Plots
Scatter plots are useful for showing relationships between two variables. Use the scatter() function:
# Creating a scatter plot
x = [5, 7, 8, 7]
y = [3, 8, 6, 4]
plt.scatter(x, y, color="green")
plt.title("Scatter Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()7
Creating Histograms
Histograms show the distribution of a dataset. Use the hist() function:
# Creating a histogram
data = [1, 1, 2, 3, 3, 3, 4, 4, 5]
plt.hist(data, bins=5, color="purple")
plt.title("Histogram")
plt.xlabel("Bins")
plt.ylabel("Frequency")
plt.show()8
Adding Multiple Plots
You can add multiple plots to the same figure using the subplot() function:
# Adding multiple plots
plt.subplot(2, 1, 1)
plt.plot([1, 2, 3], [4, 5, 6])
plt.title("First Plot")
plt.subplot(2, 1, 2)
plt.bar([1, 2, 3], [7, 8, 9])
plt.title("Second Plot")
plt.tight_layout()
plt.show()9
10
Common Mistakes with Matplotlib
Here are some mistakes to avoid:
- Forgetting to call
plt.show()to display the plot. - Using overlapping elements without adjusting the layout.
- Not labeling axes and plots for clarity.
11
What Did We Learn?
In this lesson, you learned:
- How to install and import Matplotlib.
- How to create line plots, bar charts, scatter plots, and histograms.
- How to customize plots with labels, legends, and multiple plots.
Great job! Let’s move to the next topic.

Frequently asked questions
Is the “Data Visualization with Matplotlib” lesson free?
Yes — the full text of “Data Visualization with Matplotlib” is free to read here on the web, and the Learn AI with Python course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Data Visualization with Matplotlib”?
Discover how to create visualizations to understand your data. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Data Visualization with Matplotlib” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Data Analysis with Pandas
- Data Visualization with Matplotlib
- NumPy for Numerical Computations
- Handling APIs with requests
- Web Scraping with BeautifulSoup