2013-02-21 151 views
4

此問題與此有關。如何使用任意數據製作matplotlib的4d繪圖

我想知道的是如何建議的解決方案應用到一組數據(4列),例如:

0.1 0 0.1 2.0 
0.1 0 1.1 -0.498121712998 
0.1 0 2.1 -0.49973005075 
0.1 0 3.1 -0.499916082038 
0.1 0 4.1 -0.499963726586 
0.1 1 0.1 -0.0181405895692 
0.1 1 1.1 -0.490774988618 
0.1 1 2.1 -0.498653742846 
0.1 1 3.1 -0.499580747953 
0.1 1 4.1 -0.499818696063 
0.1 2 0.1 -0.0107079119572 
0.1 2 1.1 -0.483641823093 
0.1 2 2.1 -0.497582061233 
0.1 2 3.1 -0.499245863438 
0.1 2 4.1 -0.499673749657 
0.1 3 0.1 -0.0075248589089 
0.1 3 1.1 -0.476713038166 
0.1 3 2.1 -0.49651497615 
0.1 3 3.1 -0.498911427589 
0.1 3 4.1 -0.499528887295 
0.1 4 0.1 -0.00579180003048 
0.1 4 1.1 -0.469979974092 
0.1 4 2.1 -0.495452458086 
0.1 4 3.1 -0.498577439505 
0.1 4 4.1 -0.499384108904 
1.1 0 0.1 302.0 
1.1 0 1.1 -0.272727272727 
1.1 0 2.1 -0.467336140806 
1.1 0 3.1 -0.489845926622 
1.1 0 4.1 -0.495610916847 
1.1 1 0.1 -0.000154915998165 
1.1 1 1.1 -0.148803329865 
1.1 1 2.1 -0.375881358454 
1.1 1 3.1 -0.453749548548 
1.1 1 4.1 -0.478942841849 
1.1 2 0.1 -9.03765566114e-05 
1.1 2 1.1 -0.0972702806613 
1.1 2 2.1 -0.314291859842 
1.1 2 3.1 -0.422606253083 
1.1 2 4.1 -0.463359353084 
1.1 3 0.1 -6.31234088628e-05 
1.1 3 1.1 -0.0720095219203 
1.1 3 2.1 -0.270015786897 
1.1 3 3.1 -0.395462300716 
1.1 3 4.1 -0.44875793248 
1.1 4 0.1 -4.84199181874e-05 
1.1 4 1.1 -0.0571187054704 
1.1 4 2.1 -0.236660992042 
1.1 4 3.1 -0.371593983211 
1.1 4 4.1 -0.4350485869 
2.1 0 0.1 1102.0 
2.1 0 1.1 0.328324567994 
2.1 0 2.1 -0.380952380952 
2.1 0 3.1 -0.462992178846 
2.1 0 4.1 -0.48400342421 
2.1 1 0.1 -4.25137933034e-05 
2.1 1 1.1 -0.0513190921508 
2.1 1 2.1 -0.224866151101 
2.1 1 3.1 -0.363752470126 
2.1 1 4.1 -0.430700436658 
2.1 2 0.1 -2.48003822279e-05 
2.1 2 1.1 -0.0310025255124 
2.1 2 2.1 -0.158022037087 
2.1 2 3.1 -0.29944612818 
2.1 2 4.1 -0.387965424205 
2.1 3 0.1 -1.73211484062e-05 
2.1 3 1.1 -0.0220466245862 
2.1 3 2.1 -0.12162780064 
2.1 3 3.1 -0.254424041889 
2.1 3 4.1 -0.35294082311 
2.1 4 0.1 -1.32862131387e-05 
2.1 4 1.1 -0.0170828002197 
2.1 4 2.1 -0.0988138417802 
2.1 4 3.1 -0.221154587294 
2.1 4 4.1 -0.323713596671 
3.1 0 0.1 2402.0 
3.1 0 1.1 1.30503380917 
3.1 0 2.1 -0.240578771191 
3.1 0 3.1 -0.41935483871 
3.1 0 4.1 -0.465141248676 
3.1 1 0.1 -1.95102493785e-05 
3.1 1 1.1 -0.0248114638773 
3.1 1 2.1 -0.135153019304 
3.1 1 3.1 -0.274125336409 
3.1 1 4.1 -0.36965644171 
3.1 2 0.1 -1.13811197906e-05 
3.1 2 1.1 -0.0147116366819 
3.1 2 2.1 -0.0872950700627 
3.1 2 3.1 -0.202935925412 
3.1 2 4.1 -0.306612285308 
3.1 3 0.1 -7.94877050259e-06 
3.1 3 1.1 -0.0103624783432 
3.1 3 2.1 -0.0642253568271 
3.1 3 3.1 -0.160970897235 
3.1 3 4.1 -0.261906474418 
3.1 4 0.1 -6.09709039262e-06 
3.1 4 1.1 -0.00798626913355 
3.1 4 2.1 -0.0507564081263 
3.1 4 3.1 -0.133349565782 
3.1 4 4.1 -0.228563754423 
4.1 0 0.1 4202.0 
4.1 0 1.1 2.65740045079 
4.1 0 2.1 -0.0462153115214 
4.1 0 3.1 -0.358933906213 
4.1 0 4.1 -0.439024390244 
4.1 1 0.1 -1.11538537794e-05 
4.1 1 1.1 -0.0144619860317 
4.1 1 2.1 -0.0868190343718 
4.1 1 3.1 -0.203767982755 
4.1 1 4.1 -0.308519215265 
4.1 2 0.1 -6.50646078271e-06 
4.1 2 1.1 -0.0085156584289 
4.1 2 2.1 -0.0538784714494 
4.1 2 3.1 -0.140215240068 
4.1 2 4.1 -0.23746380125 
4.1 3 0.1 -4.54421180079e-06 
4.1 3 1.1 -0.00597669061814 
4.1 3 2.1 -0.038839789599 
4.1 3 3.1 -0.106675396816 
4.1 3 4.1 -0.192922262523 
4.1 4 0.1 -3.48562423225e-06 
4.1 4 1.1 -0.00459693165308 
4.1 4 2.1 -0.0303305231375 
4.1 4 3.1 -0.0860368842133 
4.1 4 4.1 -0.162420599686 

