【问题标题】:changing the colour based on the graphs positions matplotlib根据图形位置 matplotlib 更改颜色
【发布时间】:2020-12-26 04:06:55
【问题描述】:

使用下面的数据集,我试图在 matplotlib 上绘制折线图。我正在尝试创建一个查看前一个数字并检查当前数字是否更高的函数。如果当前函数更大,它会画一条蓝线到下一个点,例如它会在(1,100) and (2,9313) 之间画一条蓝线。如果它不大于(6,203542) and (7,203542),则会画一条红线。

import matplotlib.pyplot as plt

x_long = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
L_Amount_list = [100.00, 9313.38, 43601.28, 61701.69, 74331.88, 198913.81, 153054.54, 119162.10, 74382.25, 203542.82, 160774.71, 220307.19, 366459.26]
plt.plot(x_long,L_Amount_list, color = 'green')

【问题讨论】:

    标签: python-3.x database list function matplotlib


    【解决方案1】:

    首先,为带有阈值的图表创建一个线条颜色列表。接下来,不要一次绘制一个图形,而是一次提取两个数据并设置颜色列表。

    import matplotlib.pyplot as plt
    
    x_long = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    L_Amount_list = [100.00, 9313.38, 43601.28, 61701.69, 74331.88, 198913.81, 153054.54, 119162.10, 74382.25, 203542.82, 160774.71, 220307.19, 366459.26]
    
    colors = ['b' if a < b else 'r' for a,b in zip(L_Amount_list,L_Amount_list[1:])]      
    
    for i in range(len(x_long)):
        try:
            plt.plot(x_long[i:i+2], L_Amount_list[i:i+2], color=colors[i])
        except:
            break
            
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      相关资源
      最近更新 更多