如何根据值更改条形图的颜色?

时间:2019-01-22 03:02:49

标签: python pandas dataframe

我有一个iplot,其中某些值已按条件过滤。需要更改特定设置的颜色。

Example -
          values 4-6 green
          values >6 red
          Need this in IPLOT

当前图形的所有值都具有相同的颜色。

请在此处https://plot.ly/~madanraj/30

找到该图

我的代码

a=df['Woda'].value_counts()
a.iplot(asUrl=True,kind='bar',color=color,title ="KPI Report",filename='Kpi',xTitle='No of Days',yTitle='Number of Incidents')

注意:df ['Woda']的取值范围是5-200,没什么大不了了。

1 个答案:

答案 0 :(得分:0)

您可以使用matplotlib.pyplot进行以下操作:

# test dataframe
df = pd.DataFrame({'Woda':np.random.randint(5,200,100)})

plt.figure(figsize=(12,8))

# change bins to range(4,201) if it's the actual data range
n, bins, patches = plt.hist(df.Woda, bins=range(0,200))

plt.xticks(range(0,200,20))

for i in range(4,len(patches)):
    # if you want color by value_counts: 
    # plt.setp(patches[i], 'facecolor','g' if patches[i].get_height()<7 else 'r')
    plt.setp(patches[i], 'facecolor','g' if i<7 else 'r')
plt.show()

结果:

enter image description here

然后,如果您确实需要plotly数字,请使用plotly.tools进行转换。

相关问题