2013-03-05 75 views

回答

16

要結合不同類型的地塊在同一圖表中,你應該使用功能

plt.hold(真)。

下面的代碼繪製一個三維曲面圖三維散點圖:

from mpl_toolkits.mplot3d import * 
import matplotlib.pyplot as plt 
import numpy as np 
from random import random, seed 
from matplotlib import cm 


fig = plt.figure() 
ax = fig.gca(projection='3d')    # to work in 3d 
plt.hold(True) 

x_surf=np.arange(0, 1, 0.01)    # generate a mesh 
y_surf=np.arange(0, 1, 0.01) 
x_surf, y_surf = np.meshgrid(x_surf, y_surf) 
z_surf = np.sqrt(x_surf+y_surf)    # ex. function, which depends on x and y 
ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot); # plot a 3d surface plot 

n = 100 
seed(0)          # seed let us to have a reproducible set of random numbers 
x=[random() for i in range(n)]    # generate n random points 
y=[random() for i in range(n)] 
z=[random() for i in range(n)] 
ax.scatter(x, y, z);      # plot a 3d scatter plot 

ax.set_xlabel('x label') 
ax.set_ylabel('y label') 
ax.set_zlabel('z label') 

plt.show() 

結果:

http://s9.postimage.org/ge0wb8kof/3d_scatter_surface_plt.gif

你可以看到3D繪圖這裏其他一些例子:
http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

我改變了表面情節的顏色米默認的顏色表「熱」,以區分兩個地塊的顏色 - 現在,它看到表面圖覆蓋散點圖,獨立順序的...

編輯:爲了解決這個問題,應該在曲面圖的顏色表中使用透明度;添加代碼: Transparent colormap 和轉產:

ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot); # plot a 3d surface plot 

ax.plot_surface(x_surf, y_surf, z_surf, cmap=theCM); 

我們得到:

http://s16.postimage.org/5qiqn0p5h/3d_scatter_surface_plt_transparent.gif

+1

一個解決此是(用手)在散點圖分成兩半的數據(平面和下面的數據的數據段),然後繪製在適當的順序的三件事情。請參閱http://stackoverflow.com/questions/14824893/how-to-draw-diagrams-like-this/14825951#14825951 – tacaswell 2013-03-05 20:04:28

+0

如果您需要'真正'3D渲染,請從enthought調查mayavi。 http://code.enthought.com/projects/mayavi/ – tacaswell 2013-03-05 20:05:25

+1

@tcaswell我注意到,即使改變圖的順序,表面總是出現在頂部,這很奇怪...(zorder也不起作用)這就是我選擇使用透明膠片的原因。 – 2013-03-05 20:45:00

12

使用siluaty的例子;而不是通過cmap = theCM命令使用透明度,您可以調整alpha值。這可能會讓你想要什麼?

ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot, alpha=0.2) 
+0

我想知道是否有像alpha這樣的東西,你的評論很明顯! – user89 2015-04-01 18:06:36