Pandas DataFrame Plot - Bar Chart

access_time 8 months ago visibility1160 comment 0

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)

The content of the dataframe looks like the following:


We will use this dataframe to create visuals/charts.

pandas.DataFrame.plot function

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


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


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:


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

Bar chart - groupby and unstack

Let's unstack the dataframe after groupby.


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.


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.


info Last modified by Administrator at 3 months ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Want to publish your article on Kontext?

Learn more

Kontext Column

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

Learn more arrow_forward

More from Kontext

local_offer pyspark local_offer spark-2-x local_offer python local_offer spark-dataframe

visibility 3818
thumb_up 0
access_time 11 months ago

This articles show you how to convert a Python dictionary list to a Spark DataFrame. The code snippets runs on Spark 2.x environments. The input data (dictionary list looks like the following): data = [{"Category": 'Category A', 'ItemID': 1, 'Amount': 12.40}, {"Category": 'Category B' ...

local_offer python local_offer pandas local_offer python-file-operations

visibility 326
thumb_up 0
access_time 11 months ago

Pickle files are commonly used Python data related projects. This article shows how to create and load pickle files using Pandas.  import pandas as pd import numpy as np file_name="data/test.pkl" data = np.random.randn(1000, 2) # pd.set_option('display.max_rows', None) df = ...

local_offer Azure local_offer python local_offer spark local_offer pyspark

visibility 7011
thumb_up 1
access_time 2 years ago

The page summarizes the steps required to run and debug PySpark (Spark for Python) in Visual Studio Code. Install Python from the official website: . The version I am using is 3.6.4 32-bit. Pip is shipped together in this version. Download Spark 2.3.3 from ...

About column

Code snippets and tips for various programming languages/frameworks.

rss_feed Subscribe RSS