我正在根据this thread的答案制作4D彩色图形,这是我的代码:
plt.figure(figsize=(16,9))
plt.xlabel('Angle of a')
plt.ylabel('Coherence')
plt.grid(True)
vec_coherence = np.vectorize(Coherence)
vec_qgen = np.vectorize(Q_gen)
vec_tp = np.vectorize(n_Qubit_Teleportation)
vec_q1 = np.vectorize(q1_message)
vec_q2 = np.vectorize(q2_message)
vec_q3 = np.vectorize(q3_message)
x = np.arange(0,2*np.pi,0.1)
y = np.arange(0,2*np.pi,0.1)
z = np.arange(0,2*np.pi,0.1)
Z = np.outer(z.T, z)
X, Y = np.meshgrid(x, y)
color_dimension = vec_coherence(vec_qgen(vec_tp(3)*vec_q3(x, y, z)))
minn, maxx = color_dimension.min(), color_dimension.max()
norm = matplotlib.colors.Normalize(minn, maxx)
m = plt.cm.ScalarMappable(norm=norm, cmap='jet')
m.set_array([])
fcolors = m.to_rgba(color_dimension)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X,Y,Z, rstride=1, cstride=1, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
fig.canvas.show()
plt.title("Coherence for 3-qubit teleportation")
plt.legend()
plt.savefig("plot3QTP_GenericM")
我收到此索引错误:
IndexError Traceback (most recent call last)
<ipython-input-28-04656187df61> in <module>()
32 fig = plt.figure()
33 ax = fig.gca(projection='3d')
---> 34 ax.plot_surface(X,Y,Z, rstride=1, cstride=1, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False)
35 ax.set_xlabel('x')
36 ax.set_ylabel('y')
C:\ProgramData\Anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py in plot_surface(self, X, Y, Z, *args, **kwargs)
1719
1720 if fcolors is not None:
-> 1721 colset.append(fcolors[rs][cs])
1722 else:
1723 colset.append(avgzsum / len(ps2))
IndexError: index 4 is out of bounds for axis 0 with size 4
由于错误,代码正在绘制2D和3D空图。如何解决此错误? 我正在构建此代码,以将函数f(x,y,z)的结果绘制为不同颜色的图。