Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I plot a pie chart using Pandas with this data

Tags:

python

pandas

I have a data like this

ID   Sex   Smoke
1  female    1 
2    male    0
3   female   1

How do I plot a pie chart to show how many male or female smokes?

like image 322
toy Avatar asked Jul 19 '15 07:07

toy


People also ask

Which method is used to plot a pie chart in Python?

Matplotlib API has pie() function in its pyplot module which create a pie chart representing the data in an array.

How do I visualize data with pandas?

As Pandas is Python's popular data analysis library, it provides several different functions to visualizing our data with the help of the . plot() function. There is one more advantage of using Pandas for visualization is we can serialize or create a pipeline of data analysis functions and plotting functions.

Can you plot graphs with pandas?

Pandas uses the plot() method to create diagrams. We can use Pyplot, a submodule of the Matplotlib library to visualize the diagram on the screen.


1 Answers

You can plot directly with pandas selecting pie chart:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'Sex': ['female', 'male', 'female'], 'Smoke': [1, 3, 1]})

df.Smoke.groupby(df.Sex).sum().plot(kind='pie')
plt.axis('equal')
plt.show()

enter image description here

like image 51
pcu Avatar answered Oct 05 '22 06:10

pcu