Pyplot - change color of line if data is less than zero?

前端 未结 3 443
耶瑟儿~
耶瑟儿~ 2020-12-28 22:24

I am trying to figure out if there is anything built into pyplot that will change the color of my line depending on whether or not the data is negative or positive. For exam

3条回答
  •  旧时难觅i
    2020-12-28 23:06

    You can conditionally plot data in your axes object, using a where like syntax (if you're used to something like Pandas).

    ax.plot(x[f(x)>=0], f(x)[f(x)>=0], 'g')
    ax.plot(x[f(x)<0],  f(x)[f(x)<0],  'r')
    

    Technically, it's splitting and plotting your data in two sets, but it's fairly compact and nice.

提交回复
热议问题