Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to import matplotlib in Python

Getting Started

Before we dive into the how-to of importing matplotlib, let's first understand what matplotlib is. Matplotlib is a plotting library for Python. Just as a painter uses a palette of colors and brushes to create an art piece, a programmer uses libraries like matplotlib to create beautiful graphs, charts, and other visualizations.

Now, let's imagine that these libraries are not just given to you but are kept in a large warehouse (this would be your computer's hard drive). To use them, you have to first take them out of this warehouse and bring them to your workspace (this is your Python program). This process of bringing a library from the warehouse to your workspace is what we call 'importing a library' in programming.

Importing Matplotlib

Now that you understand the concept of importing in programming, let's see how we can import matplotlib in Python.

import matplotlib.pyplot as plt

Here, import is a keyword in Python that is used to import a library. matplotlib.pyplot is the part of the matplotlib library that we will be using for plotting. as is another keyword that is used to create an alias for the library we are importing. plt is the alias we are creating for matplotlib.pyplot. We use an alias simply to make our code cleaner and easier to write.

How to use Matplotlib

Now that we have matplotlib in our workspace, let's use it to plot a simple line graph.

plt.plot([1, 2, 3, 4])

In the above code, plt.plot([1, 2, 3, 4]) is used to plot the numbers 1, 2, 3, 4 on the y-axis. plt.ylabel('Numbers') is used to label the y-axis as 'Numbers'. is used to display the plot. When you run this code, you will see a line graph of the numbers 1, 2, 3, 4.

Different Types of Plots

Matplotlib can be used to create a variety of plots. Let's look at a few examples.

Bar Plot

A bar plot is a plot that represents categorical data with rectangular bars. Each bar has a height corresponding to the value it represents.

names = ['A', 'B', 'C']
values = [5, 8, 3], values)

Here,, values) is used to create a bar plot with the categories 'A', 'B', 'C' and their corresponding values 5, 8, 3.

Scatter Plot

A scatter plot uses dots to represent values for two different numeric variables.

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.scatter(x, y)

Here, plt.scatter(x, y) is used to create a scatter plot with the x-values 1, 2, 3, 4, 5 and their corresponding y-values 2, 3, 5, 7, 11.


And there you have it! You've just navigated through the process of importing and using matplotlib in Python. It's like learning to ride a bike. At first, it may feel a bit wobbly, but with practice, you'll be riding with no hands and the wind in your hair. Keep practicing, keep experimenting and pretty soon, you'll be creating Python visualizations as effortlessly as Picasso painted masterpieces. Just remember, every line of code is a stroke on the canvas of your program, and with matplotlib, you have a complete palette to make your data come alive. So, go ahead, paint your masterpiece!