Pandas DataFrame Plot - Area Chart

This article provides examples about plotting area chart using pandas.DataFrame.plot or pandas.core.groupby.DataFrameGroupBy.plot function.

Prerequisites

The data I'm going to use is the same as the other article Pandas DataFrame Plot - Bar Chart. I'm also using Jupyter Notebook to plot them.

The DataFrame has 9 records:

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


Stacked area chart

import matplotlib
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter

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

date_form = DateFormatter("%Y-%m")
plt.xaxis.set_major_formatter(date_form)
plt.xaxis.set_major_locator(mdates.MonthLocator(interval=1))

The above code snippet plots a stacked area chart as the following screenshot shows:

Similar as the bar or line chart examples, major locator and formatter are used to control the formats.

Cumulative stacked area chart

import matplotlib
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter

df_unstack = df.groupby(['DATE','TYPE']).sum().groupby(level=[1]).cumsum().unstack()
plt =df_unstack.plot(kind='area',y='SALES', stacked = True, figsize=[10,5])

date_form = DateFormatter("%Y-%m")
plt.xaxis.set_major_formatter(date_form)
plt.xaxis.set_major_locator(mdates.MonthLocator(interval=1))

The chart looks like the following:


* 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