2017-07-26 56 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