#http://stackoverflow.com/questions/10374930/matplotlib-annotating-a-3d-scatter-plot from mpl_toolkits.mplot3d import proj3d import matplotlib.pyplot as plt import numpy as np fig = plt.figure() fig.set_size_inches([8,8]) ax = fig.add_subplot(111, projection='3d') ax.set_aspect(1) ax.set_xlim([0,2]) ax.set_ylim([0,2]) ax.set_zlim([0,2]) ax.set_aspect(1) ax.set_xlabel('x-axis',fontsize=16) ax.set_ylabel('y-axis',fontsize=16) ax.set_zlabel('z-axis',fontsize=16) y = matrix([1,1,1]).T V = matrix([[1,0.25], # columns are v_1, v_2 [0,0.50], [0,0.00]]) alpha=inv(V.T*V)*V.T*y # optimal coefficients P = V*inv(V.T*V)*V.T yhat = P*y # approximant u = np.linspace(0, 2*np.pi, 100) v = np.linspace(0, np.pi, 100) xx = np.outer(np.cos(u), np.sin(v)) yy = np.outer(np.sin(u), np.sin(v)) zz = np.outer(np.ones(np.size(u)), np.cos(v)) sphere=ax.plot_surface(xx+y[0,0], yy+y[1,0], zz+y[2,0], rstride=4, cstride=4, color='gray',alpha=0.3,lw=0.25) ax.plot3D([y[0,0],0],[y[1,0],0],[y[2,0],0],'r-',lw=3) ax.plot3D([y[0,0]],[y[1,0]],[y[2,0]],'ro') ax.plot3D([V[0,0],0],[V[1,0],0],[V[2,0],0],'b-',lw=3) ax.plot3D([V[0,0]],[V[1,0]],[V[2,0]],'bo') ax.plot3D([V[0,1],0],[V[1,1],0],[V[2,1],0],'b-',lw=3) ax.plot3D([V[0,1]],[V[1,1]],[V[2,1]],'bo') ax.plot3D([yhat[0,0],0],[yhat[1,0],0],[yhat[2,0],0],'g--',lw=3) ax.plot3D([yhat[0,0]],[yhat[1,0]],[yhat[2,0]],'go') x2, y2, _ = proj3d.proj_transform(y[0,0],y[1,0],y[2,0], ax.get_proj()) ax.annotate( "$\mathbf{y}$", xy = (x2, y2), xytext = (-20, 20), fontsize=24, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) x2, y2, _ = proj3d.proj_transform(yhat[0,0],yhat[1,0],yhat[2,0], ax.get_proj()) ax.annotate( "$\hat{\mathbf{y}}$", xy = (x2, y2), xytext = (-40, 10), fontsize=24, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) x2, y2, _ = proj3d.proj_transform(V[0,0],V[1,0],V[2,0], ax.get_proj()) ax.annotate( "$\mathbf{v}_1$", xy = (x2, y2), xytext = (120, 10), fontsize=24, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) x2, y2, _ = proj3d.proj_transform(V[0,1],V[1,1],V[2,1], ax.get_proj()) ax.annotate( "$\mathbf{v}_2$", xy = (x2, y2), xytext = (-30, 30), fontsize=24, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) plt.show() from mpl_toolkits.mplot3d import proj3d import matplotlib.pyplot as plt import numpy as np fig = plt.figure() fig.set_size_inches([8,8]) ax = fig.add_subplot(111, projection='3d') ax.set_xlim([0,2]) ax.set_ylim([0,2]) ax.set_zlim([0,2]) ax.set_aspect(1) ax.set_xlabel('x-axis',fontsize=16) ax.set_ylabel('y-axis',fontsize=16) ax.set_zlabel('z-axis',fontsize=16) y = matrix([1,1,1]).T V = matrix([[1,0.25], # columns are v_1, v_2 [0,0.50], [0,0.00]]) Q = matrix([[1,0,0], [0,2,0], [0,0,3]]) P = V*inv(V.T*Q*V)*V.T*Q yhat = P*y # approximant u = np.linspace(0, 2*np.pi, 100) v = np.linspace(0, np.pi, 100) xx = np.outer(np.cos(u), np.sin(v)) yy = np.outer(np.sin(u), np.sin(v)) zz = np.outer(np.ones(np.size(u)), np.cos(v)) xx,yy,yz=map(squeeze,split(tensordot(dstack([xx,yy,zz]),Q,axes=1),3,axis=2)) ellipsoid=ax.plot_surface(xx+y[0,0], yy+y[1,0], zz+y[2,0], rstride=4, cstride=4, color='gray',alpha=0.3,lw=0.25) ax.plot3D([y[0,0],0],[y[1,0],0],[y[2,0],0],'r-',lw=3) ax.plot3D([y[0,0]],[y[1,0]],[y[2,0]],'ro') ax.plot3D([V[0,0],0],[V[1,0],0],[V[2,0],0],'b-',lw=3) ax.plot3D([V[0,0]],[V[1,0]],[V[2,0]],'bo') ax.plot3D([V[0,1],0],[V[1,1],0],[V[2,1],0],'b-',lw=3) ax.plot3D([V[0,1]],[V[1,1]],[V[2,1]],'bo') ax.plot3D([yhat[0,0],0],[yhat[1,0],0],[yhat[2,0],0],'g--',lw=3) ax.plot3D([yhat[0,0]],[yhat[1,0]],[yhat[2,0]],'go') x2, y2, _ = proj3d.proj_transform(y[0,0],y[1,0],y[2,0], ax.get_proj()) ax.annotate( "$\mathbf{y}$", xy = (x2, y2), xytext = (-20, 20), fontsize=24, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) x2, y2, _ = proj3d.proj_transform(yhat[0,0],yhat[1,0],yhat[2,0], ax.get_proj()) ax.annotate( "$\hat{\mathbf{y}}$", xy = (x2, y2), xytext = (40, 30), fontsize=24, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) x2, y2, _ = proj3d.proj_transform(V[0,0],V[1,0],V[2,0], ax.get_proj()) ax.annotate( "$\mathbf{v}_1$", xy = (x2, y2), xytext = (120, 10), fontsize=24, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) x2, y2, _ = proj3d.proj_transform(V[0,1],V[1,1],V[2,1], ax.get_proj()) ax.annotate( "$\mathbf{v}_2$", xy = (x2, y2), xytext = (-30, 30), fontsize=24, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) plt.show() from mpl_toolkits.mplot3d import proj3d import matplotlib.pyplot as plt import numpy as np fig = plt.figure() fig.set_size_inches([8,8]) ax = fig.add_subplot(111, projection='3d') ax.set_xlim([0,2]) ax.set_ylim([0,2]) ax.set_zlim([0,2]) ax.set_aspect(1) ax.set_xlabel('x-axis',fontsize=16) ax.set_ylabel('y-axis',fontsize=16) ax.set_zlabel('z-axis',fontsize=16) y = matrix([1,1,1]).T V = matrix([[1,0.25], # columns are v_1, v_2 [0,0.50], [0,0.00]]) def rotation_matrix(angle,axis='z'): angle = angle/180.*pi if axis=='z': return matrix([[cos(angle),sin(angle),0], [sin(-angle),cos(angle),0], [0,0,1]]) elif axis=='y': return matrix([[cos(angle),sin(angle),0], [0,1,0], [sin(-angle),cos(angle),0]]) elif axis=='x': return matrix([[1,0,0], [cos(angle),sin(angle),0], [sin(-angle),cos(angle),0]]) S = matrix([[3,0,0], [0,2,0], [0,0,1]]) R = rotation_matrix(30)*rotation_matrix(30,'x')*rotation_matrix(40,'y') Q = R.T*S*R # apply 3-D rotations P = V*inv(V.T*Q*V)*V.T*Q # build projection matrix yhat = P*y # approximant u = np.linspace(0, 2*np.pi, 100) v = np.linspace(0, np.pi, 100) xx = np.outer(np.cos(u), np.sin(v)) yy = np.outer(np.sin(u), np.sin(v)) zz = np.outer(np.ones(np.size(u)), np.cos(v)) xx,yy,yz=map(squeeze,split(tensordot(dstack([xx,yy,zz]),Q,axes=1),3,axis=2)) ellipsoid=ax.plot_surface(xx+y[0,0], yy+y[1,0], zz+y[2,0], rstride=4, cstride=4, color='gray',alpha=0.3,lw=0.25) ax.plot3D([y[0,0],0],[y[1,0],0],[y[2,0],0],'r-',lw=3) ax.plot3D([y[0,0]],[y[1,0]],[y[2,0]],'ro') ax.plot3D([V[0,0],0],[V[1,0],0],[V[2,0],0],'b-',lw=3) ax.plot3D([V[0,0]],[V[1,0]],[V[2,0]],'bo') ax.plot3D([V[0,1],0],[V[1,1],0],[V[2,1],0],'b-',lw=3) ax.plot3D([V[0,1]],[V[1,1]],[V[2,1]],'bo') ax.plot3D([yhat[0,0],0],[yhat[1,0],0],[yhat[2,0],0],'g--',lw=3) ax.plot3D([yhat[0,0]],[yhat[1,0]],[yhat[2,0]],'go') x2, y2, _ = proj3d.proj_transform(y[0,0],y[1,0],y[2,0], ax.get_proj()) ax.annotate( "$\mathbf{y}$", xy = (x2, y2), xytext = (-20, 20), fontsize=24, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) x2, y2, _ = proj3d.proj_transform(yhat[0,0],yhat[1,0],yhat[2,0], ax.get_proj()) ax.annotate( "$\hat{\mathbf{y}}$", xy = (x2, y2), xytext = (40, 30), fontsize=24, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) x2, y2, _ = proj3d.proj_transform(V[0,0],V[1,0],V[2,0], ax.get_proj()) ax.annotate( "$\mathbf{v}_1$", xy = (x2, y2), xytext = (120, 10), fontsize=24, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) x2, y2, _ = proj3d.proj_transform(V[0,1],V[1,1],V[2,1], ax.get_proj()) ax.annotate( "$\mathbf{v}_2$", xy = (x2, y2), xytext = (-30, 30), fontsize=24, textcoords = 'offset points', ha = 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) plt.show()