A bar chart (bar graph) is used to present categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally. In this post, I will generate a bar graph with error bars using Python module ‘matplotlib‘. The Python module ‘Matplotlib’ has a function ‘bar()‘ to plot a bar chart using the given data. The plot will show different classification results of a machine learning model. The results are based on 11 iterations, so I am using error bars to show the max and min values of those parameters.
import matplotlib.pyplot as plt
import numpy as np
# data for plot
ydata = [[0.04282655246252677, 0.028907922912205567, 0.05246252676659528, 0.048179871520342615, 0.047109207708779445,
0.03961456102783726, 0.034261241970021415, 0.033190578158458245, 0.04389721627408994, 0.034261241970021415,
0.034261241970021415],
[0.0006116651421517226, 0.0007027642058764473, 0.0006730175728234761, 0.0006060876484542905,
0.0006804542310867188,
0.0006767359019550973, 0.0007027642058764473, 0.0005930734964936156, 0.0006302567878098298,
0.0006469892689021262,
0.0006451301043363155],
[0.8280554192686864, 0.8311494921416902, 0.8253914300049019, 0.8250685862004675, 0.8251054376880019,
0.8204736956300389, 0.825287261393621, 0.826532181089316, 0.8269853296269783, 0.8231537753035891,
0.8187248663524915],
[0.021936864633493848, 0.025254146602461208, 0.0236490101658641, 0.021401819154628143, 0.024933119315141788,
0.02557517388978063, 0.024612092027822365, 0.021615837346174425, 0.023434991974317816, 0.024398073836276082,
0.02397003745318352],
[0.00023461253928341007, 0.000268669198211647, 0.00026677716160452273, 0.00023839661249765862,
0.00025164086874752853, 0.00023650457589053433, 0.00028002141785439267, 0.00022136828303354013,
0.00022704439285491296, 0.00022704439285491296, 0.00023272050267628578],
[0.9997653874607166, 0.9997313308017883, 0.9997332228383955, 0.9997616033875023, 0.9997483591312525,
0.9997634954241095, 0.9997199785821456, 0.9997786317169665, 0.9997729556071451, 0.9997729556071451,
0.9997672794973237],
[0.6231003039513677, 0.6243386243386243, 0.6104972375690608, 0.6134969325153374, 0.6366120218579235,
0.6565934065934066, 0.6084656084656085, 0.6332288401253918, 0.6460176991150443, 0.6551724137931034,
0.6455331412103746],
[0.11468946297144221, 0.12319078176124912, 0.11780444944391164, 0.11235503912343653, 0.12366939589961216,
0.12731040952111153, 0.11996916255848895, 0.11482033149491708, 0.1208252186162181, 0.12420236484009181,
0.12214925623529795],
[0.982996835625536, 0.9830529601970612, 0.9830255584040601, 0.982987629057762, 0.9830477572510279,
0.9830589828692197, 0.9830417973648274, 0.9829915711264108, 0.9830225640281506, 0.9830390230834487,
0.9830316131780797]]
x = np.array([i for i in range(len(ydata))])
y = []
yerr = []
for v in ydata:
y.append(np.mean(v))
yerr.append([np.mean(v) - min(v), max(v) - np.mean(v)])
yerr = np.transpose(yerr) # change to 2xN matrix
# plot bar chart
fig, ax = plt.subplots()
ax.bar(x, y, yerr=yerr, align='center', capsize=10)
params = ['RR', 'C1', 'auc', 'tpr', 'fpr', 'tnr', 'ppv', 'mcc', 'npv']
ax.set_xticks(x)
ax.set_xticklabels(params)
ax.set_xlabel('parameters')
ax.set_ylabel('value')
ax.grid(b=True, which='major', axis='both', color='0.25', linestyle='-')
ax.grid(b=True, which='minor', axis='both', color='0.45', linestyle='--')
ax.minorticks_on()
plt.title('Classification results')
plt.show()