Python在3D散点图中用线连接相邻点 [英] Python joining adjacent points with line in 3D scatter plot

查看:95
本文介绍了Python在3D散点图中用线连接相邻点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两组(x,y.z)坐标,这些坐标是我用Matplotlib在3D散点图中绘制的。现在,我想将每个生成的四边形与一个平面相连。
我已经看到了如何从



我的代码:

 从mpl_toolkits.mplot3d import numpy as np 
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,线宽=无)

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.,1 600.,691.86],[2600.,1700。,709.87],[
2600.,1800。,737.83]]
data = np.asarray(data)

plot_figure(数据)


解决方案

您可以使用而不是单个多边形 plot_surface plot_wireframe 图。为了能够使用它,您需要重塑数据以构成网格。在这种情况下,这很容易,因为数据已经按有用的顺序排列。

 将numpy导入为np 
从mpl_toolkits.mplot3d导入Axes3D
导入matplotlib.pyplot as plt

#data =来自问题的数据
data = np.asarray(data)

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

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

图= plt.figure()
斧=图gca(投影='3d')

斧.plot_surface(X,Y,Z)

ax.axis('等于')
ax.axis('紧')

plt.show()



或带有

  ax.plot_wireframe(X,Y,Z)


I have got two sets of (x,y.z) coordinates which I plotted in a 3D scatter plot with Matplotlib. Now, I would like to connect each resulting quadrilateral with a plane surface. I've seen how to draw a 2D polygon in a 3D space from Plotting 3D Polygons in python-matplotlib.

What I don't know how to do is to group the points of my set into 4 points groups in order to draw polygons. I would be happy even if I managed to connect each point with its neighbours with a straight line.

My set of points lies very close to a planar distribution; but it is just a set of coordinates, there is no constraint underneath it.

My code:

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)

解决方案

Instead of individual polygons you may use a plot_surface or plot_wireframe plot. To be able to use this, you need to reshape your data to constitute a grid. In this case, this is rather easy, as the data is already in a useful order.

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()

or with

ax.plot_wireframe(X,Y,Z)

这篇关于Python在3D散点图中用线连接相邻点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