2016-04-23 68 views
-2

我正在研究一个项目,其中的数据是从一个c代码生成的,我将其复制到一个txt文件中,我在下面给出了这个文件。我应该通过Python读取数据,因此使用matplotlib生成3D图形。我经历了许多pyhton代码,但我不知道如何从数据中计算出x,y和z轴。 我知道这是一个含糊而蹩脚的问题,但我对这一点很陌生,也很喜欢数学。使用matplotlib进行三维图形绘制

DATA.TXT

s1 s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 s12 s13 s14 s15 s16 
64m 838.4 829.2 819.0 807.5 798.9 787.5 773.9 765.3 752.9 742.0 728.3 713.3 702.2 687.2 683.2 660.3 
32m 838.3 828.7 818.5 808.5 799.9 785.9 774.4 766.8 752.8 741.0 729.6 712.9 701.2 688.6 680.3 659.1 
16m 838.5 828.1 816.8 806.8 800.2 787.8 777.0 767.6 752.7 738.0 733.3 716.8 704.2 692.8 684.9 660.2 
8m 835.5 830.3 822.3 812.4 799.8 792.1 779.6 769.8 757.5 744.8 733.2 716.4 704.2 692.2 684.7 664.6 
4m 835.5 829.9 818.7 815.1 807.4 795.5 759.0 775.2 761.8 752.3 739.2 723.8 711.6 696.4 688.5 669.0 
2m 842.5 852.1 849.0 840.9 842.5 836.0 824.8 825.9 819.1 820.5 815.5 809.8 803.8 794.7 786.5 772.7 
1024k 855.4 855.8 854.4 851.1 853.0 851.0 848.1 831.7 843.6 842.2 841.2 839.7 836.7 830.0 822.3 812.0 
512k 855.3 856.7 854.3 851.8 853.1 849.8 848.1 845.7 843.2 842.8 841.2 840.4 836.4 831.2 821.5 812.0 
256k 853.6 854.5 825.0 831.8 851.4 846.4 846.5 843.2 842.6 841.8 842.3 843.0 845.3 847.0 839.1 829.9 
128k 854.6 853.3 853.6 851.2 852.9 852.7 846.6 845.5 843.8 843.7 847.6 849.9 853.4 855.1 853.8 844.8 
64k 854.4 854.6 854.0 849.6 853.2 851.6 847.3 844.4 841.6 843.2 847.7 846.6 847.6 847.4 848.1 841.7 
32k 855.8 859.7 857.2 857.3 856.0 861.4 859.8 859.4 861.8 854.7 852.4 852.9 854.0 847.8 844.6 846.4 
16k 857.6 860.4 851.9 850.0 850.4 846.9 857.0 845.1 838.3 841.6 838.5 844.9 837.1 847.1 839.7 829.4 
8k 851.1 850.0 843.8 869.5 840.6 832.4 848.6 829.4 839.2 829.0 811.9 833.7 823.0 810.7 810.8 821.4 
4k 851.9 856.4 833.6 828.1 818.7 814.3 822.1 808.4 819.8 784.8 773.3 769.9 766.6 771.5 752.7 765.2 
2k 867.3 830.7 810.1 810.9 794.2 777.5 758.2 768.5 739.7 726.9 719.1 718.2 699.9 700.0 672.1 685.9 
1k 832.3 807.8 794.8 774.0 726.9 712.4 687.5 687.7 721.9 726.9 703.5 695.7 692.5 662.2 537.7 667.2 

回答

3

首先看看在docs。我以为你从来没有用matplotlib绘制过。让我们从基本的工作原理开始。首先,将数据格式化为python可迭代列表,如列表/数组/元组。我们还需要当然matplotlib:

# we need this to create figures 
import matplotlib.pyplot as plt 
# this is needed for 3d projections 
from mpl_toolkits.mplot3d import Axes3D 

# some chunks of your data as lists 
x = [128e3, 64e3, 32e3, 16e3, 8e3, 4e3, 2e3, 1e3] 
s1 = [854.6, 854.4, 855.8, 857.6, 851.1, 851.9, 867.3, 832.3] 
s2 = [853.3, 854.6, 859.7, 860.4, 850.0, 856.4, 830.7, 807.8] 
s3 = [853.6, 854.0, 857.2, 851.9, 843.8, 833.6, 810.1, 794.8] 

# to plot 2d data create a figure 
fig = plt.figure() 
# add a (sub)plot 
ax = fig.add_subplot(111) 
# use it to plot your 2d data 
ax.plot(x, s1) 
plt.show() 

对于3D数据也基本相同:

​​

现在的3D数据是n个点跟踪(X_0,S1_0,s2_0)至( x_n,s1_n,s2_n)通过3d空间。有很多方式来呈现您的数据(请参阅链接)。他们都基本遵循相同的语法。作为3d散点图的其他示例:

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
ax.scatter(x, s1, s2) 
plt.show() 

# a bit more tricky, we will need NumPy 
import numpy as np 

# we want to plot three graphs 
idx = np.arange(3) 
# So we need a meshgrid 
X, Y = np.meshgrid(x, idx) 
Z = np.array([s1, s2, s3]) 
# Basically X has now three 'lanes' 
# Y has 1k to 128k for each lane 
# And Z[n] has the data for lane n 

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
ax.plot_wireframe(X, Y, Z, cstride=0) 

plt.show() 
+0

我已经复制了数据,请看看。 – Fatima

+1

令人印象深刻的编辑。请仅在将来发布完整答案,因为仅用于链接的答案(如编辑之前的这一答案)不可接受。查看更多[答案]。 –