matplotlib 三维曲面中非均匀数据的绘制

qoefvg9y  于 2023-05-01  发布在  其他
关注(0)|答案(2)|浏览(106)

我有一个简单的代码来绘制点在3D Matplotlib如下-

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

my_data = genfromtxt('points1.csv', delimiter=',')
points1X = my_data[:,0]
points1Y = my_data[:,1]
points1Z = my_data[:,2]

## I remove the header of the CSV File.
points1X = np.delete(points1X, 0)
points1Y = np.delete(points1Y, 0)
points1Z = np.delete(points1Z, 0)

# Convert the array to 1D array
points1X = np.reshape(points1X,points1X.size)
points1Y = np.reshape(points1Y,points1Y.size)
points1Z = np.reshape(points1Z,points1Z.size)

my_data = genfromtxt('points2.csv', delimiter=',')
points2X = my_data[:,0]
points2Y = my_data[:,1]
points2Z = my_data[:,2]
## I remove the header of the CSV File.
points2X = np.delete(points2X, 0)
points2Y = np.delete(points2Y, 0)
points2Z = np.delete(points2Z, 0)

# Convert the array to 1D array
points2X = np.reshape(points2X,points2X.size)
points2Y = np.reshape(points2Y,points2Y.size)
points2Z = np.reshape(points2Z,points2Z.size)

ax.plot(points1X, points1Y, points1Z, 'd', markersize=8, markerfacecolor='red', label='points1')
ax.plot(points2X, points2Y, points2Z, 'd', markersize=8, markerfacecolor='blue', label='points2')

plt.show()

我的问题是,我试图使一个体面的表面图出这些数据点,我有。我已经尝试使用ax.plot_surface()函数来使它看起来很好。为此,我删除了一些点,并重新计算了该函数所需的矩阵类型的输入。然而,我生成的图表更难解释和理解。所以可能有两种可能:要么是我没有正确地使用函数,要么是我试图绘制的数据对曲面图不好。
我所期待的是3D图形,它将产生类似于3D饼图的效果。我们看到,一个片段(被提取出来的)是另一个片段的一部分。我没想到它会和那个完全一样,但有某种类似的效果。
我想问的是:
1.你认为有可能制作这样的3D图形吗?
1.有没有更好的方法,我可以在三维空间中表达我的数据?
这是两个文件-
points1.csv

Dim1,Dim2,Dim3
3,8,1
3,8,2
3,8,3
3,8,4
3,8,5
3,9,1
3,9,2
3,9,3
3,9,4
3,9,5
3,10,1
3,10,2
3,10,3
3,10,4
3,10,5
3,11,1
3,11,2
3,11,3
3,11,4
3,11,5
3,12,1
3,12,2
3,13,1
3,13,2
3,14,1
3,14,2
3,15,1
3,15,2
3,16,1
3,16,2
3,17,1
3,17,2
3,18,1
3,18,2
4,8,1
4,8,2
4,8,3
4,8,4
4,8,5
4,9,1
4,9,2
4,9,3
4,9,4
4,9,5
4,10,1
4,10,2
4,10,3
4,10,4
4,10,5
4,11,1
4,11,2
4,11,3
4,11,4
4,11,5
4,12,1
4,13,1
4,14,1
4,15,1
4,16,1
4,17,1
4,18,1
5,8,1
5,8,2
5,8,3
5,8,4
5,8,5
5,9,1
5,9,2
5,9,3
5,9,4
5,9,5
5,10,1
5,10,2
5,10,3
5,10,4
5,10,5
5,11,1
5,11,2
5,11,3
5,11,4
5,11,5
5,12,1
5,13,1
5,14,1
5,15,1
5,16,1
5,17,1
5,18,1
6,8,1
6,8,2
6,8,3
6,8,4
6,8,5
6,9,1
6,9,2
6,9,3
6,9,4
6,9,5
6,10,1
6,11,1
6,12,1
6,13,1
6,14,1
6,15,1
6,16,1
6,17,1
6,18,1
7,8,1
7,8,2
7,8,3
7,8,4
7,8,5
7,9,1
7,9,2
7,9,3
7,9,4
7,9,5

