gpt4 book ai didi

python - 具有多个值的 Pandas 系列如何正确绘制它

转载 作者:行者123 更新时间:2023-12-01 08:38:55 24 4
gpt4 key购买 nike

我有以下数据:

1, method1, 3, type1, 73.9203
2, method1, 3, type1, 38.6353
3, method1, 3, type1, 38.0158
4, method1, 3, type1, 19.6426
5, method1, 3, type1, 52.3507
6, method2, 3, type2, 500.048
7, method2, 3, type1, 14.5179
8, method2, 3, type2, 500.029
9, method2, 3, type1, 267.738
10, method2, 3, type2, 500.008
11, method1, 4, type2, 500.036
12, method1, 4, type1, 271.698
13, method1, 4, type1, 309.884
14, method1, 4, type1, 103.91
15, method1, 4, type1, 478.43
16, method2, 4, type2, 500.071
17, method2, 4, type2, 500.033
18, method2, 4, type2, 500.151
19, method2, 4, type2, 500.09
20, method2, 4, type2, 500.009

我使用 Python Pandas 读取这些数据:

import pandas
import matplotlib.pyplot as plt

data_frames = pandas.read_csv("results2.txt", sep=r',\s+', engine = "python", header=None)
data_frames.columns = ["id", "method", "number", "type", "running_time"]

print(data_frames)

哪个是成功的:

    id   method  number   type  running_time
0 1 method1 3 type1 73.9203
1 2 method1 3 type1 38.6353
2 3 method1 3 type1 38.0158
3 4 method1 3 type1 19.6426
4 5 method1 3 type1 52.3507
5 6 method2 3 type2 500.0480
6 7 method2 3 type1 14.5179
7 8 method2 3 type2 500.0290
8 9 method2 3 type1 267.7380
9 10 method2 3 type2 500.0080
10 11 method1 4 type2 500.0360
11 12 method1 4 type1 271.6980
12 13 method1 4 type1 309.8840
13 14 method1 4 type1 103.9100
14 15 method1 4 type1 478.4300
15 16 method2 4 type2 500.0710
16 17 method2 4 type2 500.0330
17 18 method2 4 type2 500.1510
18 19 method2 4 type2 500.0900
19 20 method2 4 type2 500.0090

我想做的是创建数据的条形图:

  • x 轴:不同 number每个方法。
  • y 轴:类型数量。

所以我有以下代码:

series = data_frames.groupby(["number", "method", "type"])["type"].count()

这给出:

number  method   type
3 method1 type1 5
method2 type1 2
type2 3
4 method1 type1 4
type2 1
method2 type2 5
Name: type, dtype: int64

所以条形图应该看起来像这样: enter image description here

基本上,我想要一个条形图,作为 x 轴值,我们有不同的 numbermethod每个条代表一堆 type其中method .

在我发现我可以使用 matplotlib 进行绘图之前和pandas我手动查找这些值,然后按照我想要的方式进行绘图,但现在我看到使用 Pandas,您可以拥有更干净、可读和漂亮的代码,我希望以尽可能最好的方式做到这一点。

我尝试过的是:

ax = series.plot(kind='bar',
x="number",
y=["type", "method"],
legend=True)

plt.show()

但结果并不接近我想要的:

enter image description here

最佳答案

import matplotlib.pyplot as plt

suu = series.unstack(level=1).unstack() # Unstack, so that we can stack in the plot
methods = suu.columns.get_level_values('method').unique().tolist()[::-1] # Find the groups for the grouped plot
fig, ax = plt.subplots(1, 1) # Plot everything on 1 ax-object
for i, t in enumerate(methods[::-1]): # Select group
# Stack types, plot one one ax-object
suu[t].plot(kind='bar', stacked=True, position=i, color=['g', 'r'], ax=ax,
legend=i, width=0.25) # Only show one legend, and smaller bars so the groups don't touch

您想要一个堆叠条形图,因此您首先需要取消堆叠分组。您只需要部分堆叠、部分分组的条形图。因此,选择一组中的数据帧部分。 stacked=True 堆叠类型,for 循环对方法进行分组。通过选择 1 或 0 的位置,我们可以确保它们不重叠,并且通过指定较小的宽度,我们可以确保组不直接彼此相邻。我们不需要多个图例,因此仅显示一组图例。

希望这有帮助

Output

关于python - 具有多个值的 Pandas 系列如何正确绘制它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53580696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com