Python: Is it possible to change line color in a plot if exceeds a specific range?

后端 未结 4 864
刺人心
刺人心 2020-11-30 06:30

Is it possible to change the line color in a plot when values exceeds a certain y value? Example:

import numpy as np
import matplotlib.pyplot as plt
a = np.a         


        
4条回答
  •  一生所求
    2020-11-30 06:50

    I don't know wether there is a built-in function in matplolib. But you could convert your numpy array into a pandas series and then use the plot function in combination with boolean selection/masking.

    import numpy as np
    import pandas as pd
    
    a = np.array([1,2,17,20,16,3,5,4])
    aPandas = pd.Series(a)
    aPandas.plot()
    aPandas[aPandas > 15].plot(color = 'red')
    

提交回复
热议问题