0

I have a function to plot 3D scatter plots, it works fine but I don't see how I can give a color to specific data points based on a condition for example:

in The following code I am plotting 3 features; nbActionsD30, avgActionsMonth and actionSHR.

I want to give a specific color to the data points where actionsSHR value >= 50

the parameters of the function are f1, f2, f3 the feature names. data is the dataframe that contains the features.

here is my function's code :

def plot3D(f1, f2, f3, data):

%matplotlib widget
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

xs = data[f1]
ys = data[f2]
zs = data[f3]

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

plot = ax.scatter(xs, ys, zs, s=50, color = 'blue', edgecolors = "white")
ax.set_xlabel(f1)
ax.set_ylabel(f2)
ax.set_zlabel(f3)
plt.show()

plot3D("avgActionsMonth", "nbActionsD30", "actionSHR", data)

screenshot link

BigBen
  • 46,229
  • 7
  • 24
  • 40
  • `c=list_of_values_to_color_by` (see [here](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.html#mpl_toolkits.mplot3d.axes3d.Axes3D.scatter)) – tomjn Jul 07 '22 at 16:14
  • @tomjn the c parameter only defines the colors I want to use (list of colors) to mark points, my problem is that I want to mark specific data points where actionsSHR >= 50 with a distinct color – hnajjar Jul 07 '22 at 18:29

1 Answers1

0
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(subplot_kw=dict(projection="3d"))
x, y, z = np.random.normal(size=(3, 1000))
ax.scatter(x, y, z, c=z>1)
plt.show()

gives

enter image description here

tomjn
  • 5,100
  • 1
  • 9
  • 24