This code uses the histogram function instead masks.
import numpy as np
import matplotlib.pyplot as plt
# Create 300 xyz points with values ranging from 0 to 10
values = np.random.rand(300, 3) * 10
# Sort points by the Z values
sorted_values = values[np.argsort(values[:, 2])]
# Get histogram of Z values divided into 4 equal sized bins. counts
# will be the number of values in each bin. bins contains the bin
# boundaries.
counts, bins = np.histogram(sorted_values[:, 2], np.linspace(0, 10, 5))
# Plot the points in each bin
figure, axis = plt.subplots(2, 2)
start = 0
for i, count in enumerate(counts):
end = start + count
ax = axis[i // 2, i % 2]
ax.scatter(sorted_values[start:end, 0], sorted_values[start:end, 1])
ax.set_title(f"{bins[i]} < Z < {bins[i+1]}")
start = end
plt.tight_layout()
plt.show()
And the same thing using a mask.
import numpy as np
import matplotlib.pyplot as plt
values = np.random.rand(300, 3) * 10 # Make array of 300 xyz points
x, y, z = [values[:, i] for i in (0, 1, 2)] # Get x, y and z values
# Sort points into bins based on z value
figure, axis = plt.subplots(2, 2)
bins = np.linspace(0, 10, 5)
for i, (start, end) in enumerate(zip(bins, bins[1:])):
ax = axis[i // 2, i % 2]
mask = (start <= z) & (z < end)
ax.scatter(x[mask], y[mask])
ax.set_title(f"{start} <= Z < {end}")
plt.tight_layout()
plt.show()
Maybe I like the mask method better.
This is like making a 3D scatter plot, slicing the plot along the Z axis, and looking at the individual slices. It assumes the points are just points, not vertices on a line.