Pandas DataFrame Plot - Area Chart

access_time 8 months ago visibility174 comment 0

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:


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

Pandas DataFrame Plot - Pie Chart

local_offer plot local_offer pandas local_offer jupyter-notebook local_offer python local_offer pandas-plot

visibility 4817
thumb_up 0
access_time 8 months ago

This article provides examples about plotting pie chart using  pandas.DataFrame.plot  function. 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: DATE TYPE ...

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 = ...

Pandas DataFrame Plot - Bar Chart

local_offer plot local_offer pandas local_offer python local_offer jupyter-notebook local_offer pandas-plot

visibility 1160
thumb_up 0
access_time 8 months ago

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.  ...

About column

Code snippets and tips for various programming languages/frameworks.

rss_feed Subscribe RSS