2017-07-26 59 views
0

enter image description here我是编程初学者。我从2个月前开始,所以请耐心等待我:-) 所以我想用python 3.4上的matplotlib制作3d表面图。matplotlib 3d - 插入数据

我看了很多关于这个的教程,但我没有找到完全像我需要做的事情..我希望你能帮助我。 在他们给出的所有视频meshgrid 3轴(x,y,z)之间的关系,但我不想这样。我想要做的是这样的:我有16个传感器,他们被放在4行4传感器中的每一个都是1,2,3,4和第二个5,6,7,8等等(传感器的顺序非常重要),例如,来自skala的传感器编号4 = 200从0到800 ..我认为只使用x和y轴为图中的正确位置。例如与传感器4(= 800从800)被放置在第四列的第一行...所以。 .x = 4,y = 1和z = 200,从800开始,所以最后每个传感器只有一个'真实'值..z ..

如何导入这种数据与matplotlib for所有16个传感器做出3d图?我真的很感激任何形式的帮助..

+2

你可以尝试清理你的解释?我没有跟随。线的含义:'从0到800的skala传感器编号4 = 200' –

+1

你首先必须了解你有什么样的数据,你有三个一维数组,三个元素的元组,......? –

+0

我的意思是z可以取值从0到800,在这个例子中它是400. – GeorgM

回答

2

你需要从某处开始。所以我们假设这些数据是16个值的列表。然后,您可以创建它的二维数组,并将该数组显示为图像。

import numpy as np 
import matplotlib.pyplot as plt 

# input data is a list of 16 values, 
# the first value is of sensor 1, the last of sensor 16 
input_data = [200,266,350,480, 
       247,270,320,511, 
       299,317,410,500, 
       360,360,504,632] 
# create numpy array from list and reshape it to a 4x4 matrix 
z = np.array(input_data).reshape(4,4) 
# at this point you can already show an image of the data 
plt.imshow(z) 
plt.colorbar() 

plt.show() 

enter image description here

一个选项,以现在绘制值高度3D绘图,而不是颜色在2D情节将使用bar3d情节。

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

# input data is a list of 16 values, 
# the first value is of sensor 1, the last of sensor 16 
input_data = [200,266,350,480, 
       247,270,320,511, 
       299,317,410,500, 
       360,360,504,632] 

# create a coordinate grid 
x,y = np.meshgrid(range(4), range(4)) 

ax = plt.gcf().add_subplot(111, projection="3d") 
#plot the values as 3D bar graph 
# bar3d(x,y,z, dx,dy,dz) 
ax.bar3d(x.flatten(),y.flatten(),np.zeros(len(input_data)), 
     np.ones(len(input_data)),np.ones(len(input_data)),input_data) 

plt.show() 

enter image description here

您也可以绘制表面图,但在这种情况下,电网将定义面瓷砖的边缘。

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

# input data is a list of 16 values, 
# the first value is of sensor 1, the last of sensor 16 
input_data = [200,266,350,480, 
       247,270,320,511, 
       299,317,410,500, 
       360,360,504,632] 

# create a coordinate grid 
x,y = np.meshgrid(range(4), range(4)) 
z = np.array(input_data).reshape(4,4) 

ax = plt.gcf().add_subplot(111, projection="3d") 
#plot the values as 3D surface plot 
ax.plot_surface(x,y,z) 

plt.show() 

enter image description here

+0

非常感谢你!!帮了我很多..真的!但不是使用bar3d plot ..也可以用这些数据制作3d曲面图?谢谢 – GeorgM

+0

是的,但它不太可读,看着情节,谁会猜测这绘制了16个不同的传感器数据?当然这是你的选择。 – ImportanceOfBeingErnest