如果值小于零,则更改条形图的颜色并填充空白 [英] Changing color of bar plot if value is less than zero and fill white space

查看:46
本文介绍了如果值小于零,则更改条形图的颜色并填充空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个数据帧:

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.

谢谢

推荐答案

让我们尝试使用颜色参数作为列表.

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)

输出:

这篇关于如果值小于零,则更改条形图的颜色并填充空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