Pandas DataFrame Plot - Line Chart

access_time 10 months ago visibility529 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 1 month 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

Follow Kontext

Get our latest updates on LinkedIn or Twitter.

Want to publish your article on Kontext?

Learn more

More from Kontext

[QlikView教程]T0 安装环境以及简单的例子
visibility 1584
thumb_up 0
access_time 8 years ago

QlikView是一款在数据可视化展示方面有很大特长的BI工具,可以提供比较全面的Dashboard的制作。本文将简单阐述此工具的安装使用,并介绍一个简单的例子。在本系列中会陆续介绍QlikView的各种功能。 QlikView官网下载地址: Downloads 。下载后按照向导安装应用程序;建议选择全部安装所有功能。 以下为Windows64位版本的初始界面: 在菜单栏有以下子菜单: File:可新建QV报表,打开远程报表,源代码控制等 View: 重置窗口大小,工具栏选择,窗口选择等 ...

Kafka Topic Partitions Walkthrough via Python
visibility 808
thumb_up 0
access_time 5 months ago

Partition is the parallelism unit in a Kafka cluster. Partitions are replicated in Kafka cluster (cluster of brokers) for fault tolerant and throughput. This articles show you how to work with Kafka partitions using Python as programming language. Package kafka-python will be used in the ...

visibility 3418
thumb_up 0
access_time 11 months ago

Python JayDeBeApi module allows you to connect from Python to databases using Java JDBC drivers.