我正在尝试将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'
谢谢大家的支持。
答案 0 :(得分:0)
目前尚不清楚您想要实现的目标。示例中有一个3D图,但pcolor
是2D图。你无法在3D图中添加pcolor
图,这就是你得到错误的原因,所以添加一个新图:
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
的顺序,具体取决于您实际想要查看的内容。