How to Plot a DataFrame using Pandas

There are different Python libraries, such as Matplotlib, which can be used to plot DataFrames. But did you know that you could also plot a DataFrame using pandas?

You can certainly do that. In this short tutorial, I’ll show you the steps to plot a DataFrame using pandas.

More specifically, I’ll show you how to plot a scatter, line, bar and pie charts using pandas.

Let’s now review 4 simple examples to see how to plot each of those 4 charts.

Plot a scatter diagram using pandas

Scatter plots are used to depict a relationship between two variables. In the next section, I’ll review the steps to plot a scatter diagram using pandas.

Step 1: Collect the data

To start, you’ll need to collect the data that will be used to create the scatter diagram.

For example, I collected the following data that 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:

from pandas import DataFrame
   
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 = DataFrame(Data,columns=['Unemployment_Rate','Stock_Index_Price'])
print (df)

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 that you need by setting kind = ‘scatter’

Putting everything together:

from pandas import DataFrame
   
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 = DataFrame(Data,columns=['Unemployment_Rate','Stock_Index_Price'])
df.plot(x ='Unemployment_Rate', y='Stock_Index_Price', kind = 'scatter')	

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: Collect the data

To start, you’ll need to collect the data for the line chart. Here, I compiled the following data, which captures the unemployment rate over time:

data Python

Step 2: Create the DataFrame

Now create the pandas DataFrame based on the above data:

from pandas import DataFrame
   
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 = DataFrame(Data,columns=['Year','Unemployment_Rate'])
print (df)	

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:

from pandas import DataFrame
   
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 = DataFrame(Data,columns=['Year','Unemployment_Rate'])
df.plot(x ='Year', y='Unemployment_Rate', kind = 'line')	

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: Collect your data

As before, you’ll need to collect your data. Here I used the following dataset:

dataset Python

Step 2: Create the DataFrame

Create the DataFrame as follows:

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

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:

from pandas import DataFrame
   
Data = {'Country': ['USA','Canada','Germany','UK','France'],
        'GDP_Per_Capita': [45000,42000,52000,49000,47000]
       }
  
df = DataFrame(Data,columns=['Country','GDP_Per_Capita'])
df.plot(x ='Country', y='GDP_Per_Capita', kind = 'bar')

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: Collect your data

For demonstration purposes, I collected the following data about the status of tasks:

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:

from pandas import DataFrame

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

print (df)

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:

from pandas import DataFrame

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

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

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

pie chart pandasWe just reviewed few examples about plotting DataFrames using pandas. A good additional source for plotting DataFrames is the pandas documentation.