How to plot two DataFrame columns in pandas?

Today we will learn how to plot multiple specific columns of a pandas DataFrame against each other in pandas bar, scatter, line and histogram charts.

We will start by importing the pandas library and then create an example DataFrame.

import pandas as pd

# create data for the DataFrame
biz_date = pd.Series(pd.date_range(start='2/10/23', end = '2/23/23', freq='B'))
revenue = [4746, 3371, 4747, 4601, 4501, 3438, 2899, 3860, 3960, 3337]
expense = [2268, 2863, 3298, 2339, 3072, 2491, 3446, 2733, 2894, 2673]

#Construct the DataFrame

revenue = pd.DataFrame(dict(revenues = revenue, expenses =expense), index = biz_date)

Plot two columns in the same scatter chart

Creating a scatter with pandas is relatively simple. We’ll first build the chart, then assign a title to the scatter using the set_title() method. Make sure to designate your specific column names (as strings) to the x and y parameters.

perf_scatter = revenue.plot.scatter(x='revenues', y='expenses')
perf_scatter.set_title("Revenue vs Expenses");

Here’s our chart:

Draw columns against each other in a bar chart

An effective way to compare two key performance indicators is simply plot them on the same axis one against the other in a bar chart.

We first create the chart and assign it a custom colormap, we then tweak a bit the x axis labels to make them a bit more legible. Last we add a title.

perf_bar = revenue.plot.bar(cmap='Dark2')
perf_bar.set_xticklabels(d.strftime('%m-%d-%y') for d in revenue.index)
perf_bar.set_title("Revenue vs Expenses");

Here’s our bar chart:

Plot two columns on different figure axes

Last example is to draw the two columns on different axes of a figure.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(1,2, figsize = (10,6));
# rotates the xticks by 45 degrees
fig.autofmt_xdate(rotation=45)
# titles
ax[0].set_title('Revenue')
ax[1].set_title('Expenses')

# draw line charts
ax[0].plot(revenue.index, revenue['revenues']) 
ax[1].plot(revenue.index, revenue['expenses']);

Plotting two columns as histograms

In the same fashion we can plot our columns as two side-by-side histogram charts:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(1,2, figsize = (10,6));
# rotates the xticks by 45 degrees
fig.autofmt_xdate(rotation=45)
# titles
ax[0].set_title('Revenue frequency')
ax[1].set_title('Expense frequency')

# draw histograms
ax[0].hist(revenue['revenues'])
ax[1].hist(revenue['expenses']);

Here’s our chart:

Follow up learning

How to plot an horizontal line in your pandas charts?