绘制peaks函数曲面并且将它绕Z轴旋转。写出编写的程序,并将旋转后的图形进行也复制上。
时间: 2024-11-09 15:23:11 浏览: 14
在Python中,我们可以使用matplotlib库来绘制三维图形,结合numpy库计算peaks函数的值,然后利用mpl_toolkits.mplot3d模块中的Axes3D类进行旋转。下面是一个简单的示例:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Peaks function (also known as Ricker or Mexican hat wavelet)
def peaks(x, y):
return (1 - x**2 - y**2)**2 * np.exp(-(x**2 + y**2))
# Generate a grid of points
X, Y = np.meshgrid(np.linspace(-5, 5, 100), np.linspace(-5, 5, 100))
Z = peaks(X, Y)
# Create a figure and an axes object for 3D plotting
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot the surface and set some properties
surf = ax.plot_surface(X, Y, Z, cmap='viridis', linewidth=0, antialiased=False)
# Rotate the surface around the z-axis
for angle in range(0, 360, 10): # For each rotation step
ax.view_init(elev=30, azim=angle) # Set elevation and azimuth
plt.draw() # Update the plot
plt.pause(.01) # Pause for a short time to see the rotation
# Save the rotated plot
plt.savefig('rotated_peaks.png')
plt.close()
# Copy the original plot for comparison
ax.view_init(elev=30, azim=0) # Reset to initial view
plt.title("Original Peaks Function")
surf_original = ax.plot_surface(X, Y, Z, cmap='viridis', label="Original Peaks", alpha=0.7)
ax.legend()
plt.show()
阅读全文