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 how to plot a DataFrame using pandas.

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

Let’s now review 3 simple examples to see how to plot each of those 3 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:

 

Unemployment_RateStock_Index_Price
6.11500
5.81520
5.71525
5.71523
5.81515
5.61540
5.51545
5.31560
5.21555
5.21565

 

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:

 

YearUnemployment_Rate
19209.8
193012
19408
19507.2
19606.9
19707
19806.5
19906.2
20005.5
20106.3

 

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 data-set:

 

CountryGDP_Per_Capita
USA45000
Canada42000
Germany52000
UK49000
France47000

 

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

 

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