【问题标题】:How to give the markers in the pyplot scatter plot a custom RGB color?如何为 pyplot 散点图中的标记提供自定义 RGB 颜色?
【发布时间】:2018-12-27 10:53:14
【问题描述】:

我正在使用 matplotlib.pyplot 库中的 scatter 函数来可视化 3D 散点图中的某些数据。这个函数Link to documentation 有一个名为'c' 的参数,它可以用来给标记一个特定的颜色。

他们注意到为“c”提供的参数可以是存储在二维数组中的 RGB 代码。然而,当我尝试下面的代码时,它给出了一个错误:“AttributeError: 'list' object has no attribute 'shape'”。

我的问题:是否可以为散点图中的标记提供 RGB 格式的自定义颜色?如果是,我怎样才能做到这一点?

任何帮助将不胜感激。

编辑:解决方案:“RGB”列表中的值应介于 0 到 1 之间,而不是 0 到 255。

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import csv

RGB = [[255,255,255], [127,127,127], [10,10,10]]

r = []
g = []
b = []
a = 0
i = 0
j = 0
k = 0

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
mark = (".",",","o","v","^","<",">","1","2","3","4","8","s","p","P","*","h","H","+","x","X" ) # tuple for available markers from the matplotlib library

with open ('Color_Measurement_POCNR1_wolkjes.csv', 'r') as csvfile:
    plots = csv.reader(csvfile, delimiter = ';')
    for row in plots:
        if a == 1:
            r.append(float(row[6]))
            g.append(float(row[7]))
            b.append(float(row[8]))
            print("j = ", j, "r = ", r[j]," g = ", g[j], " b = ", b[j])
            ax.scatter(r[j], g[j], b[j], c= RGB[0], marker = mark[k] , s = 50)
            a = 0
            j += 1
            k += 1
        if row[0] == "Mean values":
            a += 1

ax.set_xlabel('R Label')
ax.set_ylabel('G Label')
ax.set_zlabel('B Label')

plt.show()

【问题讨论】:

    标签: python matplotlib scatter-plot


    【解决方案1】:

    他们注意到为“c”提供的参数可以是存储在二维数组中的 RGB 代码。

    没错。但是,在您的代码中,您使用一维列表。 您可以将此列表封装在另一个列表中以使其成为 2D,

    ax.scatter( ..., c= [RGB[0]])
    

    【讨论】:

    • 问题似乎出在其他地方,我需要将值 0 到 255 更改为从 0.0 到 1.0 的值,即浮点值。
    • 另外,我相信 RGB = [[255,255,255], [127,127,127], [10,10,10]] 确实是一个二维列表,至少根据:tutorialspoint.com/python/python_2darray.htm
    • RGB[0] 是一维的。
    猜你喜欢
    • 1970-01-01
    • 2013-06-13
    • 2018-05-20
    • 2015-06-06
    • 2020-10-27
    • 2013-01-27
    • 2019-09-21
    • 1970-01-01
    相关资源
    最近更新 更多