change citations to citep
[phd-thesis.git] / tiered_vs._tierless_programming / img / gen_stacked_barchart.py
1 import io
2 import matplotlib.pyplot as plt
3 import pandas as pd
4
5 components = [ 'Sensor Interface',
6 'Sensor Node',
7 'Manage Nodes',
8 'Web Interface',
9 'Database Interface',
10 'Communication'
11 ]
12
13 plt.rcParams["font.family"] = "serif"
14 plt.rcParams['mathtext.fontset'] = 'custom'
15 plt.rcParams['mathtext.rm'] = 'Libertine'
16 plt.rcParams['mathtext.it'] = 'Libertine:italic'
17 plt.rcParams['mathtext.bf'] = 'Libertine:bold'
18
19 CB_color_cycle = ['#377eb8', '#ff7f00', '#4daf4a',
20 '#f781bf', '#a65628', '#984ea3',
21 '#999999', '#e41a1c', '#dede00']
22
23 #data = '''PWS,PRS,CWS,CRS
24 #9,10,7,7
25 #32,32,5,3
26 #14,13,21,19
27 #10,10,17,18
28 #19,18,47,50
29 #17,17,3,3'''
30 data = '''PWS,PRS,CWS,CRS
31 9.252669039145907,9.895833333333333,6.626506024096386,7.096774193548387
32 31.67259786476868,31.77083333333333,5.421686746987952,2.5806451612903226
33 13.523131672597866,13.194444444444445,21.084337349397592,19.35483870967742
34 9.9644128113879,9.722222222222222,16.86746987951807,18.064516129032257
35 18.861209964412812,18.40277777777778,46.987951807228917,50.32258064516129
36 16.725978647686832,17.01388888888889,3.0120481927710843,2.5806451612903226
37 '''
38
39 # convert CSV literal string into pandas data frame
40
41 df = pd.read_csv(io.StringIO(data), sep=",", header=0)
42 df.index = components
43 print(df)
44
45 fig, ax = plt.subplots()
46
47 ax.bar(df.columns, df.loc[components[-1]], label=components[-1])
48 baseline = df.loc[components[-1]]
49 for i in range(len(components)-2, -1, -1):
50 ax.bar(df.columns, df.loc[components[i]], label=components[i],
51 bottom = baseline, color=CB_color_cycle[i])
52 baseline += df.loc[components[i]]
53 #y_offset=-15
54 y_offset=-1.2
55 for bar in ax.patches:
56 ax.text(
57 # Put the text in the middle of each bar. get_x returns the start
58 # so we add half the width to get to the middle.
59 bar.get_x() + bar.get_width() / 2,
60 # Vertically, add the height of the bar to the start of the bar,
61 # along with the offset.
62 bar.get_height() /2 + bar.get_y() + y_offset,
63 # bar.get_height() + bar.get_y() + y_offset,
64 # This is actual value we'll show.
65 round(bar.get_height()),
66 # Center the labels and style them a bit.
67 ha='center',
68 color='black',
69 weight='bold',
70 size=8
71 )
72
73 #ax.bar(labels, women_means, width, yerr=women_std, bottom=men_means,
74 # label='Women')
75
76 ax.set_ylabel('% coverage')
77 #ax.set_title('Source code coverage by component')
78
79 box = ax.get_position()
80 ax.set_position([box.x0, box.y0, box.width * 0.7, box.height])
81
82 handles, labels = plt.gca().get_legend_handles_labels()
83 ##order = list(range(len(components)))
84 order = list(range(len(components)-1, -1, -1))
85 plt.legend([handles[idx] for idx in order],[labels[idx] for idx in order], loc='center left', bbox_to_anchor=(1, 0.5))
86
87 ##ax.legend()
88
89 plt.savefig('bar_chart.pdf')
90 ##plt.show()