所以我试图在一个盒子里绘制粒子的运动,并想用一个色条来可视化速度。我的代码如下:
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' ]
不幸的是,我不知道该怎么办,因为我不使用标签。我看过其他可行的例子,但它不是3D轴... 如果有人知道该怎么做,那就太好了,谢谢。
答案 0 :(得分:0)
我猜想mpl_toolkits.axes_grid1
尚未经过测试可用于3D图。至少这里的问题来自make_axes_locatable
。相反,如果创建法线轴,如
cax = fig.add_subplot(1,30,30)
它工作正常。
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
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
cax = fig.add_subplot(1,30,30)
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=, 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')
或者,为避免重叠,可以使用GridSpec
作为
from matplotlib.gridspec import GridSpec
gs = GridSpec(1,30)
fig = plt.figure()
ax = fig.add_subplot(gs[:-1], projection='3d')
cax = fig.add_subplot(gs[-1])