Vertikale Linien vom 3D-Oberflächendiagramm zum 2D-KonturdiagrammPython

Python-Programme
Anonymous
 Vertikale Linien vom 3D-Oberflächendiagramm zum 2D-Konturdiagramm

Post by Anonymous »

Ich habe ein 3D-Oberflächendiagramm und ein 2D-Konturdiagramm in der xy-Ebene. Ich möchte ein paar vertikale Linien vom Oberflächenplot zum Computerplot erstellen (idealerweise eine vertikale Linie pro Kontur).
Probenanforderung (von hier kopiert):
Image

Allerdings gelingt es mir nicht, eine ähnliche Darstellung wie oben zu erzielen. Unten ist mein Dummy-Mindestarbeitsbeispiel, in dem ich eine rote vertikale Linie mit fest codiertem Code hinzugefügt habe, aber ich hätte gerne ähnliche vertikale Linien.

Code: Select all

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

# Create a quadratic surface
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = X**2 + Y**2  # Quadratic surface
Z = Z + 20

# Mask a circular region (e.g., radius > 2)
mask = (X**2 + Y**2) > 4
Z_masked = np.ma.masked_where(mask, Z)

# Create plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot the surface
surf = ax.plot_surface(X, Y, Z_masked, cmap=cm.viridis, edgecolor='none', alpha=0.8)

# Add contours on the XY plane at Z_min level
z_min = Z_masked.min() - 20
contour = ax.contour(X, Y, Z_masked, zdir='z', offset=z_min, colors='black', linewidths=1)

# Calculate the center of the valid region
valid_mask = ~Z_masked.mask
x_valid = X[valid_mask]
y_valid = Y[valid_mask]
z_valid = Z_masked[valid_mask]

# Find the approximate center
x_center = np.mean(x_valid)
y_center = np.mean(y_valid)
z_center = np.mean(z_valid)

# Plot the vertical line in red
ax.plot([x_center, x_center], [y_center, y_center], [z_min, z_center],
color='red', linewidth=2, alpha=0.9)

# Add markers for clarity
ax.scatter(x_center, y_center, z_center, color='red', s=50, label='Surface Center')
ax.scatter(x_center, y_center, z_min, color='red', s=50, label='Contour Center')

# Labels and legend
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
ax.legend()

# Show plot
plt.show()

print

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post