3D GIF中的colorbar
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3D GIF中的colorbar相关的知识,希望对你有一定的参考价值。
所以我试图在一个盒子里绘制粒子的运动,并想用彩色条可视化速度。我的代码看起来像这样
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
。相反,如果你创建一个普通的轴
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])
以上是关于3D GIF中的colorbar的主要内容,如果未能解决你的问题,请参考以下文章