How to Plot a DataFrame using Pandas

In this guide, you’ll see how to plot a DataFrame using Pandas.

More specifically, you’ll see the complete steps to plot:

  • Scatter diagram
  • Line chart
  • Bar chart
  • Pie chart

Plot a Scatter Diagram using Pandas

Scatter plots are used to depict a relationship between two variables. Here are the steps to plot a scatter diagram using Pandas.

Step 1: Prepare the data

To start, prepare the data for your scatter diagram.

For example, the following data will be used to create the scatter diagram. This data captures the relationship between two variables related to an economy:

example of dataset

Step 2: Create the DataFrame

Once you have your data ready, you can proceed to create the DataFrame in Python. For our example, the DataFrame would look like this:

import pandas as pd
   
data = {'Unemployment_Rate': [6.1,5.8,5.7,5.7,5.8,5.6,5.5,5.3,5.2,5.2],
        'Stock_Index_Price': [1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]
       }
  
df = pd.DataFrame(data,columns=['Unemployment_Rate','Stock_Index_Price'])
print (df)

Run the code in Python, and you’ll get the following DataFrame:

Example of dataset

Step 3: Plot the DataFrame using Pandas

Finally, you can plot the DataFrame by adding the following syntax:

df.plot(x ='Unemployment_Rate', y='Stock_Index_Price', kind = 'scatter')	

Notice that you can specify the type of chart by setting kind = ‘scatter’

You’ll also need to add the Matplotlib syntax to show the plot (ensure that the Matplotlib package is install in Python):

  • import matplotlib.pyplot as plt
  • plt.show()

Putting everything together:

import pandas as pd
import matplotlib.pyplot as plt
   
data = {'Unemployment_Rate': [6.1,5.8,5.7,5.7,5.8,5.6,5.5,5.3,5.2,5.2],
        'Stock_Index_Price': [1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]
       }
  
df = pd.DataFrame(data,columns=['Unemployment_Rate','Stock_Index_Price'])
df.plot(x ='Unemployment_Rate', y='Stock_Index_Price', kind = 'scatter')
plt.show()

Once you run the above code, you’ll get the following scatter diagram:

How to Plot a DataFrame using Pandas

Plot a Line Chart using Pandas

Line charts are often used to display trends overtime. Let’s now see the steps to plot a line chart using Pandas.

Step 1: Prepare the data

To start, prepare your data for the line chart. Here is an example of a dataset that captures the unemployment rate over time:

data Python

Step 2: Create the DataFrame

Now create the DataFrame based on the above data:

import pandas as pd
   
data = {'Year': [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010],
        'Unemployment_Rate': [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
       }
  
df = pd.DataFrame(data,columns=['Year','Unemployment_Rate'])
print (df)

This is how the DataFrame would look like:

Plot a DataFrame using Pandas

Step 3: Plot the DataFrame using Pandas

Finally, plot the DataFrame by adding the following syntax:

df.plot(x ='Year', y='Unemployment_Rate', kind = 'line')

You’ll notice that the kind is now set to ‘line’ in order to plot the line chart.

Here is the complete Python code:

import pandas as pd
import matplotlib.pyplot as plt
   
data = {'Year': [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010],
        'Unemployment_Rate': [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
       }
  
df = pd.DataFrame(data,columns=['Year','Unemployment_Rate'])
df.plot(x ='Year', y='Unemployment_Rate', kind = 'line')
plt.show()

And once you run the code, you’ll get this line chart:

Plot a DataFrame using Pandas

Plot a Bar Chart using Pandas

Bar charts are used to display categorical data. Let’s now see how to plot a bar chart using Pandas.

Step 1: Prepare your data

As before, you’ll need to prepare your data. Here, the following dataset will be used to create the bar chart:

dataset Python

Step 2: Create the DataFrame

Create the DataFrame as follows:

import pandas as pd
   
data = {'Country': ['USA','Canada','Germany','UK','France'],
        'GDP_Per_Capita': [45000,42000,52000,49000,47000]
       }
  
df = pd.DataFrame(data,columns=['Country','GDP_Per_Capita'])
print (df)

You’ll then get this DataFrame:

Plot a DataFrame using Pandas

Step 3: Plot the DataFrame using Pandas

Finally, add the following syntax to the Python code:

df.plot(x ='Country', y='GDP_Per_Capita', kind = 'bar')

In this case, set the kind = ‘bar’ to plot the bar chart.

And the complete Python code is:

import pandas as pd
import matplotlib.pyplot as plt
   
data = {'Country': ['USA','Canada','Germany','UK','France'],
        'GDP_Per_Capita': [45000,42000,52000,49000,47000]
       }
  
df = pd.DataFrame(data,columns=['Country','GDP_Per_Capita'])
df.plot(x ='Country', y='GDP_Per_Capita', kind = 'bar')
plt.show()

Run the code and you’ll get this bar chart:

How to Plot a DataFrame using Pandas

Plot a Pie Chart using Pandas

Step 1: Prepare your data

For demonstration purposes, the following data about the status of tasks was prepared:

Tasks Pending300
Tasks Ongoing500
Tasks Completed700

The goal is to create a pie chart based on the above data.

Step 2: Create the DataFrame

You can then create the DataFrame using this code:

import pandas as pd

data = {'Tasks': [300,500,700]}
df = pd.DataFrame(data,columns=['Tasks'],index = ['Tasks Pending','Tasks Ongoing','Tasks Completed'])

print (df)

You’ll now see this DataFrame:

Data for chart

Step 3: Plot the DataFrame using Pandas

Finally, plot the DataFrame by adding the following syntax:

df.plot.pie(y='Tasks',figsize=(5, 5),autopct='%1.1f%%', startangle=90)

And here is the complete Python code:

import pandas as pd
import matplotlib.pyplot as plt

data = {'Tasks': [300,500,700]}
df = pd.DataFrame(data,columns=['Tasks'],index = ['Tasks Pending','Tasks Ongoing','Tasks Completed'])

df.plot.pie(y='Tasks',figsize=(5, 5),autopct='%1.1f%%', startangle=90)
plt.show()

Once you run the code, you’ll get this pie chart:

pie chart pandasYou just reviewed few examples about plotting DataFrames using Pandas. A good additional source for plotting DataFrames is the Pandas Documentation.