【问题标题】:Color using RGB value in Matplotlib在 Matplotlib 中使用 RGB 值着色
【发布时间】:2018-10-13 21:33:07
【问题描述】:

我有一个包含 X、Y、Z、R、G、B 值的数据框,我想使用这些信息绘制散点图,对于散点图中每个点的颜色,我需要从 R 中给出值, G,B。我尝试了以下代码,如果我给出静态颜色,比如 color = 'r',它就可以工作。但是当我试图给出如下代码所示的 RGB 值时,它会产生错误。

fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')

for i in range(size):
    ax.plot(pts[:,0], pts[:,1], pts[:,2], '.', color = rgba(pts[i, 3], pts[i, 4], pts[i, 5]), markersize=8, alpha=0.5)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

如何从数据框的 R、G、B 列中给出值?

所有值都是 8 位格式。

【问题讨论】:

  • 什么是RGBA?请尝试color = (pts[i, 3], pts[i, 4], pts[i, 5]),但值应在 [0,1] 范围内。 matplotlib.org/users/colors.html
  • 如果有这样的功能,我只是在徘徊。
  • 不,没有(虽然有 to_rgba)。您可以简单地将颜色指定为元组,例如颜色= (0.1, 0.2, 0.5)。
  • 我试过了,但它仍然给我错误:'numpy.float64' 类型的对象没有 len()

标签: python matplotlib colors data-visualization


【解决方案1】:

这应该可行:

pts = np.array([[1, 2, 3, 0.0, 0.7, 0.0],
                [2, 3, 4, 0.5, 0.0, 0.0]])  # example


fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')

#size = pts.shape[0]
#for i in range(size):
#    ax.plot([pts[i, 0]], [pts[i, 1]], [pts[i, 2]], '.',
#            color=(pts[i, 3], pts[i, 4], pts[i, 5]), markersize=8, #alpha=0.5)

for p in pts:
    ax.plot([p[0]], [p[1]], [p[2]], '.', color=(p[3], p[4], p[5]),  
            markersize=8, alpha=0.5)  

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

您可以通过元组(r, g, b) 指定rgb colors,其中r、g、b 是[0, 1] 范围内的浮点数。

【讨论】:

    猜你喜欢
    • 2013-01-30
    • 2020-04-29
    • 2020-12-24
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多