z不作为x&y函数的曲面的Python 3D绘图

2024-06-25 23:06:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想画一些3D曲面。给定的x和y为极坐标,z为高度值。我首先变换x&;y到笛卡尔坐标。 用x、y和z绘制散点图效果很好,但为了进一步分析,最好绘制一些曲面图

我知道我必须使用plot_曲面(X,Y,Z),但我不能以所需的形式引入Z

我的代码如下:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import math
 

def polar_to_cartesian(ih, r):
    x_range = []
    y_range = []
 
    for i in range(len(ih)):
        x_range.append(ih[i] * math.cos((math.pi / 180) * r[i]))
        y_range.append(ih[i] * math.sin((math.pi / 180) * r[i]))

    return x_range, y_range

radius = [0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.751, 0.751, 0.751, 0.751, 0.751, 0.751, 0.751, 0.751, 1.251, 1.251, 1.251, 1.251, 1.251, 1.251, 1.251, 1.251, 1.501, 1.501, 1.501, 1.501, 1.501, 1.501, 1.501, 1.501, 2.002, 2.002, 2.002, 2.002, 2.002, 2.002, 2.002, 2.002, 2.252, 2.252, 2.252, 2.252, 2.502, 2.502, 2.502, 2.502]
degree = [0, 22.5, 157.5, 202.5, 337.5, 67.5, 112.5, 247.5, 292.5, 22.5, 157.5, 202.5, 337.5, 67.5, 112.5, 247.5, 292.5, 22.5, 157.5, 202.5, 337.5, 67.5, 112.5, 247.5, 292.5, 22.5, 157.5, 202.5, 337.5, 67.5, 112.5, 247.5, 292.5, 22.5, 157.5, 202.5, 337.5, 67.5, 112.5, 247.5, 292.5, 22.5, 157.5, 202.5, 337.5, 22.5, 157.5, 202.5, 337.5]

x, y = polar_to_cartesian(radius, degree)
z_hor = [0, -1.23, -2.89, 2.62, -1.305, -5.75, -8.975, -1.35, -1.975, -2.675, -6.925, 2.81, -3.47, -6.49, -13.1, -0.18, -3.4, -6.735, -11.02, 2.705, -6.31, -9.96, -18.535, -0.785, -3.735, -11.64, -17.855, 0.47, -12.46, -15.195, -28.37, -2.1, -7.49, -12.46, -18.54, 2.85, -11.97, -15.08, -10.215, -10.28, -19.045, -4.26, -15.405, -6.24, -1.965, -14.78, -19.235, 3.36, -13.695]
z_ver = [0, -3.075, -1.715, 3.995, 0.22, -4.41, -3.855, 3, 2.42, -6.015, -1.435, 2.64, 1.31, -8.385, -5.055, 0.825, 3.465, -7.44, -1.76, 2.695, 2.81, -12.61, -7.97, 0.575, 6.14, -15.2, -4.595, -1.23, 0.485, -20.855, -13.245, -3.89, 4.805, -12.92, -0.18, 2.345, 5.41, -18.59, -17.825, -10.485, 6.88, -1.665, -1.9, -2.14, -0.51, -14.245, 1.78, 9.575, 8.33] 

X,Y = np.meshgrid(x, y)

fig = plt.figure(figsize=(6,6))
ax1 = fig.add_subplot(111, projection='3d')

ax1.scatter(x, y, z_hor)
ax1.scatter(x, y, z_ver)

ax1.set_xlabel('x [mm]')
ax1.set_ylabel('y [mm]')
ax1.set_zlabel('z[µm]')


 
plt.show()

有人能给我一个如何进行的提示吗? 谢谢


Tags: toimportasnp绘制rangepltmath