AppDividend
Latest Code Tutorials

Matplotlib Tutorial With Example | Python Jupyter Notebook Course

0

In this tutorial, we will see the Matplotlib Tutorial With Example | Python Jupyter Notebook Course. We will learn how to report and chart using the Python matplotlib library. 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 change to a figure and we will able to analyze the data based on that figure.

Matplotlib is the Python 2D plotting chart library which 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.

Matplotlib Tutorial With Example

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

https://docs.google.com/spreadsheets/d/1zeeZQzFoHE2j_ZrqDkVJK9eF7OH1yvg75c8S-aBcxaU/edit#gid=0

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 DataFrame data structure in Pandas. So write the following code in the next cell.

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

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 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 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 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]
filteredData.Sport.value_counts().plot()

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 Jupyter Notebook is 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]
filteredData.head()
filteredData.Sport.value_counts().plot(kind='bar')

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 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.

filteredData.Sport.value_counts().plot(kind='barh')

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]
filteredData.head()
filteredData.Sport.value_counts().plot(kind='pie')

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 Tutorial With 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.