gpt4 book ai didi

Python matplotlib plot3d 轮廓长度

转载 作者:太空宇宙 更新时间:2023-11-04 05:29:43 24 4
gpt4 key购买 nike

有没有办法使用 matplotlib 获取散点图的等高线长度?我有列(x_vals、y_vals、z_vals),我想获得 z_val = z_0 的等高线长度。

最佳答案

主要思想是设置等高线的理想水平,从等高线中提取坐标并得到它的总长度。

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from scipy.interpolate import griddata

def randrange(n, vmin, vmax):
return (vmax - vmin)*np.random.rand(n) + vmin

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zl, zh)
ax.scatter(xs, ys, zs, c=c, marker=m)

# define grid
xi = np.linspace(np.min(xs),np.max(xs),n)
yi = np.linspace(np.min(ys),np.max(ys),n)
zi = griddata((xs, ys), zs, (xi[None,:], yi[:,None]), method='cubic')

# set desire levels and plot contour
levels = [(np.min(zs)+np.max(zs))/2.]
cs = plt.contour(xi,yi,zi,linewidths=1.25,colors='k', levels=levels)

# get x,y of contour lines' segments and calc contour lines length
print "x, y of contour lines ", levels
length = 0
x0,y0 = cs.allsegs[0][0][0]
startx = x0
starty = y0
print x0, y0
for coords in cs.allsegs[0][0][1:]:
x1,y1 = coords[0], coords[1]
length += np.sqrt((x1-x0)**2 + (y1-y0)**2)
x0,y0 = x1,y1
print x0,y0

length += np.sqrt((startx-x0)**2 + (starty-y0)**2)
print "length: ", length

plt.show()

enter image description here

示例输出:

x, y of contour lines  [-17.507777689387403]
23.4999959668 10.5371953741
23.5817457231 10.497649817
23.6719663171 10.4697921243
23.762186911 10.4360099894
23.852407505 10.3946824087
23.9426280989 10.3407058644
24.0328486928 10.2723811242
24.1230692868 10.1944775666
24.2132898807 10.1068535535
24.3035104746 10.0100558541
24.3937310686 9.90574448784
24.4839516625 9.79553098263
24.5741722565 9.67298314465
24.5768503134 9.66917600432
24.6643928504 9.60388531053
24.7546134443 9.52506691946
24.8448340383 9.43215297781
24.9350546322 9.32232950793
25.0252752261 9.19236277318
25.1154958201 9.03870318233
25.205716414 8.85776647714
25.2704564726 8.71121323379
25.295937008 8.68421625849
25.3861576019 8.58769144841
25.4763781958 8.48390641242
25.5665987898 8.36983000445
25.6568193837 8.23868454477
25.7470399776 8.08554200856
25.8372605716 7.90529576185
25.9035308097 7.75325046325
25.9274811655 7.71825086423
26.0177017595 7.5871801381
26.1079223534 7.45184378689
26.1981429473 7.3106545718
26.2883635413 7.16154945578
26.3785841352 7.0017709519
26.4688047291 6.82751937848
26.4848907014 6.79528769271
26.5590253231 6.59847581588
26.649245917 6.34538263269
26.739466511 6.07013541536
26.8082579554 5.83732492217
26.7438422237 4.87936215163
26.739466511 4.86265749258
26.649245917 4.53024953676
26.5590253231 4.20849894529
26.4792050487 3.92139938109
26.4688047291 3.8934269156
26.3785841352 3.6842175811
26.2883635413 3.52455851848
26.1981429473 3.39886137161
26.1079223534 3.2971242973
26.0177017595 3.21266028078
25.9274811655 3.14082302724
25.8372605716 3.07008168517
25.7470399776 2.98668089345
25.725548407 2.96343661055
25.6568193837 2.75631666794
25.5665987898 2.54190386724
25.4763781958 2.38569318308
25.3861576019 2.27114415544
25.295937008 2.18527424797
25.205716414 2.11775204404
25.1154958201 2.06220926565
25.0252752261 2.01460543416
25.0068077972 2.00547384001
length: 19.003201041

关于Python matplotlib plot3d 轮廓长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37530582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com