Don Coder
Don Coder

Reputation: 556

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

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

Upvotes: 2

Views: 2735

Answers (1)

Scott Boston
Scott Boston

Reputation: 153570

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:

enter image description here

Upvotes: 3

Related Questions