0

I'm making a 4D color graph based on the answer for this thread, this is my code:

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")

and I'm getting this Index Error:

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

And with the error, the code is plotting a 2D and a 3D empty graphs. How can I fix this error? I'm building this code to plot the results from a function f(x, y, z) as different colors on the plot.

Nillmer
  • 159
  • 1
  • 7
  • Without [mcve], one can only say: Check the shape of `fcolors`. – ImportanceOfBeingErnest Mar 23 '19 at 22:30
  • I did this and I realised that color_dimension should receive the matrices, not the vectors, I fixed it but now i'm havong another problem, my X and Y coordinates are ok but my Z is all wrong, while it should go from 0 to about 6, when I plot it goes to 30, how to I keep the values of Z from getting too distorted when converting to a matrix? – Nillmer Mar 24 '19 at 21:28

0 Answers0