Matplotlib: A plotting Library for the Python

Matplotlib is an “extensive library for creating static, animated, and interactive visualizations in Python.” Most of the Matplotlib utilities lie under the pyplot submodule and are usually imported under the plt alias:


Use the  read_csv() function of the DataFrame data structure in Pandas.

data = pd.read_csv('data.csv', skiprows=4)

You can use the read_csv() function, skip the first four rows, and then display the remaining ones.

Run the cell and see the output. It will show the first 30 rows and the last 30 if so many exist. In our data file, there are more than 29,000 rows. That is why we can see the first and last 30 rows.

Matplotlib Tutorial With Example

Import Matplotlib

We can import the Matplotlib library using the following code. Next, write the following code inside the next Jupyter Notebook cell.

import matplotlib.pyplot as plt

%matplotlib inline

Now, hit Ctrl + Enter, which will import the Library.

An iPython kernel works seamlessly with Matplotlib.pyplot Library.

You can see in the above code that we have used the %matplotlib inline magic command, which means it will show different charts inside the Jupyter Notebook.

Plot Types

There is a kind of Plot type which are following.

  1. plot(kind=line): It is best when we need to track the changes over some time.
  2. plot(kind=bar): Bar graphs are best for comparing the groups.
  3. plot(kind=pie): Best for reaching the parts of a whole system.

Now let’s take an example of a one-by-one chart in Jupyter Notebook.

Let’s plot a graph of sports that participate in the Olympics Edition 2008.

We have already imported the matplotlib.pyplot Library in the Notebook, now we will use that to plot the graph of different sports.

Plotting a Line Chart

We will display the line chart. So let’s add the following code to the Jupyter Notebook.

filteredData = data[data.Edition == 2008]

In the above code, first, we have the data for the Olympics 2008 edition; then, we have to count the number of sports the Olympics has and plot the line graph based on that data.

Python Jupyter Notebook Course

By default, the plot() function gives us the line chart.

Plotting a BarChart 

We can also display the bar chart instead of the line chart. We must pass a parameter kind and value to the bar, showing the bar chart.

filteredData = data[data.Edition == 2008]

Here, we have used the head() function to display the first five rows and plot the bar charts based on the sports count held in the 2008 Olympics.


Bar chart in Matplotlib.pyplot

The above bar chart is the Vertical Bar Chart.

We can also get the Horizontal plot using the following code.


We passed the kind=’barh’ parameter, giving us the following result.

Horizontal Bar chart in Matplotlib.pyplot

Plotting a Pie Chart

We can also display the pie chart instead of the bar chart. We must pass a parameter kind and value to the pie, showing the bar chart. See the following example. Write the following code in the cell.

filteredData = data[data.Edition == 2008]


Plot a Pie Chart using Matplotlib.pyplot Library

So, we have learned all kinds of charts using the Real-time example in Python Jupyter Notebook.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.