2016-09-27 100 views
2

我在這裏發現這個不錯的example用Python 2.7繪製3D數據。彩色的三維圖

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


# ====== 
## data: 

DATA = np.array([ 
    [-0.807237702464, 0.904373229492, 111.428744443], 
    [-0.802470821517, 0.832159465335, 98.572957317], 
    [-0.801052795982, 0.744231916692, 86.485869328], 
    [-0.802505546206, 0.642324228721, 75.279804677], 
    [-0.804158144115, 0.52882485495, 65.112895758], 
    [-0.806418040943, 0.405733109371, 56.1627277595], 
    [-0.808515314192, 0.275100227689, 48.508994388], 
    [-0.809879521648, 0.139140394575, 42.1027499025], 
    [-0.810645106092, -7.48279012695e-06, 36.8668106345], 
    [-0.810676720161, -0.139773175337, 32.714580273], 
    [-0.811308686707, -0.277276065449, 29.5977405865], 
    [-0.812331692291, -0.40975978382, 27.6210856615], 
    [-0.816075037319, -0.535615685086, 27.2420699235], 
    [-0.823691366944, -0.654350489595, 29.1823292975], 
    [-0.836688691603, -0.765630198427, 34.2275056775], 
    [-0.854984518665, -0.86845932028, 43.029581434], 
    [-0.879261949054, -0.961799684483, 55.9594146815], 
    [-0.740499820944, 0.901631050387, 97.0261463995], 
    [-0.735011699497, 0.82881933383, 84.971061395], 
    [-0.733021568161, 0.740454485354, 73.733621269], 
    [-0.732821755233, 0.638770044767, 63.3815970475], 
    [-0.733876941678, 0.525818698874, 54.0655910105], 
    [-0.735055978521, 0.403303715698, 45.90859502], 
    [-0.736448900325, 0.273425879041, 38.935709456], 
    [-0.737556181137, 0.13826504904, 33.096106049], 
    [-0.738278724065, -9.73058423274e-06, 28.359664343], 
    [-0.738507612286, -0.138781586244, 24.627237837], 
    [-0.738539663773, -0.275090412979, 21.857410904], 
    [-0.739099040189, -0.406068448513, 20.1110519655], 
    [-0.741152200369, -0.529726022182, 19.7019157715], 
]) 


Xs = DATA[:,0]  
Ys = DATA[:,1]  
Zs = DATA[:,2] 


## plot:  
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 

surf = ax.plot_trisurf(Xs, Ys, Zs, cmap=cm.jet, linewidth=0) 
fig.colorbar(surf) 

ax.xaxis.set_major_locator(MaxNLocator(5)) 
ax.yaxis.set_major_locator(MaxNLocator(6)) 
ax.zaxis.set_major_locator(MaxNLocator(5)) 

fig.tight_layout() 


fig.savefig('3D.png') 
plt.show()   

結果是好的:

Output

但是,可能纔有可能把這個三維地圖 「以2D」?我想只有顏色作爲Z座標的指示。就像從頂部看到這個情節一樣。 並注意,數據(以及z座標)來自測量,而不是函數。

我有大量的數據和我的電腦很慢...

+5

你看着'plt.pcolormesh'或'plt.contourf'?這是他們用法的一個例子http://matplotlib.org/examples/images_contours_and_fields/pcolormesh_levels.html – lanery

回答

3

正如在註釋中,你可以使用一個輪廓。由於您已經在使用三角測量,因此您可以使用tricontourf。看下面的例子。

import matplotlib.pyplot as plt 
import numpy as np 



## data: 

DATA = np.array([ 
    [-0.807237702464, 0.904373229492, 111.428744443], 
    [-0.802470821517, 0.832159465335, 98.572957317], 
    [-0.801052795982, 0.744231916692, 86.485869328], 
    [-0.802505546206, 0.642324228721, 75.279804677], 
    [-0.804158144115, 0.52882485495, 65.112895758], 
    [-0.806418040943, 0.405733109371, 56.1627277595], 
    [-0.808515314192, 0.275100227689, 48.508994388], 
    [-0.809879521648, 0.139140394575, 42.1027499025], 
    [-0.810645106092, -7.48279012695e-06, 36.8668106345], 
    [-0.810676720161, -0.139773175337, 32.714580273], 
    [-0.811308686707, -0.277276065449, 29.5977405865], 
    [-0.812331692291, -0.40975978382, 27.6210856615], 
    [-0.816075037319, -0.535615685086, 27.2420699235], 
    [-0.823691366944, -0.654350489595, 29.1823292975], 
    [-0.836688691603, -0.765630198427, 34.2275056775], 
    [-0.854984518665, -0.86845932028, 43.029581434], 
    [-0.879261949054, -0.961799684483, 55.9594146815], 
    [-0.740499820944, 0.901631050387, 97.0261463995], 
    [-0.735011699497, 0.82881933383, 84.971061395], 
    [-0.733021568161, 0.740454485354, 73.733621269], 
    [-0.732821755233, 0.638770044767, 63.3815970475], 
    [-0.733876941678, 0.525818698874, 54.0655910105], 
    [-0.735055978521, 0.403303715698, 45.90859502], 
    [-0.736448900325, 0.273425879041, 38.935709456], 
    [-0.737556181137, 0.13826504904, 33.096106049], 
    [-0.738278724065, -9.73058423274e-06, 28.359664343], 
    [-0.738507612286, -0.138781586244, 24.627237837], 
    [-0.738539663773, -0.275090412979, 21.857410904], 
    [-0.739099040189, -0.406068448513, 20.1110519655], 
    [-0.741152200369, -0.529726022182, 19.7019157715], 
]) 


Xs = DATA[:,0]  
Ys = DATA[:,1]  
Zs = DATA[:,2] 


## plot:  
fig = plt.figure() 
contour = plt.tricontourf(Xs, Ys, Zs, cmap="YlGnBu_r") 
fig.colorbar(contour) 
fig.savefig('3D.png') 
plt.show() 

結果是

enter image description here

+0

我該如何改變顏色。我試圖通過cmap =「rgb」來更改cmap =「YlGnBu_r」,但我沒有成功。 基本上我想顏色從紅色變爲高值變爲藍色變成最低值。 –

+1

如果您正在討論上面使用的顏色貼圖,它被稱爲「jet」或「rainbow」(Matplotlib中的「jet」),您可能需要閱讀[this](http://matplotlib.org/users/ colormaps.html)並檢查[this](http://matplotlib.org/examples/color/colormaps_reference.html)以獲取可用選項。我使用了不同的色彩映射表,因爲它對於輪廓圖的目的更好,您可以檢查[this](https://jakevdp.github.io/blog/2014/10/16/how-bad-is-your-colormap /)爲什麼噴氣機是一個不好的選擇。和[這個答案](http://scicomp.stackexchange.com/a/19559/9667),關於colormap的使用。 – nicoguaro