【发布时间】: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