简体   繁体   中英

Pie chart - How to represent just one value of a column?

Goal - Can we represent just one value on the pie chart? For example, I have a column titled 'Severity' and it has 500 values in total and contains multiple values 'CAT 1' & 'CAT 2' and individual count are as follows (CAT 1 - 484 & and CAT2 -16)

My question is can I represent on PIE chart just one value? (ie) only CAT 1 which has 475 values (please see the below screenshot attached for more

在此处输入图像描述

How to have actual values in matplotlib Pie Chart displayed - I think this has what you are looking for.

eg

import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame()
df["Severity_Values"] = ["CAT1","CAT2"]
df["counts"] = [484,16]
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.axis('equal')


p, tx, autotexts = ax.pie(df.counts,radius=1.8,labels=df.Severity_Values,autopct='%1.2f%%',textprops={'fontsize':14},data='Text')
for i, a in enumerate(autotexts):
    if i == 0:
        a.set_text(f"Changed Text \n{a.get_text()}")
    else:
        a.set_text("")

for i, a in enumerate(tx):
    if i == 0:
        a.set_text(f"Changed Text \n{a.get_text()}")
    else:
        a.set_text("")
        
plt.show()

Of course the if statement could be conditional on a match with df data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM