Since it is one shot in Excel, I tried to find out if matplotlib also has a band graph function, but it seems that it is not. Normalized and displayed so that the total value of the stacked bar chart is 1.
import numpy as np
import matplotlib.pyplot as plt
N, K = 4, 3
data = np.random.rand(N, K)
tick_labels = ["a", "b", "c", "d"]
labels = ["x", "y", "z"]
normalized = data / data.sum(axis=1, keepdims=True)
cumulative = np.zeros(N)
tick = np.arange(N)
for k in range(K):
color = plt.cm.viridis(float(k) / K, 1)
plt.barh(tick, normalized[:, k], left=cumulative, color=color, label=labels[k])
# plt.bar(tick, normalized[:, k], bottom=cumulative, color=color, label=labels[k])
cumulative += normalized[:, k]
plt.xlim((0, 1))
# plt.ylim((0, 1))
plt.yticks(tick, tick_labels)
# plt.xticks(tick, tick_labels)
plt.legend()
plt.show()
Reference: https://de.dariah.eu/tatom/topic_model_visualization.html
Recommended Posts