I have loaded a picture into a numpy array using mahotas.
import mahotas
img = mahotas.imread('test.jpg')
Each pixel in img
is represented by an array of RGB values:
img[1,1] = [254, 200, 189]
I have made a 3D scatterplot of R values on one axis, G values on the 2nd axis and B values on the third axis. This is no problem:
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
for i 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 just plotting the first column of the image for the time being).
How can I color each of the scatterplot dots by the color of each image pixel? i.e. I guess I would like to color the dots by their RGB value, but I'm not sure if this is possible?
Matplotlib recognizes the following formats to specify a color. RGB or RGBA (red, green, blue, alpha) tuple of float values in a closed interval [0, 1]. Case-insensitive hex RGB or RGBA string. Case-insensitive RGB or RGBA string equivalent hex shorthand of duplicated characters.
scatter( x , y , sz , c ) specifies the circle colors. You can specify one color for all the circles, or you can vary the color. For example, you can plot all red circles by specifying c as "red" .
All of the line properties can be controlled by keyword arguments. For example, you can set the color, marker, linestyle, and markercolor with: plot(x, y, color='green', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=12).
Use matplotlib. pyplot. scatter() to make a colored scatter plot. Using lists of corresponding x and y coordinates and a list colors , create a list color_indices that assigns each coordinate the index of a color from colors .
Yes, you can do this, but it needs to be done through a separate mechanism than the c
argument. In a nutshell, use facecolors=rgb_array
.
First off, let me explain what's going on. The Collection
that scatter
returns has two "systems" (for lack of a better term) for setting colors.
If you use the c
argument, you're setting the colors through the ScalarMappable
"system". This specifies that the colors should be controlled by applying a colormap to a single variable. (This is the set_array
method of anything that inherits from ScalarMappable
.)
In addition to the ScalarMappable
system, the colors of a collection can be set independently. In that case, you'd use the facecolors
kwarg.
As a quick example, these points will have randomly specified rgb colors:
import matplotlib.pyplot as plt
import numpy as 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With