user3607022
user3607022

Reputation: 482

Python scatter plot with RGB colors

I am trying to set the colors of the points of a scatter plot to various RGB values.

Here follows a plot of points a, b and c in a 3d scatter plot.

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

a = [0, 0, 0]
b = [127, 127, 127]
c = [255, 255, 255]

colors = np.array([a, b, c]) / 255.

print(colors)

fig = plt.figure()
ax = Axes3D(fig)

ax.set_xlim3d(0, 255)
ax.set_ylim3d(0, 255)
ax.set_zlim3d(0, 255)

ax.set_xlabel("Red")
ax.set_ylabel("Green")
ax.set_zlabel("Blue")

ax.scatter(a, b, c, marker='o', s=100, facecolors=colors)
plt.show()

As I understand it, the RGB color argument facecolors of ax.scatter is between 0 and 1, not between 0 and 255, which is why I divide by 255. Output from print(colors), i.e the argument to facecolors, is as follows:

[[0.         0.         0.        ]
 [0.49803922 0.49803922 0.49803922]
 [1.         1.         1.        ]]

The output chart seems to only show one scatter point:

Output

It seems to me the plot should show three points with three colors: white, black and something in between.

Upvotes: 1

Views: 1330

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54698

Right. You seem to be thinking that you're providing 3 separate points there, but that's not right. You're providing 3 x values (0,0,0), 3 y values (127,127,127), and 3 z values (255,255,255). You provided 3 identical points.

I assume you want a=[0,127,255], b=[0,127,255] and c=[0,127,255].

Upvotes: 1

Related Questions