Home > database >  How to plot a pie chart with different values inside a column of a CSV file in python
How to plot a pie chart with different values inside a column of a CSV file in python

Time:04-05

I am trying to plot a pie chart with regards to a selected column of floats. I have a column I want to work on and the pie chart is supposed to represent the ratio of each of the different values.

I have not tried anything yet as I am not sure how to to implement it yet

CodePudding user response:

Okay, so you can create a pie plot in Python using Matplotlib and Seaborn. In matplotlib there's a function pie() that can help you in creating pie plots. The function takes in 1 compulsory parameter that is by the name x.

For this parameter, you have to pass in the counts of the different things inside your main list.


For example, consider a list with 200 apples and 200 bananas. In this case, the count of each item can be stored inside a list as [200,200]. So running the code below would give me a pieplot with 200 apples and 200 bananas.

count = [200, 200]

plt.figure(dpi = 120)
plt.pie(x = count)
plt.show()

If you want the counts of both items to be plotted with their corresponding portions you can use the labels parameter.

count = [37, 14]

plt.figure(dpi = 120)
plt.pie(x = count, labels = [37, 14])
plt.show()

CodePudding user response:

Easy Here you go

Python using Matplotlib and Pandas

data.csv

    month_number,facecream,facewash,toothpaste,bathingsoap,shampoo,moisturizer,total_units,total_profit
1,2500,1500,5200,9200,1200,1500,21100,211000
2,2630,1200,5100,6100,2100,1200,18330,183300
3,2140,1340,4550,9550,3550,1340,22470,224700
4,3400,1130,5870,8870,1870,1130,22270,222700
5,3600,1740,4560,7760,1560,1740,20960,209600
6,2760,1555,4890,7490,1890,1555,20140,201400
7,2980,1120,4780,8980,1780,1120,29550,295500
8,3700,1400,5860,9960,2860,1400,36140,361400
9,3540,1780,6100,8100,2100,1780,23400,234000
10,1990,1890,8300,10300,2300,1890,26670,266700
11,2340,2100,7300,13300,2400,2100,41280,412800
12,2900,1760,7400,14400,1800,1760,30020,300200

use matplotlib's

pie()

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('path to data.csv')
slices =
[df.facecream.sum(),df.facewash.sum(),df.toothpaste.sum(),df.bathingsoap.sum(),df.s
hampoo.sum(),df.moisturizer.sum()]
label =
["FaceCream","FaceWash","ToothPaste","BathingSoap","shampoo","Moisturizer"]
plt.pie(slices,
 labels=label,
 startangle=150,
 shadow=False,
 explode=(0,0,0,0,0,0),
 autopct='%1.1f%%')
plt.title("Total Sale Data For Last Year For Each Product")
plt.legend(loc='lower right')
plt.show()

enter image description here

  • Related