Latest Code Tutorials

Python Matplotlib Example | Matplotlib.pyplot Library


Python Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. In this tutorial, we will see how to use the Matplotlib library to learn how to report and chart using the Python matplotlib library.

Matplotlib example

The matplotlib.pyplot is the collection of command style and functions that make matplotlib works like a MATLAB in Python.

Each pyplot function makes some changes to a figure, and we will able to analyze the data based on that figure.

Matplotlib is the Python 2D plotting chart library that produces the publication quality figures in the hardcopy formats and interactive environments across the platforms. 

Matplotlib can be used in the Python scripts, the Python and IPython shells, the Jupyter Notebook,  a web application servers, and four graphical user interface toolkits. Matplotlib is the basics of Python data visualization. Let’s start Matplotlib Tutorial With Example.

Matplotlib tries to make easy things easy and hard things as possible. You can generate the plots, histograms, power spectra, bar charts, error charts, scatterplots, etc., with just a few lines of code.

So, in this demo, we will use Jupyter Notebook to display different kinds of charts. Also, we need data to work on this project. You can save the CSV file from the below URL.

Now, open the Jupyter Notebook and import the Pandas Library first.

Write the following code inside the first cell in Jupyter Notebook.

import pandas as pd

Run the cell by hitting Ctrl + Enter.

Okay, now we will use the  read_csv() function of the DataFrame data structure in Pandas. So write the following code in the next cell.

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

So, we have used the read_csv() function and skipped the first four rows and then display the remaining rows. Run the cell and see the output. It will show the first 30 rows and the last 30 rows if there are so many rows. In our data file, there are above 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. Write the following code inside the next Jupyter Notebook cell.

import matplotlib.pyplot as plt
%matplotlib inline

Now, hit the Ctrl + Enter, and it will import the library.

An iPython kernel works seamlessly with the Matplotlib.pyplot library.

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

Plot Types

There is a kind of Plot types 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 comparing the parts of a whole system.

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

Let’s plot a graph of different sports takes part 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.

Plot a Line Chart using Matplotlib.pyplot Library

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

filteredData = data[data.Edition == 2008]

Now, in the above code, first, we have got the data of Olympics 2008 edition, and then we have to count the number of sports that Olympic has and plot the line graph based on that data. The output of the above code in the Jupyter Notebook is the following.

Python Jupyter Notebook Course

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

Plot a Bar Chart using Matplotlib.pyplot Library

We can also display the bar chart instead of the line chart. We need to pass a parameter kind and value to the bar, and it will show the bar chart. See the following example. Write the following code in the cell.

filteredData = data[data.Edition == 2008]

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

The output is the following.

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 have passed the kind=’barh’ parameter, and it will give us the following result.

Horizontal Bar chart in Matplotlib.pyplot

Plot a Pie Chart using Matplotlib.pyplot Library

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

filteredData = data[data.Edition == 2008]

See the output below.

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.

Finally, Python Matplotlib Example is over.

Leave A Reply

Your email address will not be published.

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