[Note] This question has poped up several times already, but does not seem to have a fix that works with the current versions. Both solutions mentioned in Define bar chart colors for pandas matplotlib with defined column and in Matplotlib Bar Chart choose color if value is positive vs value is negative do not work anymore
I want to plot a dataframe with bars and assign colors based on the value. All values below a threshold should be red, above green.
My dataframe looks something like this: A is the index, '%' the only column
A %
--------------
a 1
b 10
c 50
d 80
I want everything below 50 to be red, the rest blue. The dataframe is already in the right ascending order.
I have tried the dictionary solution for the colors described above (with adding an additional column), with no success; also a few other solutions like dividing the df into two dataframes (one with values above threshold, one with values below).
Just trying to cycle through a list of different colors does not seem to work:
df.plot(kind = 'bar', ax=axes3, color = ['r', 'g', 'b'])
all the columns come out red here.
I have tried the solution in this question, by adding a column 'colors' differentiating the values between 'red' and 'blue'.
A % colors
--------------
a 1 red
b 10 red
c 50 blue
d 80 blue
and I added this to my code:
color = df.colors.map({'red': 'r', 'blue': 'b'})
still, all my columns are red.