Pandas DataFrame Plot - Line Chart

access_time 7 months ago visibility296 comment 0

This article provides examples about plotting line chart using pandas.DataFrame.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


Line chart plot

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

The output of the plotting:

Change marker

The following code snippet changes marker to circle. Refer to matplotlib documentation about all the options you could choose. 

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

Cumulative line chart

Similar as the bar chart plotting, we can also plot a cumulative line chart.

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

X-axis labels

In the above charts, X-axis labels are very crowded. There are multiple ways to fix it. One of the approaches is to use formatter and also set major locator. 

The code snippet looks like the following

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='line',y='SALES', marker='o', stacked = True)

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

Chart looks like this:

Similarly for the non-cumulative one, you can also set up the major locator:

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='line',y='SALES', marker='o', stacked = True)

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

Output looks like the following screenshot:

info Last modified by Raymond at 2 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 kafka local_offer python

visibility 94
thumb_up 0
access_time 2 months ago

Apache Kafka is written with Scala. Thus, the most natural way is to use Scala (or Java) to call Kafka APIs, for example, Consumer APIs and Producer APIs. For Python developers, there are open source packages available that function similar as official Java clients.  This article shows you ...

local_offer Java local_offer python local_offer SQL Server

visibility 1861
thumb_up 0
access_time 7 months ago

In my previous article  Connect to SQL Server via JayDeBeApi in Python , I showed examples of using Python package jaydebeapi to connect to SQL Server via JDBC. The code was working properly. However after I reinstall Python 3.8.2 x64 version on Windows, I'm getting a new error ...

local_offer python local_offer spark

visibility 987
thumb_up 0
access_time 12 months ago

This code snippet shows how to convert string to date.

About column

Code snippets and tips for various programming languages/frameworks.

rss_feed Subscribe RSS