如何在 pandas 饼图中显示值?
How to show values in pandas pie chart?
我想在饼图中可视化某个卡丁车的圈数。为了实现这一目标,我想计算按卡丁车编号分组的单圈时间。我发现有两种方法可以创建这样的饼图:
1#
df.groupby('KartNumber')['Laptime'].count().plot.pie()
2#
df.groupby(['KartNumber']).count().plot(kind='pie', y='Laptime')
打印(df)
print(df)
HeatNumber NumberOfKarts KartNumber DriverName Laptime
0 334 11 5 Monique 53.862
1 334 11 5 Monique 59.070
2 334 11 5 Monique 47.832
3 334 11 5 Monique 47.213
4 334 11 5 Monique 51.975
... ... ... ... ... ...
4053 437 2 20 luuk 39.678
4054 437 2 20 luuk 39.872
4055 437 2 20 luuk 39.454
4056 437 2 20 luuk 39.575
4057 437 2 20 luuk 39.648
不带绘图的输出:
KartNumber
1 203
10 277
11 133
12 244
13 194
14 172
15 203
16 134
17 253
18 247
19 240
2 218
20 288
21 14
4 190
5 314
6 54
60 55
61 9
62 70
63 65
64 29
65 53
66 76
67 42
68 28
69 32
8 49
9 159
None 13
如您所见,我有卡丁车号码和圈速计数。但我想在饼图(或图例)中显示圈数。我尝试使用 autopct 但无法使其正常工作。有谁知道如何实现我想要的情况?
编辑:有关此数据集的更多信息,请参阅:
通过使用如下命令:
plt.pie(values, labels=labels, autopct='%.2f')
通过以这种格式设置 autopct,它会显示图表每个部分的百分比。如果有任何问题,请分享您的结果截图。
@Mathijs 和@Danielgard
嗯,其实你可以,看我下面的例子:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(32, 8), subplot_kw=dict(aspect="equal"))
recipe = ["1 cl. Vanilla Concentrate ",
"8 cl. Orange Juice",
"8 cl. Pineapple Juice",
"4 cl. orgeat Syrup"]
data = [float(x.split()[0]) for x in recipe]
ingredients = [' '.join(x.split()[-2:]) for x in recipe]
def func(pct, allvals):
absolute = int(pct/100.*np.sum(allvals))
return "{:.1f}%\n({:d} cl.)".format(pct, absolute)
wedges, texts, autotexts = ax.pie(data, autopct=lambda pct: func(pct, data),
textprops=dict(color="w"))
ax.legend(wedges, ingredients,
title="Ingredients",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1))
plt.setp(autotexts, size=10, weight="bold")
ax.set_title("TIE-BREAK ALCOOL FREE COCKTAIL RECIPE")
plt.show()
结果:
我想在饼图中可视化某个卡丁车的圈数。为了实现这一目标,我想计算按卡丁车编号分组的单圈时间。我发现有两种方法可以创建这样的饼图:
1#
df.groupby('KartNumber')['Laptime'].count().plot.pie()
2#
df.groupby(['KartNumber']).count().plot(kind='pie', y='Laptime')
打印(df)
print(df)
HeatNumber NumberOfKarts KartNumber DriverName Laptime
0 334 11 5 Monique 53.862
1 334 11 5 Monique 59.070
2 334 11 5 Monique 47.832
3 334 11 5 Monique 47.213
4 334 11 5 Monique 51.975
... ... ... ... ... ...
4053 437 2 20 luuk 39.678
4054 437 2 20 luuk 39.872
4055 437 2 20 luuk 39.454
4056 437 2 20 luuk 39.575
4057 437 2 20 luuk 39.648
不带绘图的输出:
KartNumber
1 203
10 277
11 133
12 244
13 194
14 172
15 203
16 134
17 253
18 247
19 240
2 218
20 288
21 14
4 190
5 314
6 54
60 55
61 9
62 70
63 65
64 29
65 53
66 76
67 42
68 28
69 32
8 49
9 159
None 13
如您所见,我有卡丁车号码和圈速计数。但我想在饼图(或图例)中显示圈数。我尝试使用 autopct 但无法使其正常工作。有谁知道如何实现我想要的情况?
编辑:有关此数据集的更多信息,请参阅:
通过使用如下命令:
plt.pie(values, labels=labels, autopct='%.2f')
通过以这种格式设置 autopct,它会显示图表每个部分的百分比。如果有任何问题,请分享您的结果截图。
@Mathijs 和@Danielgard
嗯,其实你可以,看我下面的例子:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(32, 8), subplot_kw=dict(aspect="equal"))
recipe = ["1 cl. Vanilla Concentrate ",
"8 cl. Orange Juice",
"8 cl. Pineapple Juice",
"4 cl. orgeat Syrup"]
data = [float(x.split()[0]) for x in recipe]
ingredients = [' '.join(x.split()[-2:]) for x in recipe]
def func(pct, allvals):
absolute = int(pct/100.*np.sum(allvals))
return "{:.1f}%\n({:d} cl.)".format(pct, absolute)
wedges, texts, autotexts = ax.pie(data, autopct=lambda pct: func(pct, data),
textprops=dict(color="w"))
ax.legend(wedges, ingredients,
title="Ingredients",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1))
plt.setp(autotexts, size=10, weight="bold")
ax.set_title("TIE-BREAK ALCOOL FREE COCKTAIL RECIPE")
plt.show()
结果: