python - matplotlib 3D scatterplot with marker color corresponding to RGB values -
i have loaded picture numpy array using mahotas.
import mahotas img = mahotas.imread('test.jpg')
each pixel in img
represented array of rgb values:
img[1,1] = [254, 200, 189]
i have made 3d scatterplot of r values on 1 axis, g values on 2nd axis , b values on third axis. no problem:
fig = plt.figure() ax = fig.add_subplot(111, projection = '3d') in range(1,img.shape[1]+1): xs = img[i,1][0] ys = img[i,1][1] zs = img[i,1][2] ax.scatter(xs, ys, zs, c='0.5', marker='o') ax.set_xlabel('x label') ax.set_ylabel('y label') ax.set_zlabel('z label') plt.show()
(i'm plotting first column of image time being).
how can color each of scatterplot dots color of each image pixel? i.e. guess color dots rgb value, i'm not sure if possible?
yes, can this, needs done through separate mechanism c
argument. in nutshell, use facecolors=rgb_array
.
first off, let me explain what's going on. collection
scatter
returns has 2 "systems" (for lack of better term) setting colors.
if use c
argument, you're setting colors through scalarmappable
"system". specifies colors should controlled applying colormap single variable. (this set_array
method of inherits scalarmappable
.)
in addition scalarmappable
system, colors of collection can set independently. in case, you'd use facecolors
kwarg.
as quick example, these points have randomly specified rgb colors:
import matplotlib.pyplot plt import numpy np x, y = np.random.random((2, 10)) rgb = np.random.random((10, 3)) fig, ax = plt.subplots() ax.scatter(x, y, s=200, facecolors=rgb) plt.show()
Comments
Post a Comment