【问题标题】:How can I change the linecolor if y-value changes如果 y 值发生变化,如何更改线条颜色
【发布时间】:2016-06-22 12:05:11
【问题描述】:

我正在使用 pylab 绘制一些图表 假设我想绘制这个:

x = [0,1,2,3,4,5,6,7,8,9,10]
y = [0,0,0,1,1,0,0,1,0,0,0]

plt.plot(x,y,'g')
plt.show()

但是每次 [y = 1] 我都想将线的颜色更改为红色。

这可能吗?

【问题讨论】:

标签: python python-3.x matplotlib


【解决方案1】:

灵感来自this answer:

from matplotlib import collections  as mc
from matplotlib import pyplot as plt

x = [0,1,2,3,4,5,6,7,8,9,10]
y = [0,0,0,1,1,0,0,1,0,0,0]

def getLines(points):
    lines = []
    lastX, lastY = points[0]
    for x,y in points[1:]:
        lines.append([(lastX,lastY), (lastX+1,lastY)])
        if y!=lastY:
            lines.append( [(x, lastY), (x,y)] ) 
        lastX, lastY = (x,y)
    return lines    

def getColor(point0, point1):
    x0,y0 = point0
    x1,y1 = point1
    return "r" if (y1==y0) and (y1==1) else "g"

points = [(i,j) for i,j in zip(x,y)]
lines = getLines(points)
colors = [getColor(*line) for line in lines]


lc = mc.LineCollection(lines, colors=colors, linewidths=2)
fig, ax = plt.subplots()
ax.add_collection(lc)
ax.autoscale()
ax.margins(0.1)

输出:

【讨论】:

    【解决方案2】:

    @michael_j_ward (+1) 的好回答。只是为了使用普通绘图命令提供替代方案,这也是一种可能的解决方案。

    您需要考虑的第一件事是,您需要根据现有数据转换数据:

    x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    y = [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0]
    

    进入你想要的情节:

    import matplotlib.pyplot as plt
    
    x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    y = [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0]
    nx, ny = [x[0]], [y[0]]
    
    for i in range(0, len(y)-1):
        nx.append(x[i])
        ny.append(y[i])
        if y[i] == 0 and y[i+1] == 1:
            nx.append(x[i])
            ny.append(y[i+1])
        elif y[i] == 1 and y[i+1] == 0:
            nx.append(x[i+1])
            ny.append(y[i])
    nx.append(x[-1])
    ny.append(y[-1])
    
    
    plt.plot(x, y, c='blue', label='Your data')
    plt.plot(nx, ny, c='black', linestyle='--', label='New data')
    plt.ylim(-1, 2)
    plt.yticks([0, 1], ['0', '1'])
    plt.legend()
    plt.show()
    

    ,其比较如下:

    使用新数据你可以做到:

    import matplotlib.pyplot as plt
    
    x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    y = [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0]
    nx, ny = [x[0]], [y[0]]
    
    for i in range(0, len(y)-1):
        nx.append(x[i])
        ny.append(y[i])
        if y[i] == 0 and y[i+1] == 1:
            nx.append(x[i])
            ny.append(y[i+1])
        elif y[i] == 1 and y[i+1] == 0:
            nx.append(x[i+1])
            ny.append(y[i])
    nx.append(x[-1])
    ny.append(y[-1])
    
    for i in range(1,len(ny)):
        if ny[i] == 1 and ny[i-1] == 1:
            choice = 'r'
        else:
            choice = 'g'
        plt.plot([nx[i-1], nx[i]], [ny[i-1], ny[i]], c=choice)
    
    plt.ylim(-1, 2)
    plt.yticks([0, 1], ['0', '1'])
    plt.grid()
    plt.show()
    

    ,结果如下:

    【讨论】:

      猜你喜欢
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      相关资源
      最近更新 更多