A pie chart is a circular statistical graphic, that is divided into several slices. And each slice represents a numerical proportion. For example, let’s look into the titanic dataset. The dataset contains information about the embark town of each passenger. Let’s say we want to know how many passengers embarked from each embark town. We can create a pie chart and see the numerical proportion of embark towns of passengers.
We can use the following Python code to create a pie chart of the embark towns of the passengers of the titanic ship.
import pandas
from matplotlib import pyplot
df = pandas.read_csv("titanic.csv")
print(df.info())
df.embark_town.value_counts().plot(kind="pie", label="Embark Town")
pyplot.savefig("titanic-pie-chart.png")
Here, we are using the df.embark_town.value_counts() function to get the number of passengers from each embark town (How to find the count of unique records for all the unique values in a column of a DataFrame?). After that, we are using the data to plot a pie chart.
The pie chart will look like the following:









































0 Comments