LoginSignup
15
14

More than 5 years have passed since last update.

matplotlibで帯グラフ

Last updated at Posted at 2017-03-27

Excelなら一発なのでmatplotlibにも帯グラフの関数あるのかなと調べてみたけどない模様。
積み上げグラフ (stacked bar chart) の合計値が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()

Bar chart

参考:https://de.dariah.eu/tatom/topic_model_visualization.html

15
14
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14