我有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()
我认为您可以绘制多次,一次绘制两个头部和线条,然后再次绘制单个头部。我为每个新状态分配了不同的颜色,然后再次以不同的颜色绘制了单个点
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()