0

I am trying to color each bar based on a value, as explained here

I tried a simple code, and it worked:

df=pd.DataFrame([95,94,99,90,89,91,92,85], columns=["Value"])
mask1 = df.Value < 90
mask2 = df.Value >= 90
bar=plt.bar( df.index[mask1],df.Value[mask1], color='red')
bar=plt.bar( df.index[mask2],df.Value[mask2], color='blue')
plt.hlines(90,0,7);

enter image description here

The problem is that when I apply to my dataset the order of the columns are changed. The original data is:
. %REALIZADAS MÊS JANEIRO 96 FEVEREIRO 86 MARÇO 94 ABRIL 96 MAIO 85 JUNHO 92 JULHO 96 AGOSTO 92 SETEMBRO 94 OUTOBRO 94 NOVEMBRO 97 DEZEMBRO 94

And I plotted it using
plt.bar(coleta.index, coleta['%REALIZADAS']) plt.xticks(rotation=90)
results in this chart:

enter image description here

And I tried this code:

mask1= coleta["%REALIZADAS"]>=90
mask2= coleta["%REALIZADAS"]<90
bar=plt.bar(coleta.index[mask1], coleta['%REALIZADAS'][mask1],    color="blue")
bar=plt.bar(coleta.index[mask2], coleta['%REALIZADAS'][mask2],    color="red")
plt.xticks(rotation=90)
plt.hlines(90, 0,11, color='black')

But resulted in this chart:

enter image description here

How can I keep the order of the columns?

Oalvinegro
  • 458
  • 5
  • 21

1 Answers1

1

You did not post your dataframe, but I assume that the indeces of your dataframe are not numbers but the name of each Month, which I assume is throwing off the positioning of the column as I did on purpose here:

import pandas as pd 
import matplotlib.pyplot as plt
%matplotlib inline

df=pd.DataFrame([95,94,99,90,89,91,92,85], columns=["Value"])
df.index = ["a{}".format(i) for i in range(len(df))]  #<--- my fault!

mask1 = df.Value < 90
mask2 = df.Value >= 90

bar=plt.bar( df.index[mask1],df.Value[mask1], color='red')
bar=plt.bar( df.index[mask2],df.Value[mask2], color='blue')
plt.hlines(90,0,7)

One possible solution can be this:

import pandas as pd 
import matplotlib.pyplot as plt
%matplotlib inline

df=pd.DataFrame([95,94,99,90,89,91,92,85], columns=["Value"])

df["colors"] = "red"
df["colors"].mask(df.Value >= 90, "blue", inplace=True)

plt.bar([i for i in range(len(df))], df["Value"], color=df["colors"])
plt.show()

I create another column called colors and I initialize it to all red. Then based on the condition df.Value >= 90 I set the appropriate cell colors to blue.
I pass then this to the bar plot with the color argument, and to be sure that the indeces are correct I just create a list with integers from 0 to len(df).

Hope it helps!

neko
  • 379
  • 1
  • 5
  • You are wright aboute the names at the index. I edites the question. Your example worked but did not show the names at x-label. How can I do it? – Oalvinegro Nov 07 '19 at 20:06
  • `plt.xticks(ticks=[i for i in range(len(df))], labels=["a" for i in range(len(df))])`. Obviously replace the labels with what you want – neko Nov 12 '19 at 15:17