python绘制旋转椭球体和向量
我试图绘制一个椭球体和向量,如图所示: 我使用python绘制旋转椭球体和向量,python,plot,ellipse,svd,Python,Plot,Ellipse,Svd,我试图绘制一个椭球体和向量,如图所示: 我使用U旋转椭球体和V作为向量,U和V来自SVD。 我从.txt文件中读取的矩阵A: A.txt 3, 4, 5 6, 3, 2 8, 1, 2 我得到以下错误 "ValueError: shapes (3,3) and (150,) not aligned: 3 (dim 1) != 150 (dim 0)" for line B[:, i] = np.dot(U, B[:, i]) 谁能帮帮我吗?我可以画出椭球体,但它
U
旋转椭球体和V
作为向量,U
和V
来自SVD
。
我从.txt
文件中读取的矩阵A:
A.txt
3, 4, 5
6, 3, 2
8, 1, 2
我得到以下错误
"ValueError: shapes (3,3) and (150,) not aligned: 3 (dim 1) != 150 (dim 0)" for line B[:, i] = np.dot(U, B[:, i])
谁能帮帮我吗?我可以画出椭球体,但它不是扁平的,它看起来像一个球体
import numpy as np
import matplotlib.pyplot as plt
A = np.loadtxt("A.txt", dtype='i', delimiter=',')
#SVD
U, S, V = np.linalg.svd(A)
theta = np.linspace(0.0, 2.0 * np.pi, 50)
fi = np.linspace(0.0, np.pi, 50)
x = S[0] * np.outer( np.sin(theta), np.cos(fi) )
y = S[1] * np.outer( np.sin(theta), np.sin(fi) )
z = S[2] * np.outer( np.cos(theta), np.ones_like(fi) )
#plot
fig = plt.figure()
ax = fig.gca(projection='3d')
#plot lying down ellipsoid
ax.plot_wireframe(x, y, z, rstride=4, cstride=4, color='b', alpha=0.2) #elipsoid E1
#x, y, z matrix
B = np.vstack( (x, y) )
B = np.vstack( (B, z) )
#rotate ellipsoid with matrix U
k = len(x)
for i in range(0, k):
B[:, i] = np.dot(U, B[:, i])
#plot rotated ellipsoid
plt.plot3D(B[0, :], B[1, :], B[2, :], 'bo')
#sigma1, sigma2 si sigma3
sigma_1 = S[0]
sigma_2 = S[1]
sigma_3 = S[2]
#vectors
sigma1_u1 = sigma_1 * np.atleast_2d( U[:, 0] ).T
sigma2_u2 = sigma_2 * np.atleast_2d( U[:, 1] ).T
sigma3_u3 = sigma_3 * np.atleast_2d( U[:, 2] ).T
v_zero_1 = np.zeros_like(sigma1_u1)
v_zero_2 = np.zeros_like(sigma1_u1)
#vector_matrix
vector_matrix = sigma1_u1
vector_matrix = np.hstack( (matrice_vector, v_zero_1) )
vector_matrix = np.hstack( (matrice_vector, sigma2_u2) )
vector_matrix = np.hstack( (matrice_vector, v_zero_2) )
vector_matrix = np.hstack( (matrice_vector, sigma3_u3) )
#plot vectors
ax.plot3D(matrice_vector[0, :], matrice_vector[1, :], matrice_vector[2, :], 'b', linewidth = 4)