Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
3D points in 2D coordinates
#7
(Jun-07-2023, 05:52 PM)deanhystad Wrote: 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.

Thanks, this works for my dataset.
Reply


Messages In This Thread
3D points in 2D coordinates - by sunath - Jun-03-2023, 01:29 AM
RE: 3D points in 2D coordinates - by deanhystad - Jun-03-2023, 01:31 PM
RE: 3D points in 2D coordinates - by DPaul - Jun-04-2023, 05:35 AM
RE: 3D points in 2D coordinates - by rajeshgk - Jun-06-2023, 05:03 PM
RE: 3D points in 2D coordinates - by sunath - Jun-06-2023, 05:09 PM
RE: 3D points in 2D coordinates - by deanhystad - Jun-07-2023, 05:52 PM
RE: 3D points in 2D coordinates - by sunath - Jun-07-2023, 06:32 PM
RE: 3D points in 2D coordinates - by sunath - Jun-07-2023, 06:59 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020