%matplotlib inline
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
This function draws a 3D set of axis and outputs a "subplot", that you'll be able to use to add more elements to the plot
def plot_3D_axis():
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax._axis3don = False
ax.plot(np.zeros(3),np.array(range(-1,2)),np.zeros(3),"k-")
ax.plot(np.array(range(-1,2)),np.zeros(3),np.zeros(3),"k-")
ax.plot(np.zeros(3),np.zeros(3),np.array(range(-1,2)),"k-")
ax.set_xlim([-0.5,0.5])
ax.set_ylim([-0.5,0.5])
ax.set_zlim([-0.5,0.5])
ax.set_xticks([-1,-0.5,0,0.5,1])
ax.set_xticklabels([],fontsize=20)
ax.set_yticks([-1,-0.5,0,0.5,1])
ax.set_yticklabels([],fontsize=20)
ax.set_zticks([-1,-0.5,0,0.5,1])
ax.set_zticklabels([],fontsize=20)
ax.text(1.05,0,0,"$D_1$",fontsize=17)
ax.text(0,1.05,0,"$D_2$",fontsize=17)
ax.text(0,0,1.05,"$D_3$",fontsize=17)
ax.text(-0.1,-0.01,0.01,"$\mathcal{O}$",fontsize=20)
return(ax)
This function computes the norm of each row (axis=1) or columns (axis=0) of a matrix M
def get_norm(M,axis=0):
return(np.sqrt((M**2).sum(axis=axis)))
This function computes the norm of each row (axis=1) or columns (axis=0) of a matrix M and devide the rows or the colums by their norm
def to_normed(M,axis=0):
norms = get_norm(M,axis)
for i,norm in enumerate(norms):
if norm:
M[:,i] *= 1./norm
return(M)
This is a simple example
A = np.random.randn(100,3)*0.1
ax = plot_3D_axis()
for a in A:
ax.plot([a[0]],[a[1]],[a[2]],"o",alpha=0.2,color="b")
xx, yy = np.meshgrid(np.arange(-0.5,1.5,0.1), np.arange(-0.5,1.5,0.1))
z = 0.1*xx+0.3*yy
ax = plot_3D_axis()
ax.plot_surface(xx, yy, z,alpha=0.3)