Skip to content

Instantly share code, notes, and snippets.

@jeremysinger
Created April 7, 2022 21:57
Show Gist options
  • Save jeremysinger/b76d93647a39a44e2f7ea4e9bfb3e21d to your computer and use it in GitHub Desktop.
Save jeremysinger/b76d93647a39a44e2f7ea4e9bfb3e21d to your computer and use it in GitHub Desktop.
import io
import matplotlib.pyplot as plt
import pandas as pd
components = [ 'Sensor Interface',
'Sensor Node',
'Manage Nodes',
'Web Interface',
'Database Interface',
'Communication'
]
data = '''PWS,PRS,CWS,CRS
9,10,7,7
32,32,5,3
14,13,21,19
10,10,17,18
19,18,47,50
17,17,3,3'''
# convert CSV literal string into pandas data frame
df = pd.read_csv(io.StringIO(data), sep=",", header=0)
df.index = components
print(df)
fig, ax = plt.subplots()
ax.bar(df.columns, df.loc[components[-1]], label=components[-1])
baseline = df.loc[components[-1]]
for i in range(len(components)-2, -1, -1):
ax.bar(df.columns, df.loc[components[i]], label=components[i],
bottom = baseline)
baseline += df.loc[components[i]]
#ax.bar(labels, women_means, width, yerr=women_std, bottom=men_means,
# label='Women')
ax.set_ylabel('% coverage')
ax.set_title('Source code coverage by component')
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.7, box.height])
handles, labels = plt.gca().get_legend_handles_labels()
##order = list(range(len(components)))
order = list(range(len(components)-1, -1, -1))
plt.legend([handles[idx] for idx in order],[labels[idx] for idx in order], loc='center left', bbox_to_anchor=(1, 0.5))
##ax.legend()
plt.savefig('stacked_bar_chart.pdf')
##plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment