#http://stackoverflow.com/questions/10374930/matplotlib-annotating-a-3d-scatter-plot from mpl_toolkits.mplot3d import proj3d import matplotlib.patches as patches import mpl_toolkits.mplot3d.art3d as art3d 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 = (40, 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')) xx = array([0,yhat[0],yhat[0]]) yy = array([0,yhat[1],yhat[1]]) zz = array([0,0,2]) ax.add_collection3d( art3d.Poly3DCollection([zip(xx,yy,zz)],alpha=0.15,color='m') ) ax.set_title(r'The magenta sheet contains vectors with the same projection, $\mathbf{\hat{y}}$') plt.show() import numpy as np from scipy.optimize import minimize # constraints formatted for scipy.optimize.minimize cons = [{'type':'eq','fun':lambda x: x[0]-1,'jac':None}, {'type':'eq','fun':lambda x: x[1]-1,'jac':None}, ] init_point = np.array([1,2,3,0]) # initial guess ysol= minimize(lambda x: np.dot(x,x),init_point,constraints=cons,method='SLSQP') # using projection method c = np.matrix([[1,1,0,0]]).T # RHS constraint vector V = np.matrix(np.eye(4)[:,0:2]) ysol_p = V*np.linalg.inv(V.T*V)*V.T*c print 'scipy optimize solution:', print ysol['x'] print 'projection solution:', print np.array(ysol_p).flatten() print np.allclose(np.array(ysol_p).flat,ysol['x'],atol=1e-6) import numpy as np from scipy.optimize import minimize # constraints formatted for scipy.optimize.minimize cons = [{'type':'eq','fun':lambda x: x[0]-1,'jac':None}, {'type':'eq','fun':lambda x: x[1]-1,'jac':None}, ] Q = np.diag ([1,2,3,4]) init_point = np.array([1,2,3,0]) # initial guess ysol= minimize(lambda x: np.dot(x,np.dot(Q,x)),init_point,constraints=cons,method='SLSQP') # using projection method Qinv = np.linalg.inv(Q) c = np.matrix([[1,1,0,0]]).T # RHS constraint vector V = np.matrix(np.eye(4)[:,0:2]) ysol_p = V*np.linalg.inv(V.T*Qinv*V)*V.T*Qinv*c print 'scipy optimize solution:', print ysol['x'] print 'projection solution:', print np.array(ysol_p).flatten() print np.allclose(np.array(ysol_p).flat,ysol['x'],atol=1e-5)