I have the following data:
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
I read those data using 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)
Which is successful:
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
What I would like to do is to create a bar chart plot of the data:
- x-axis: distinct
numbers per method. - y-axis: number of types.
So I have the following code:
series = data_frames.groupby(["number", "method", "type"])["type"].count()
Which gives:
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
So the bar chart should look something like this:

So basically, I want a bar chart that as x-axis values we have the distinct number per method each bar representing a stack of type of that method.
Before I found out that I can plot using matplotlib and pandas I was manually finding those values and then I was plotting as I wanted, but now that I see with Pandas you can have a more clean, readable and beautiful code I would like to do it in the best way possible.
What I have tried is:
ax = series.plot(kind='bar',
x="number",
y=["type", "method"],
legend=True)
plt.show()
But the result is not near of what I am looking for:

