2
我有這段代碼產生一個直方圖,標識三種類型的字段; 「低」, 「中等」 和 「高」:matplotlib直方圖與圖例
import pylab as plt
import pandas as pd
df = pd.read_csv('April2017NEW.csv', index_col =1)
df1 = df.loc['Output Energy, (Wh/h)'] # choose index value and Average
df1['Average'] = df1.mean(axis=1)
N, bins, patches = plt.hist(df1['Average'], 30)
cmap = plt.get_cmap('jet')
low = cmap(0.5)
medium =cmap(0.25)
high = cmap(0.8)
for i in range(0,4):
patches[i].set_facecolor(low)
for i in range(4,11):
patches[i].set_facecolor(medium)
for i in range(11,30):
patches[i].set_facecolor(high)
plt.xlabel("Watt Hours", fontsize=16)
plt.ylabel("Households", fontsize=16)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
ax = plt.subplot(111)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.show()
產生這樣的:
如何獲得一個傳說中有三個不同的顏色?