Matplotlib pcolor 用作"2D plot in 3D"时不起作用



我正试图在这里给出的示例代码中添加一个pcolor图http://matplotlib.org/examples/mplot3d/2dcollections3d_demo.html

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'

感谢大家的支持。

目前还不清楚您想要实现什么。示例中有一个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的顺序。