Pandas color bar chart based off dataframe values

I’m still rather new to Pandas, and was unable to relate similar problems asked here with what I have. I have 3 columns: country, mean, income stored in the dataframe.

I’m able to create a horizontal bar chart where countries as labeled on the Y axis, and the mean is listed on the X axis. The income field has 4 possible values, and I would like to color each bar based off those values. I’m currently plotting with this and I am not sure how to use the income field to change the color of each bar depending on the income value

df.sort_values(by=['mean']).plot.barh(x='country', y='mean')


From the docs, you can pass the optional argument color to barh()

color: str, array-like, or dict, optional

with array-like being

A sequence of color strings referred to by name, RGB or RGBA
code, which will be used for each column recursively....

So you can take your dataframe, create a list mapping each row in the plotted dataframe (including the sorting you do) to colors depending on the value of the income column and pass that as color argument.

Example: Sample dataframe

df = pd.DataFrame({'country': ["DE", "US"], 'mean': [0.1, 0.2] , 'income': ['value1', 'value2'] })
color_list = df.sort_values(by=['mean'])['income'].map('value1':'red', 'value2':'blue'}).tolist()


df.sort_values(by=['mean']).plot.barh(x='country', y='mean', color=color_list)

Output image

enter image description here