Issue
I have this dataframe:
import pandas as pd
import numpy as np
df = pd.DataFrame(
data={
"Time": ['07/10/2022 08:22:44','07/10/2022 08:27:22','07/10/2022 08:27:44','07/10/2022 08:29:55','07/10/2022 08:33:14','07/10/2022 08:48:44'],
"Sum":[50,5,10,20,5,30]
}
)
and I am trying to create a barh where the index would be set to 'Time'
and the color of the bars is based on the value 'Sum'
...I was thinking something like this:
df=df.set_index('Time')
colors=[]
for val in df['Sum']:
if val <= 10:
colors.append('green')
elif val > 10 & val <=20:
colors.append('orange')
else:
colors.append('red')
ax = df.plot(kind='barh',color=colors)
but all I'm getting is the color green. What am I doing wrong?
Solution
Try this
df=df.set_index('Time')
colors=[]
for val in df['Sum']:
if val <= 10:
colors.append('green')
elif (val > 10) & (val <=20):
colors.append('orange')
else:
colors.append('red')
plt.barh(df.index, df['Sum'],color=colors)
plt.show()
this will give you what you want. Carefull with the orange condition, you need to wrap in parenthesis to do the proper boolean check
Answered By - Yuca
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.