点2。csv

Dim1,Dim2,Dim3
3,12,3
3,12,4
3,12,5
3,13,3
3,13,4
3,13,5
3,14,3
3,14,4
3,14,5
3,15,3
3,15,4
3,15,5
3,16,3
3,16,4
3,16,5
3,17,3
3,17,4
3,17,5
3,18,3
3,18,4
3,18,5
4,12,2
4,12,3
4,12,4
4,12,5
4,13,2
4,13,3
4,13,4
4,13,5
4,14,2
4,14,3
4,14,4
4,14,5
4,15,2
4,15,3
4,15,4
4,15,5
4,16,2
4,16,3
4,16,4
4,16,5
4,17,2
4,17,3
4,17,4
4,17,5
4,18,2
4,18,3
4,18,4
4,18,5
5,12,2
5,12,3
5,12,4
5,12,5
5,13,2
5,13,3
5,13,4
5,13,5
5,14,2
5,14,3
5,14,4
5,14,5
5,15,2
5,15,3
5,15,4
5,15,5
5,16,2
5,16,3
5,16,4
5,16,5
5,17,2
5,17,3
5,17,4
5,17,5
5,18,2
5,18,3
5,18,4
5,18,5
6,10,2
6,10,3
6,10,4
6,10,5
6,11,2
6,11,3
6,11,4
6,11,5
6,12,2
6,12,3
6,12,4
6,12,5
6,13,2
6,13,3
6,13,4
6,13,5
6,14,2
6,14,3
6,14,4
6,14,5
6,15,2
6,15,3
6,15,4
6,15,5
6,16,2
6,16,3
6,16,4
6,16,5
6,17,2
6,17,3
6,17,4
6,17,5
6,18,2
6,18,3
6,18,4
6,18,5
7,10,1
7,10,2
7,10,3
7,10,4
7,10,5
7,11,1
7,11,2
7,11,3
7,11,4
7,11,5
7,12,1
7,12,2
7,12,3
7,12,4
7,12,5
7,13,1
7,13,2
7,13,3
7,13,4
7,13,5
7,14,1
7,14,2
7,14,3
7,14,4
7,14,5
7,15,1
7,15,2
7,15,3
7,15,4
7,15,5
7,16,1
7,16,2
7,16,3
7,16,4
7,16,5
7,17,1
7,17,2
7,17,3
7,17,4
7,17,5
7,18,1
7,18,2
7,18,3
7,18,4
7,18,5
oalqel3c

oalqel3c1#

我不明白你的问题在哪里,除了你需要网格化你的数据。以下是来自points1.csv的数据:

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

fig = plt.figure(figsize=plt.figaspect(0.5))
ax = fig.add_subplot(1, 2, 1, projection='3d')
# note this: you can skip rows!
my_data = np.genfromtxt('file1.csv', delimiter=',',skiprows=1)
X = my_data[:,0]
Y = my_data[:,1]
Z = my_data[:,2]

xi = np.linspace(X.min(),X.max(),100)
yi = np.linspace(Y.min(),Y.max(),100)
# VERY IMPORTANT, to tell matplotlib how is your data organized
zi = griddata((X, Y), Z, (xi[None,:], yi[:,None]), method='cubic')

CS = plt.contour(xi,yi,zi,15,linewidths=0.5,color='k')
ax = fig.add_subplot(1, 2, 2, projection='3d')

xig, yig = np.meshgrid(xi, yi)

surf = ax.plot_surface(xig, yig, zi,
        linewidth=0)

plt.show()


希望这对你有帮助。

vtwuwzda

vtwuwzda2#

在我的情况下,Oz123建议的程序遇到了
TypeError:genfromtxt()获取了意外的关键字参数“skiprows”
所以我将'skiprows'关键字参数切换为'skip_header',它就像一个魅力。

python -c "import numpy as np;print np.version.version;"
1.14.3
python --version
Python 2.7.15rc1

相关问题