【问题标题】:Changing color of bar plot if value is less than zero and fill white space如果值小于零并填充空白,则更改条形图的颜色
【发布时间】:2018-05-13 03:37:39
【问题描述】:

我已经创建了一个 DataFrame:

df.tail(20)
       Speed
130 -0.000272
131 -0.000219
132 -0.000178
133 -0.000234
134 -0.000261
135 -0.000281
136 -0.000244
137 -0.000255
138 -0.000290
139 -0.000210
140 -0.000216
141 -0.000209
142 -0.000139
143 -0.000060
144  0.000007
145  0.000043
146  0.000068
147  0.000093
148 -0.000025
149 -0.000005

我正在使用条形图来绘制条形图:

ind = np.arange(len(df))
ax.bar(ind, df['Speed'], width=0.5, color="r")

我想要做的是,如果值小于零,则将条形颜色更改为红色,如果值大于零,则将其更改为绿色。并显示平滑图形而不是带有空白的条形。

谢谢

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    让我们尝试使用颜色参数作为列表。

    fig,ax = plt.subplots()
    ind = np.arange(len(df))
    colormat=np.where(df['Speed']>0, 'g','r')
    ax.bar(ind, df['Speed'], width=0.5, color=colormat)
    

    输出:

    【讨论】:

    • 完美运行!非常感谢
    猜你喜欢
    • 2012-06-23
    • 2014-10-27
    • 2015-09-27
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多