初始問題的解決方案是:

# Python-matplotlib Commands 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm 
import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure() 
ax = fig.gca(projection='3d') 
X = np.arange(-5, 5, .25) 
Y = np.arange(-5, 5, .25) 
X, Y = np.meshgrid(X, Y) 
R = np.sqrt(X**2 + Y**2) 
Z = np.sin(R) 
Gx, Gy = np.gradient(Z) # gradients with respect to x and y 
G = (Gx**2+Gy**2)**.5 # gradient magnitude 
N = G/G.max() # normalize 0..1 
surf = ax.plot_surface(
    X, Y, Z, rstride=1, cstride=1, 
    facecolors=cm.jet(N), 
    linewidth=0, antialiased=False, shade=False) 
plt.show() 

據我所見,這適用於所有matplotlib-演示,變量X,Y和Z很好地準備。實際情況並非總是如此。

想法如何重用給定的解決方案與任意數據?

+0

是它,你遇到問題的列讀取X和Y? – aaren 2013-02-21 12:03:32

+0

不,但事實是(X; Y; ​​Z)不在這個例子的網格中。 – Tengis 2013-02-21 13:00:50

回答

0

一種可能性是使用色彩空間,例如RGBA或HSVA,它們是4維的,但很好地顯示alpha(透明度)可能是個問題。

其他可能性將是一個帶滑塊的動態圖。其中一個尺寸將由滑塊表示。

但我不確定這是不是你問的。

12

偉大的問題Tengis,所有的數學家都喜歡炫耀帶有功能的華麗表面圖,而不用處理現實世界的數據。您提供的示例代碼使用漸變,因爲變量的關係是使用函數建模的。在這個例子中,我將使用標準正態分佈生成隨機數據。

反正這裏是你如何可以快速繪製與前三個變量4D隨機(任意)數據是在軸與第四之中顏色:

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 = np.random.standard_normal(100) 
y = np.random.standard_normal(100) 
z = np.random.standard_normal(100) 
c = np.random.standard_normal(100) 

ax.scatter(x, y, z, c=c, cmap=plt.hot()) 
plt.show() 

注:與熱配色方案熱圖(黃紅色)用於第四維

結果:
enter image description here