Changing color of bar plot if value is less than zero and fill white space

两盒软妹~` 提交于 2021-01-28 08:16:31

问题


I have created a DataFrame:

df.tail(20)
       Speed
130 -0.000272
131 -0.000219
132 -0.000178
133 -0.000234
134 -0.000261
135 -0.000281
136 -0.000244
137 -0.000255
138 -0.000290
139 -0.000210
140 -0.000216
141 -0.000209
142 -0.000139
143 -0.000060
144  0.000007
145  0.000043
146  0.000068
147  0.000093
148 -0.000025
149 -0.000005

I am using a barchart to draw barplot:

ind = np.arange(len(df))
ax.bar(ind, df['Speed'], width=0.5, color="r")

What I am trying to do is changing bar colors to red if values are less than zero and green if they are higher than zero. And show smooth graph instead of bars with white spaces.

Thanks


回答1:


Let's try using color parameter as list.

fig,ax = plt.subplots()
ind = np.arange(len(df))
colormat=np.where(df['Speed']>0, 'g','r')
ax.bar(ind, df['Speed'], width=0.5, color=colormat)

Output:



来源:https://stackoverflow.com/questions/50309815/changing-color-of-bar-plot-if-value-is-less-than-zero-and-fill-white-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!