I have a pandas DataFrame with positive and negative values as a bar chart. How can I plot "orange" for positive values and "sky blue" for negative values?
The code when the color is not changed is:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
%matplotlib inline
# create some fake data
Value = pd.Series([-20, -15, 18, -8, 6, 7, 10, 2, 10, 4],
index=['Rent', 'Transportation', 'Bills', 'Food',
'Travel', 'Entertainment', 'Health', 'Other', 'Clothes', 'Phone'])
df = pd.DataFrame({'Value' : Value})
df = df.sort_values(by='Value')
my_range=list(range(1,len(df.index)+1))
fig, ax = plt.subplots(figsize=(5,3.5))
#color
clrs='orange'
# create for each expense type an horizontal line that starts at x = 0 with the length
# represented by the specific expense percentage value.
plt.hlines(y=my_range, xmin=0, xmax=df['Value'], color=clrs, alpha=0.2, linewidth=5)
# create for each expense type a dot at the level of the expense percentage value
plt.plot(df['Value'], my_range, "o", markersize=5, color=clrs, alpha=0.6)
I tried to change the color:
clrs = np.where(df['percentage']>0, 'orange', 'skyblue')
However I got:
ValueError: Invalid RGBA argument: array(['skyblue', 'skyblue', 'skyblue', 'orange', 'orange', 'orange',
'orange', 'orange', 'orange', 'orange'], dtype='<U7')
I have checked posts related to Invalid RGBA argument
(This, This) and how to change the color
(This, This), but they don't work.
Can anyone help?