2017-06-14 392 views
0

我有两组(x,y.z)坐标,我用Matplotlib绘制三维散点图。现在,我想将每个产生的四边形与平面连接起来。 我已经看到如何从3D空间中绘制一个2D多边形从Plotting 3D Polygons in python-matplotlibPython在三维散点图中用线连接相邻点

我不知道该怎么做是为了绘制多边形,将我设置的点分成4个点组。即使我设法用直线将每个点与其邻居连接起来,我也会很开心。

我的一组点非常接近平面分布;但它只是一组坐标,它下面没有任何限制。 enter image description here

我的代码:

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

def plot_figure(data): 
    fig = plt.figure() 
    ax = fig.gca(projection='3d') 

    ax.scatter(data[:, 0], data[:, 1], data[:, 2], 
      c='r', s=20, linewidths=None) 

    ax.axis('equal') 
    ax.axis('tight') 

    plt.show() 


data = [[ 1900., 800., 442.82], [ 1900., 900., 463.04], [ 1900.,1000., 
473.06], [ 1900.,1100., 485.07], 
[ 1900.,1200., 498.63], [ 1900.,1300., 513.83], [ 1900.,1400., 536.1 ], [ 
1900.,1500., 551.29], 
[ 1900.,1600., 566.5 ], [ 1900.,1700., 581.65], [ 1900.,1800., 603.91], [ 
2000., 800., 453.5 ], 
[ 2000., 900., 473.75], [ 2000.,1000., 487.14], [ 2000.,1100., 499.48], [ 
2000.,1200., 513.39], 
[ 2000.,1300., 528.92], [ 2000.,1400., 551.85], [ 2000.,1500., 567.35], [ 
2000.,1600., 582.9 ], 
[ 2000.,1700., 598.4 ], [ 2000.,1800., 621.32], [ 2100., 800., 464.23], [ 
2100., 900., 485.34], 
[ 2100.,1000., 502.87], [ 2100.,1100., 515.71], [ 2100.,1200., 530.13], [ 
2100.,1300., 546.14], 
[ 2100.,1400., 570.05], [ 2100.,1500., 586.1 ], [ 2100.,1600., 602.15], [ 
2100.,1700., 618.15], 
[ 2100.,1800., 642.09], [ 2200., 800., 474.94], [ 2200., 900., 498.72], [ 
2200.,1000., 516.91], 
[ 2200.,1100., 530.09], [ 2200.,1200., 544.83], [ 2200.,1300., 561.2 ], [ 
2200.,1400., 585.8 ], 
[ 2200.,1500., 602.17], [ 2200.,1600., 618.55], [ 2200.,1700., 634.89], [ 
2200.,1800., 659.46], 
[ 2300., 800., 487.69], [ 2300., 900., 513.43], [ 2300.,1000., 532.64], [ 
2300.,1100., 546.32], 
[ 2300.,1200., 561.57], [ 2300.,1300., 578.43], [ 2300.,1400., 604.03], [ 
2300.,1500., 620.89], 
[ 2300.,1600., 637.76], [ 2300.,1700., 654.62], [ 2300.,1800., 680.23], [ 
2400., 800., 500.75], 
[ 2400., 900., 526.83], [ 2400.,1000., 546.69], [ 2400.,1100., 560.71], [ 
2400.,1200., 576.3 ], 
[ 2400.,1300., 593.52], [ 2400.,1400., 619.78], [ 2400.,1500., 636.98], [ 
2400.,1600., 654.2 ], 
[ 2400.,1700., 671.38], [ 2400.,1800., 697.66], [ 2500., 800., 516.1 ], [ 
2500., 900., 542.71], 
[ 2500.,1000., 563.6 ], [ 2500.,1100., 578.12], [ 2500.,1200., 594.18], [ 
2500.,1300., 611.89], 
[ 2500.,1400., 639.17], [ 2500.,1500., 656.87], [ 2500.,1600., 674.58], [ 
2500.,1700., 692.26], 
[ 2500.,1800., 719.53], [ 2600., 800., 530.05], [ 2600., 900., 556.98], [ 
2600.,1000., 578.51], 
[ 2600.,1100., 593.37], [ 2600.,1200., 609.77], [ 2600.,1300., 627.81], [ 
2600.,1400., 655.76], 
[ 2600.,1500., 673.82], [ 2600.,1600., 691.86], [ 2600.,1700., 709.87], [ 
2600.,1800., 737.83]] 
data = np.asarray(data) 

plot_figure (data) 
+0

如果你已经有了一个四边形网格,您可直接使用'plot_surface'。如果你有这样一个网格中的点,你可能首先要把它们带入正确的形状,这样'plot_surface'就能理解它们。如果你需要帮助,你可能会想更新你的问题并显示你的实际代码。我想这在任何情况下都比绘制单个多边形更简单。 – ImportanceOfBeingErnest

+0

我的坐标系不构成网格。他们并不躺在飞机上,即使从照片上看,他们似乎非常接近飞机。 – Argentina

+0

从你显示的情节看起来像它。无论如何,如果您想要答案,请提供您的数据和代码以显示情节。 – ImportanceOfBeingErnest

回答

1

而不是单独的多边形,你可以使用plot_surfaceplot_wireframe情节。为了能够使用它,您需要重新整理数据以构成网格。在这种情况下,这很容易,因为数据已经处于有用的顺序。

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

#data = data from the question 
data = np.asarray(data) 

a,b = len(np.unique(data[:,0])), len(np.unique(data[:,1])) 

X = data[:,0].reshape(a,b).T 
Y = data[:,1].reshape(a,b).T 
Z = data[:,2].reshape(a,b).T 

fig = plt.figure() 
ax = fig.gca(projection='3d') 

ax.plot_surface(X,Y,Z) 

ax.axis('equal') 
ax.axis('tight') 

plt.show() 

enter image description here

ax.plot_wireframe(X,Y,Z) 

enter image description here

+0

在第一种情况下是否可以标记网格线(每个四边形边界)?我想我可以叠加线框.. – Argentina

+0

你可以设置edgecolor,'ax.plot_surface(X,Y,Z,edgecolor =“limegreen”)' – ImportanceOfBeingErnest