【问题标题】:Matplotlib, how to change the colors for heads and lines on a plot?Matplotlib,如何更改绘图上头部和线条的颜色?
【发布时间】:2020-06-16 09:38:48
【问题描述】:

我有 10 个设备通过电线连接,我使用 matplotlib.plot 为这个系统制作了一个图,其中这些设备的状态为 0。运行代码后,会出现 4 个级别的 new_status。然后我试图根据新状态绘制系统。我的问题是我无法找到改变头部和线条颜色的方法。例如,设备 1 的新状态现在是 3,因此设备 1 的头部颜色和连接到设备 6 的线的颜色应该是红色,而设备 6 应该仍然是蓝色。我尝试了 if 循环,但我无法弄清楚。

x=(1,2,3,4,5,1,2,3,4,5)
y=(1,2,3,4,5,6,7,8,9,10)
status=[0,0,0,0,0,0,0,0,0,0]
new_status=[3,0,0,2,0,0,0,0,4,1]
ax = plt.subplot()
y16 = (y[0], y[5])
x16 = (x[0], x[5])
ax.plot(y16 ,x16, marker='o', color='blue')
y28 = (y[7], y[1])
x28 = (x[7], x[1])
ax.plot(y28, x28, marker='o', color='blue')
y37 = (y[2], y[6])
x37 = (x[2], x[6])
ax.plot(y37, x37, marker='o', color='blue')
y410 = (y[3], y[9])
x410 = (x[3], x[9])
ax.plot(y410, x410, marker='o', color='blue', )
y59 = (y[4], y[8])
x59 = (x[4], x[8])
ax.plot(y59, x59, marker='o', color='blue')
plt.show()

【问题讨论】:

    标签: python-3.x matplotlib plot colormap


    【解决方案1】:

    我认为您可以绘制多次,一次用于两个头和线,然后再次用于单个头。我已经为每个新状态分配了不同的颜色,然后用不同的颜色再次绘制了单个点

    x=(1,2,3,4,5,1,2,3,4,5)
    y=(1,2,3,4,5,6,7,8,9,10)
    status=[0,0,0,0,0,0,0,0,0,0]
    new_status=[3,0,0,2,0,0,0,0,4,1]
    colours =['r', 'b', 'g', 'y', 'c'] 
    y16 = (y[0], y[5])
    x16 = (x[0], x[5])
    plt.plot(y16 ,x16, marker='o', color=colours[new_status[0]])
    plt.plot(y[5], x[5], marker='o', color=colours[new_status[5]])
    y28 = (y[7], y[1])
    x28 = (x[7], x[1])
    plt.plot(y28, x28, marker='o', color=colours[new_status[7]])
    plt.plot(y[1], x[1], marker='o', color=colours[new_status[1]])
    y37 = (y[2], y[6])
    x37 = (x[2], x[6])
    plt.plot(y37, x37, marker='o', color=colours[new_status[2]])
    plt.plot(y[6], x[6], marker='o', color=colours[new_status[6]])
    y410 = (y[3], y[9])
    x410 = (x[3], x[9])
    plt.plot(y410, x410, marker='o', color=colours[new_status[3]])
    plt.plot(y[9], x[9], marker='o', color=colours[new_status[9]])
    y59 = (y[4], y[8])
    x59 = (x[4], x[8])
    plt.plot(y59, x59, marker='o', color=colours[new_status[4]])
    plt.plot(y[8], x[8], marker='o', color=colours[new_status[8]])
    plt.show()
    

    【讨论】:

    • 谢谢!!这就是我要找的。​​span>
    猜你喜欢
    • 1970-01-01
    • 2020-06-09
    • 2013-09-08
    • 2012-01-23
    • 1970-01-01
    • 2022-01-15
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多