更改matplotlib中单个条的颜色?

时间:2018-09-13 08:23:34

标签: python python-3.x dictionary matplotlib

我有两个字典- 选择的候选人和被拒绝的候选人。 结构如下图所示- selected = {“ name”:score}#表示拒绝 我想以绿色显示选定的候选人,以红色显示拒绝的候选人。 我该怎么办?

我已经尝试过这种方法,但这给了我一些荒谬的结果:

#Husain Shaikh
#test 3 matplotlib
import matplotlib.pyplot as plt
selected={"Husain":92, "Asim":65,"Chirag": 74 }
rejected={"Absar":70,"premraj":57}
plt.bar(range(len(selected)),list(selected.values()),color="green")
plt.xticks(range(len(selected)),list(selected.keys()))
plt.bar(range(len(rejected)),list(rejected.values()),color="red")
plt.xticks(range(len(rejected)),list(rejected.keys()))
plt.xlabel("Candidates")
plt.ylabel("Score")
plt.plot()
plt.show()

2 个答案:

答案 0 :(得分:0)

您可以尝试类似的事情

sql.selectquery=SELECT * FROM Table  where to_char(datetime,'YYYYMMDDHH24MISS') <= :#today

答案 1 :(得分:0)

您在这里。我只显示代码的相关/修改部分

# Import numpy, matplotlib and data here

loc_s = np.arange(len(selected))+0.1 # Offsetting the tick-label location
loc_r = np.arange(len(rejected))-0.1 # Offsetting the tick-label location
xtick_loc = list(loc_s) + list(loc_r)
xticks = list(selected.keys())+ list(rejected.keys())

plt.bar(loc_s,list(selected.values()),color="green", width=0.2,label='Selected')
plt.bar(loc_r,list(rejected.values()),color="red", width=0.2,label='Rejected')
plt.xticks(xtick_loc, xticks, rotation=45)

# Labels and legend here

输出

enter image description here