以下代码有效,但一行除外;如果我包括ax.set_zlim3d(minz, maxz)
(第29行),那么条形都会在x-y平面上偏移。如何在不发生这种情况的情况下更改此代码以修复z轴的限制?
%matplotlib inline
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import proj3d
# need to install ffmpeg, on osx; brew install ffmpeg
from matplotlib import cm, animation, rc
rc('animation', html='html5')
#from IPython.display import HTML
import itertools
def setup_plot(minz, maxz):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
xypos = [(3,3),(2,3),(1,3),(3,2),(2,2),(1,2),(3,1),(2,1),(1,1)]
xpos = [i for i,j in xypos]
ypos = [j for i,j in xypos]
zpos = [0,0,0,0,0,0,0,0,0]
dx = 0.5 * np.ones_like(zpos)
dy = 0.5 * np.ones_like(zpos)
values = np.zeros(9)
poly3d = ax.bar3d(xpos, ypos, zpos, dx, dy, values, zsort='average')
ax.view_init(elev=35,azim=45)
ax.zaxis.set_rotate_label(False) # disable automatic rotation
ax.set_zlabel('Potential (V)', rotation=90)
ax.set_zlim3d(minz, maxz)
showxy=False
if showxy:
ax.set_xlabel('x')
ax.set_ylabel('y')
else:
ax.set_xticks([])
ax.set_yticks([])
ax.grid(True)
sm = plt.cm.ScalarMappable(cmap=cm.jet)
sm.set_array([minz, maxz])
fig.colorbar(sm, shrink=0.5, aspect=5)
fig.set_figwidth(8)
fig.set_figheight(6)
return fig, ax
minz, maxz = 0.5, 1.5
fig, ax = setup_plot(minz, maxz)
def update_bars(i, iter_vals, minz, maxz):
xypos = [(3,3),(2,3),(1,3),(3,2),(2,2),(1,2),(3,1),(2,1),(1,1)]
xpos = [i for i,j in xypos]
ypos = [j for i,j in xypos]
zpos = [0,0,0,0,0,0,0,0,0]
dx = 0.5 * np.ones_like(zpos)
dy = 0.5 * np.ones_like(zpos)
values = iter_vals.next()
values_norm = values - minz
colors = cm.jet(values_norm)
ax.collections.pop()
ax.bar3d(xpos, ypos, zpos, dx, dy, values, color=colors, zsort='average')
return
values = np.random.random((20,9))*1.5
anim = animation.FuncAnimation(fig, update_bars, frames=values.shape[0], interval=600, blit=False,
fargs=[itertools.cycle(values), minz, maxz], repeat=True)
anim
答案 0 :(得分:1)
当您更改z轴上的限制时,您将移动绘图并基本上放大条形图,将中心移动到x-y平面上的(3.25,3.25)。您可以通过在setup_plot
函数中插入以下行来手动更改x-y平面的限制:
ax.set_ylim3d(2,4.5)
ax.set_xlim3d(2,4.5)