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?
Matplotlib API has pie() function in its pyplot module which create a pie chart representing the data in an array.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With