如何更改plt.plot中点的rgb颜色

问题描述 投票:0回答:2

我想以此顺序更改点的颜色(红色,绿色,蓝色)

import matplotlib.pyplot as plt
plt.plot ([0,10,20], [0, 10,20], 'or--')
plt.show ()

所有三个点都是红色,如何更改plt.plot中的点的颜色。

例如,类似

plt.plot ([0,10,20], [0, 10,20], 'o--', point1 = [255,0,0], point2 = [0,255,0], point3 = [0,0,255 ])
python matplotlib
2个回答
0
投票

[plot.plot绘制线条,如果需要颜色点,则应使用plt.scatter

# scatter takes (r,g,b) values between (0,1)
colors = np.array([[24,88,174],[255,0,0],[0,255,0]])/255

# lines
plt.plot([0,10,20], [0,10,20],'--')

# scatter has `c` option for colors
plt.scatter([0,10,20], [0, 10,20], c=colors)

输出:

enter image description here


0
投票

使用此代码,无论x和y是什么,点都将以这种模式着色:红色,绿色,蓝色:

import matplotlib.pyplot as plt

x = [0,10,20,30,35,60]
y = [0,5,40,35,15,40]

c = 'or--'
while len(x) > 0:
    plt.plot(x,y,c)
    if c == 'or--': c = 'og--'
    elif c == 'og--': c = 'ob--'
    else: c = 'or--'
    x.pop(0)
    y.pop(0)
plt.show ()

Output

© www.soinside.com 2019 - 2024. All rights reserved.