2016-05-13 104 views
0

使用我想一個令pColor劇情添加到比原來的腳本時,這裏給出http://matplotlib.org/examples/mplot3d/2dcollections3d_demo.htmlmatplotlib令pColor爲「3D 2D情節」

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

fig = plt.figure() 
ax = fig.gca(projection='3d') 

x = np.linspace(0, 1, 100) 
y = np.sin(x * 2 * np.pi)/2 + 0.5 
ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z') 

colors = ('r', 'g', 'b', 'k') 
for c in colors: 
    x = np.random.sample(20) 
    y = np.random.sample(20) 
    ax.scatter(x, y, 0, zdir='y', c=c) 

xc = np.linspace(0, 1, 100) 
yc = np.linspace(0, 1, 100) 
fc = np.random.random((len(xc),len(yc))) 
ax.pcolor(xc,yc,fc, zdir = 'x') 

ax.legend() 
ax.set_xlim3d(0, 1) 
ax.set_ylim3d(0, 1) 
ax.set_zlim3d(0, 1) 

plt.show() 

只是一些新行示例代碼時不工作。不幸的是,它並不好,我真的不明白爲什麼。

Traceback (most recent call last): 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/artist.py", line 61, in draw_wrapper 
    draw(artist, renderer, *args, **kwargs) 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/figure.py", line 1159, in draw 
    func(*args) 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 271, in draw 
    for col in self.collections] 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 271, in <listcomp> 
    for col in self.collections] 
AttributeError: 'PolyCollection' object has no attribute 'do_3d_projection' 

謝謝大家的支持。

回答

0

目前尚不清楚你想實現什麼。示例中有一個3D圖,但pcolor是2D圖。你可以不pcolor劇情添加到3D圖,這就是爲什麼你得到錯誤,所以添加一個新的身影:

fig = plt.figure() 
ax = fig.gca(projection='3d') 

x = np.linspace(0, 1, 100) 
y = np.sin(x * 2 * np.pi)/2 + 0.5 
ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z') 

colors = ('r', 'g', 'b', 'k') 
for c in colors: 
    x = np.random.sample(20) 
    y = np.random.sample(20) 
    ax.scatter(x, y, 0, zdir='y', c=c) 

ax.legend() 
ax.set_xlim3d(0, 1) 
ax.set_ylim3d(0, 1) 
ax.set_zlim3d(0, 1) 


fig = plt.figure() 
xc = np.linspace(0, 1, 100) 
yc = np.linspace(0, 1, 100) 
fc = np.random.random((len(xc),len(yc))) 
plt.pcolor(xc,yc,fc,cmap='RdBu') 
plt.colorbar() 

pcolor有沒有屬性zdir,所以你需要調整的xc,yc,fc的順序正確的方式,取決於你真正想看到什麼。

+0

感謝您的貢獻,但這不是我正在尋找的。我會更好地解釋。如果您嘗試matplotlib網站中的示例腳本,您會看到ax.plot和ax.scatter被繪製爲三維座標平面上的投影。我也想爲ax.pcolor做這件事(而不是像你建議的那樣使用一個新圖形)。 –