【问题标题】:Error: Change the color of a bar if condition is True matplotlib错误:如果条件为 True matplotlib,则更改条形的颜色
【发布时间】:2020-10-27 09:58:44
【问题描述】:

我有一个带有正负值的 pandas DataFrame 作为条形图。 如何为正值绘制“橙色”,为负值绘制“天蓝色”?

不改变颜色时的代码是:

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)

我尝试改变颜色:

clrs = np.where(df['percentage']>0, 'orange', 'skyblue')

但是我得到了:

ValueError: Invalid RGBA argument: array(['skyblue', 'skyblue', 'skyblue', 'orange', 'orange', 'orange',
       'orange', 'orange', 'orange', 'orange'], dtype='<U7')

我查看了与Invalid RGBA argument(This, This) 和how to change the color(This, This) 相关的帖子,但它们不起作用。
有人可以帮忙吗?

【问题讨论】:

    标签: python dataframe matplotlib colors


    【解决方案1】:

    您可以将数据框拆分为正值和负值并分别绘制:

    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')
    
    # Separate the dataframe into positive and negative values
    df_pos = df[(df>0)]
    df_neg= df[(df<0)]
    
    my_range_pos=list(range(1,len(df_pos.index)+1))
    my_range_neg=list(range(1,len(df_neg.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.
    # Plot positive
    plt.hlines(y=my_range_pos, xmin=0, xmax=df_pos['Value'], color='orange', alpha=0.2, linewidth=5)
    # Plot negative
    plt.hlines(y=my_range_neg, xmin=0, xmax=df_neg['Value'], color='blue', alpha=0.2, linewidth=5)
    
    # create for each expense type a dot at the level of the expense percentage value
    # Plot positive
    plt.plot(df_pos['Value'], my_range_pos, "o", markersize=5, color='orange', alpha=0.6)
    # Plot negative
    plt.plot(df_neg['Value'], my_range_neg, "o", markersize=5, color='blue', alpha=0.6)
    

    结果:

    【讨论】:

    • 这就是我想做的。感谢您的帮助!
    • @mimi 乐于助人!如果这对您有用,您能否将我的答案标记为已接受? :)
    • 哎呀,我不习惯。谢谢你告诉我!
    猜你喜欢
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 2020-04-12
    • 2011-08-03
    • 2012-01-19
    相关资源
    最近更新 更多