defplot_multi_barh(records=np.ones((4, 4)),
label=np.array(['label-{}'.format(x) for x in np.arange(4)]),
width=0.2,
alpha=0.7,
figsize=(20, 10),
xlim=1,
ylim=0.3):"""
Plot Multi barh in a figure.
:param records: Data Matrix.
:param label: Label Array
:param width: the width of each bar.
:param alpha: Color alpha.
:param figsize: Figure size.
:param xlim:
:param ylim:
:return: None
"""
ind = np.arange(len(label))
fig, ax = plt.subplots(figsize=figsize)
ax_array = []
for i in range(records.shape[0]):
ax_array.append(ax.barh(ind + i * width, records[i], width, color=colors[i], alpha=alpha))
ax.set_yticks(ind + 2 * width)
ax.set_yticklabels(label)
plt.ylim(-1, len(ind) + ylim)
plt.xlim(0, np.max(records) + xlim)
ax.legend(([each[0] for each in ax_array]), label, loc='best')
plt.show()
Plot Stack Bar
defplot_stack_bar(records=np.ones(shape=(4, 5)),
legend_labels=np.array(['color-{}'.format(x) for x in np.arange(5)]),
index_label=np.array(['index-{}'.format(x) for x in np.arange(4)]),
width=0.4,
alpha=0.3,
ylabel='ylabel',
xlabel='xlabel',
title='title',
figsize=(24, 10),
normalize=False):defnorm_data(records):for i in range(records.shape[0]):
total = np.sum(records[i][:])
for j in range(records.shape[1]):
records[i][j] /= total
if normalize:
norm_data(records)
fig = plt.figure(figsize=figsize, facecolor="white")
ax = fig.add_subplot(1, 1, 1)
ind = np.arange(records.shape[0])
axs_array = []
bottom_array = np.zeros(records.shape[0])
for i in range(records.shape[1]):
axs_array.append(ax.bar(ind,
records[:, i],
width=width,
color=colors[i],
bottom=bottom_array,
align='center',
alpha=alpha
))
bottom_array = bottom_array + records[:, i]
# for i in range(records.shape[0]):# for j in range(records.shape[1]):# plt.text(i, np.sum(records[i, 0: (j + 1)]),# round(records[i][j], 2),# ha="center")
plt.ylabel('{}'.format(ylabel), fontsize=20)
plt.title('{}'.format(title), fontsize=20)
plt.xlabel('{}'.format(xlabel), fontsize=20)
plt.xticks(ind, index_label, rotation=40)
plt.legend(([each[0] for each in axs_array]),
legend_labels,
loc='best')
plt.show()
Plot Pie
defplot_pie(data=np.ones(10),
labels=['index-{}'.format(x) for x in np.arange(10)]):
sizes = [360 * each / np.sum(data) for each in data]
plt.pie(sizes,
labels=labels,
autopct='%1.1f%%',
startangle=0,
colors=[each for each in colors[:data.shape[0]]])
plt.axis('equal')
plt.show()