Pandas DataFrame Plot - Pie Chart

access_time 7 months ago visibility4765 comment 0

This article provides examples about plotting pie 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


Pie chart

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

The above code outputs the following chart:

Shadow effect

df.groupby(['TYPE']).sum().plot(kind='pie', y='SALES', shadow = True)

Start angle

df.groupby(['TYPE']).sum().plot(kind='pie', y='SALES', shadow = True, startangle=90)

Subplots (trellis)

We can also easily implement subplots/trellis charts.

Let's add derive one more column on the existing DataFrame using the following code:

df['COUNT'] = df['SALES'] /100
df

The dataframe now looks like this:

DATETYPESALESCOUNT
02020-01-01TypeA100010.0
12020-01-01TypeB2002.0
22020-01-01TypeC3003.0
32020-02-01TypeA7007.0
42020-02-01TypeB4004.0
52020-02-01TypeC5005.0
62020-03-01TypeA3003.0
72020-03-01TypeB9009.0
82020-03-01TypeC1001.0


Now we can plot the charts using the following code:

df.groupby(['TYPE']).sum().plot(kind='pie', subplots=True, shadow = True,startangle=90,figsize=(15,10))

In the above code, subplots=True parameter is used to plot charts on both SALES and COUNT metrics. The chart size is also increased using figsize parameter.

The chart now looks like the following screenshot:

Add percentage

df.groupby(['TYPE']).sum().plot(kind='pie', subplots=True, shadow = True,startangle=90,
figsize=(15,10), autopct='%1.1f%%')


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 python

visibility 379
thumb_up 0
access_time 7 months ago

In my previous article about  Convert string to date in Python / Spark , I showed how to use Spark udf to convert string to date in PySpark. Today I'm going to show you how to use pure Python function to convert string to date. datetime.datetime.strptime function is used to convert string to ...

Kafka Topic Partitions Walkthrough via Python

local_offer kafka local_offer python

visibility 173
thumb_up 0
access_time 2 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 ...

Pandas DataFrame Plot - Line Chart

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

visibility 301
thumb_up 0
access_time 7 months ago

This article provides examples about plotting line 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 SALES ...

About column

Code snippets and tips for various programming languages/frameworks.

rss_feed Subscribe RSS