У меня есть следующие данные:
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
Таким образом, гистограмма должна выглядеть примерно так: 
В общем, я хочу столбчатую диаграмму, в которой в качестве значений по оси X мы имеем number
на method
каждый столбец, представляющий стек type
этого method
.
До того, как я узнал, что я могу строить графики, используя matplotlib
и pandas
, я вручную находил эти значения, а затем я строил графики так, как хотел, но теперь, когда я вижу с Пандами, вы можете получитьболее чистый, читаемый и красивый код Я хотел бы сделать это наилучшим из возможных способов.
Я попробовал:
ax = series.plot(kind='bar',
x="number",
y=["type", "method"],
legend=True)
plt.show()
Но результат не близок кчто я ищу:
