[英]how to print 2d data into 3d, data read from file with python
I would like to print my 2d data into 3d with python as the photo in image bellow. 我想将2d数据用python打印到3d中,如下图所示。 Currently I am reading my data from files where I have the x and y numbers on 2 columns.
目前,我正在从文件中读取数据,其中文件的x和y分别位于2列中。 Any help will be apreciated.
任何帮助将不胜感激。 The code that prints my data in 2d looks like this:
二维打印我的数据的代码如下所示:
import numpy as np
import pylab as pl
import matplotlib as mpl
data1 = np.loadtxt('NL_extb_1.xye')
data2 = np.loadtxt('NL_extb_2.xye')
data3 = np.loadtxt('NL_extb_3.xye')
data4 = np.loadtxt('NL_extb_4.xye')
data5 = np.loadtxt('NL_extb_5.xye')
pl.plot(data1[:,0], data1[:,1] , 'black')
pl.plot(data2[:,0], data2[:,1], 'black')
pl.plot(data3[:,0], data3[:,1] ,'black')
pl.plot(data4[:,0], data4[:,1] ,'black')
pl.plot(data5[:,0], data5[:,1] ,'black')
pl.xlabel("2Theta")
pl.ylabel("Counts")
pl.show()
The plot in your image looks like made in mplot3d
. 图像中的绘图看起来像是在
mplot3d
中mplot3d
。 There is an example how to do it in the tutorial : 本教程中有一个示例如何执行此操作:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05) #<-- REPLACE THIS LINE WITH YOUR DATA
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
plt.show()
Setting rstride
or cstride
to 0 will get you the plot typy you want. 将
rstride
或cstride
设置为0将使您想要的rstride
cstride
。
So now, the only thing you should do is to create the correct input variables X, Y, Z
. 因此,现在,您唯一要做的就是创建正确的输入变量
X, Y, Z
。 I assume that all your datai
have the same length, say N, and all x
columns datai[:0]
are identical . 我假设您所有的
datai
具有相同的长度,例如N,并且所有x
列datai[:0]
都相同 。 (If not, then more work is needed.) Then (如果没有,则需要更多工作。)然后
X, Y = np.meshgrid(range(1,6), data1[:,0])
# or maybe the other way:
# X, Y = np.meshgrid(data1[:,0], range(1,6))
and Z
consists of all y
columns from your data concatenated into an array of the same shape as X and Y
(all of them should be N x 5 arrays or 5 x N arrays). Z
由数据中的所有y
列组成,这些y
列连接成与X and Y
形状相同的数组(它们都应为N x 5数组或5 x N数组)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.