Pandas DataFrame Plot - Bar Chart

Recently, I've been doing some visualization/plot with Pandas DataFrame in Jupyter notebook. In this article I'm going to show you some examples about plotting bar chart (incl. stacked bar chart with series) with Pandas DataFrame. I'm using Jupyter Notebook as IDE/code execution environment. 

Prepare the data

Use the following code snippet to create a Pandas DataFrame object in memory:

import pandas as pd
from datetime import datetime

def str_to_date(str):
    return datetime.strptime(str, '%Y-%m-%d').date()

data = [{'DATE':str_to_date('2020-01-01'), 'TYPE': 'TypeA', 'SALES': 1000},
        {'DATE':str_to_date('2020-01-01'), 'TYPE': 'TypeB', 'SALES': 200},
        {'DATE':str_to_date('2020-01-01'), 'TYPE': 'TypeC', 'SALES': 300},
        {'DATE':str_to_date('2020-02-01'), 'TYPE': 'TypeA', 'SALES': 700},
        {'DATE':str_to_date('2020-02-01'), 'TYPE': 'TypeB', 'SALES': 400},
        {'DATE':str_to_date('2020-02-01'), 'TYPE': 'TypeC', 'SALES': 500},
        {'DATE':str_to_date('2020-03-01'), 'TYPE': 'TypeA', 'SALES': 300},
        {'DATE':str_to_date('2020-03-01'), 'TYPE': 'TypeB', 'SALES': 900},
        {'DATE':str_to_date('2020-03-01'), 'TYPE': 'TypeC', 'SALES': 100}
       ]
df = pd.DataFrame(data)
df

The content of the dataframe looks like the following:

DATETYPESALES
02020-01-01TypeA1000
12020-01-01TypeB200
22020-01-01TypeC300
32020-02-01TypeA700
42020-02-01TypeB400
52020-02-01TypeC500
62020-03-01TypeA300
72020-03-01TypeB900
82020-03-01TypeC100


We will use this dataframe to create visuals/charts.

pandas.DataFrame.plot function

Refer to the following documentation about pandas.DataFrame.plot function.

pandas.DataFrame.plot

* If you are using different version of Pandas, please navigate to the corresponded document version.

matplotlib

Plot function depends on matplotlib, please ensure you have it installed in your system. If not, you can use the following command to install it:

!pip install matplotlib

The output looks like this:


Bar chart

Use the following code to plot a bar chart:

df.plot(kind='bar', x='DATE', y='SALES')

The chart looks like the following:


Bar chart - groupby

Let's add a groupby and see how it looks like:

df.groupby(['DATE','TYPE']).sum().plot(kind='bar')

The output is slightly better as it added TYPE to X-axis. 

Bar chart - groupby and unstack

Let's unstack the dataframe after groupby.

df.groupby(['DATE','TYPE']).sum().unstack().plot(kind='bar')

The output now looks like this:

It is what we are looking for however there is a work 'None' in the legend.

To get rid of that, we just need to specify y attribute.

df.groupby(['DATE','TYPE']).sum().unstack().plot(kind='bar',y='SALES')

The chart now looks like this:

Stacked bar chart

Setting parameter stacked to True in plot function will change the chart to a stacked bar chart.

df.groupby(['DATE','TYPE']).sum().unstack().plot(kind='bar',y='SALES', stacked=True)

Cumulative stacked bar chart

To create a cumulative stacked bar chart, we need to use groupby function again:

df.groupby(['DATE','TYPE']).sum().groupby(level=[1]).cumsum().unstack().plot(kind='bar',y='SALES', stacked = True)

The chart now looks like this:

We group by level=[1] as that level is Type level as we want to accumulate sales by type.

Horizontal bar chart

To create horizontal bar charts, we just need to change chart kind to barh.

df.groupby(['DATE','TYPE']).sum().unstack().plot(kind='barh',y='SALES')


info Last modified by Raymond at 3 months ago * This page is subject to Site terms.

More from Kontext

local_offer teradata local_offer python

visibility 222
thumb_up 0
access_time 2 months ago

Pandas is commonly used by Python users to perform data operations. In many scenarios, the results need to be saved to a storage like Teradata. This article shows you how to do that easily using JayDeBeApi or  ...

open_in_new View open_in_new Spark + PySpark

local_offer python

visibility 63
thumb_up 0
access_time 2 months ago

CSV is a common data format used in many applications. It's also a common task for data workers to read and parse CSV and then save it into another storage such as RDBMS (Teradata, SQL Server, MySQL). In my previous article  ...

open_in_new View open_in_new Python Programming

local_offer teradata local_offer python local_offer Java

visibility 135
thumb_up 0
access_time 2 months ago

Python JayDeBeApi module allows you to connect from Python to Teradata databases using Java JDBC drivers. In article Connect to Teradata database through Python , I showed ho...

open_in_new View open_in_new Python Programming

local_offer pandas local_offer sqlite

visibility 43
thumb_up 0
access_time 2 months ago

In my previous posts, I showed how to use  jaydebeapi or sqlite3 pack...

open_in_new View open_in_new Python Programming

info About author

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward

Kontext Column

Created for everyone to publish data, programming and cloud related articles. Follow three steps to create your columns.


Learn more arrow_forward