所以我试着在一个盒子里画出粒子的运动,并想用色条来显示速度。我的代码是这样的
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.animation import FuncAnimation
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
div = make_axes_locatable(ax)
cax = div.append_axes('right', size='15%', pad='5%')
tx = ax.set_title('Timestep 0')
def update(t):
tx.set_text('Timestep {0}'.format(t))
X,Y,Z,u,v,w=np.random.rand(6,6)
track = ax.scatter(X, Y, Z, s=1, c=np.linalg.norm([u, v, w], axis=0), cmap=plt.cm.get_cmap('inferno'))
cax.cla()
plt.colorbar(track,cax=cax)
return track, ax
if __name__ == '__main__':
anim = FuncAnimation(fig, update, frames=np.arange(10), interval=100, blit=False)
anim.save('ClusterTrajectories.gif', dpi=150, writer='imagemagick')
但是,如果我运行它,我会得到以下错误
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/home/itvmi/.local/lib/python2.7/site-packages/matplotlib/animation.py", line 1007, in save
anim._init_draw()
File "/home/itvmi/.local/lib/python2.7/site-packages/matplotlib/animation.py", line 1490, in _init_draw
self._draw_frame(next(self.new_frame_seq()))
File "/home/itvmi/.local/lib/python2.7/site-packages/matplotlib/animation.py", line 1512, in _draw_frame
self._drawn_artists = self._func(framedata, *self._args)
File "<stdin>", line 6, in update
File "/home/itvmi/.local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2262, in colorbar
ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
File "/home/itvmi/.local/lib/python2.7/site-packages/matplotlib/figure.py", line 1602, in colorbar
cb = cbar.colorbar_factory(cax, mappable, **kw)
File "/home/itvmi/.local/lib/python2.7/site-packages/matplotlib/colorbar.py", line 1347, in colorbar_factory
cb = Colorbar(cax, mappable, **kwargs)
File "/home/itvmi/.local/lib/python2.7/site-packages/matplotlib/colorbar.py", line 926, in __init__
ColorbarBase.__init__(self, ax, **kw)
File "/home/itvmi/.local/lib/python2.7/site-packages/matplotlib/colorbar.py", line 325, in __init__
self.config_axis()
File "/home/itvmi/.local/lib/python2.7/site-packages/matplotlib/colorbar.py", line 360, in config_axis
ax.yaxis.set_label_position(self.ticklocation)
File "/home/itvmi/.local/lib/python2.7/site-packages/matplotlib/axis.py", line 1811, in set_label_position
raise ValueError(msg)
ValueError: Position accepts only [ 'top' | 'bottom' ]
不幸的是,我不知道现在该怎么办,因为我不使用标签。我也见过其他例子,但它不是一个三维轴。。。 如果有人知道该做什么,那就太好了,提前谢谢你。你知道吗
我想
mpl_toolkits.axes_grid1
从来没有被测试过可以使用3D绘图。至少这里的问题来自make_axes_locatable
。如果你创建一个法线轴很好用。你知道吗
或者,为了避免重叠,可以使用
GridSpec
作为相关问题 更多 >
编程相关推荐