【问题标题】:how change rgb color of points in plt.plot如何更改 plt.plot 中点的 rgb 颜色
【发布时间】:2020-05-21 00:05:09
【问题描述】:

我想按这个顺序改变点的颜色(红、绿、蓝)

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


    【解决方案1】:

    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)
    

    输出:

    【讨论】:

      【解决方案2】:

      使用这段代码,无论 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 ()
      

      【讨论】:

      • 如果我想要任意颜色会发生什么,例如 [24,88,174]
      • 您可以使用十六进制值:'#008000' 是绿色阴影。
      猜你喜欢
      • 1970-01-01
      • 2013-06-30
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多