KitDocumentation

Pie

Creates a pie chart to show either categorical or range distributions / counts

Options

values: Specifies the data to use in the pie chart
bins: Specifies the number of bins to use for numerical values
where: Condition on which to filter the data
samePlot: A flag that forces multiple plots to be rendered on the same plot
sameWindow : A flag that forces multiple plots to be rendered on the same window

Examples

Example 1 - Pie Chart with Default Binning

When plotting a pie chart of a numeric column without specifying bins, a default of 5 equal-width bins is applied. This gives a quick distribution overview of the Wins column by grouping win counts into 5 ranges automatically.
#> Pie --values Wins
AFLEFT  # --bins not specified for pie chart with numeric values, default of 5 will be used

values = pd.cut(nBADf['Wins'], bins=5).value_counts()
indices = [str(index) for index in values.index]
combinedIndices = [str(indices[i]) + ' - ' + str(round(value[1] * 100 / sum(values), 1)) + '%' for i, value in enumerate(values.items())]

plt.pie(values, labels=combinedIndices)

plt.title('Wins', fontsize=14, fontweight='bold')  AFRIGHT

Example 2 - Pie Chart with Specified Number of Bins

You can increase granularity by specifying the number of bins. This example divides the Wins column into 10 evenly spaced bins to provide a more detailed breakdown of the data distribution.
#> Pie --values Wins --bins 10
AFLEFT  values_1 = pd.cut(nBADf['Wins'], bins=10).value_counts()
indices_1 = [str(index) for index in values_1.index]
combinedIndices_1 = [str(indices_1[i]) + ' - ' + str(round(value[1] * 100 / sum(values_1), 1)) + '%' for i, value in enumerate(values_1.items())]

plt.pie(values_1, labels=combinedIndices_1)

plt.title('Wins', fontsize=14, fontweight='bold')  AFRIGHT

Example 3 - Pie Chart with Custom Bin Ranges

Instead of relying on equal-sized bins, you can define custom bin ranges that reflect domain-specific thresholds. In this example, the Wins column is bucketed into defined intervals that reflect milestone win totals.
#> Pie --values Wins --bins 0 20 30 40 50 100
AFLEFT  values_2 = pd.cut(nBADf['Wins'], bins=[0, 20, 30, 40, 50, 100]).value_counts()
indices_2 = [str(index) for index in values_2.index]
combinedIndices_2 = [str(indices_2[i]) + ' - ' + str(round(value[1] * 100 / sum(values_2), 1)) + '%' for i, value in enumerate(values_2.items())]

plt.pie(values_2, labels=combinedIndices_2)

plt.title('Wins', fontsize=14, fontweight='bold')  AFRIGHT

Example 4 - Pie Chart of Categorical Values

When using a categorical column such as Team, the pie chart shows the count of each unique value. This example groups by team name and shows how frequently each team appears in the dataset. Unsurprisingly, each team has roughly the same number of players.
#> Pie --values Team
AFLEFT  values_3 = nBADf['Team'].astype('category').cat.codes.value_counts().values
indices_3 = nBADf['Team'].astype('category').cat.codes.value_counts().index
pieChartLabels = nBADf['Team'].unique()
combinedIndices_3 = [str(pieChartLabels[i]) + ' - ' + str(round(values_3[i] * 100 / sum(values_3), 1)) + '%' for i in range(len(values_3))]

plt.pie(values_3, labels=combinedIndices_3)

plt.title('Team', fontsize=14, fontweight='bold')  AFRIGHT

Example 5 - Pie Chart of Aggregated Values by Group

This example uses the group option to sum the Points for each team. The pie chart then displays total points scored by each team, allowing you to compare their overall offensive output visually.
#> Pie --values Points --group Team
AFLEFT  nBADfGroup = nBADf.groupby('Team')['Points'].sum()
nBADfGroup = nBADfGroup.sort_values()
pieLabels = [f'{name}: {value}' for name, value in zip(nBADfGroup.index, nBADfGroup)]

if (nBADfGroup <= 0).all():
    nBADfGroup *= -1

plt.pie(nBADfGroup, labels=pieLabels)

colorCycleIndex = (colorCycleIndex + 1) % len(colorCycle)

plt.title('Points', fontsize=14, fontweight='bold')  AFRIGHT