0

質問:- Matplotlib または Seaborn を使用して、異なるスタック バーで代替色を取得する方法。たとえば、プロットに 3 つの積み上げ棒があります。最初のバーは、緑、青、黄色と表示する必要があります。2 番目のスタック バーは、オレンジ、青、赤である必要があります。3 番目のスタック バーは、青、紫、赤である必要があります。

以下のコードを書きました。スタックバーは表示されますが、上で説明した色の組み合わせは表示されません。必要のない同じ種類の色の 3 つのスタック バーをすべて取得しています。

どんな助け..

テスト プロット 3

import numpy as np
from matplotlib import pyplot as plt

num_set = [{'USA':914, 'GBR':70, 'IND':48},
           {'USA':770, 'GBR':67, 'IND':16},
           {'USA':282, 'GBR':20, 'IND':12}]

lan_guage    = [['USA','GBR','IND'], 
               ['GBR','IND','USA'], 
               ['IND','USA','GBR']] 
colors = ["r","g","b"]
names = sorted(num_set[0].keys())
values = np.array([[data[name] for name in order] for data,order in zip(num_set, lan_guage)])
lefts = np.insert(np.cumsum(values, axis=1),0,0, axis=1)[:, :-1]
orders = np.array(lan_guage)
bottoms = np.arange(len(lan_guage))

for name, color in zip(names, colors):
    idx = np.where(orders == name)
    value = values[idx]
    left = lefts[idx]
    plt.bar(left, height=0.8, width=value, bottom=bottoms, 
    color=color, orientation="horizontal", label=name)
plt.yticks(bottoms+0.4, ["Student-%d" % (t+1) for t in bottoms])

plt.legend(loc="best", bbox_to_anchor=(1.0, 1.00))
plt.subplots_adjust(right=0.75)
# Turn on the grid
plt.minorticks_on()
plt.grid(which='major', linestyle='-', linewidth='0.5', color='green')
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')

plt.show()
4

1 に答える 1