2015-09-07 55 views
0

我想創建一個2D高斯分佈,並在一定程度上旋轉它。爲什麼我的散點圖不旋轉?

import numpy as np 
import matplotlib.pyplot as plt 

x = np.random.normal(0, 15, 5000) 
y = np.random.normal(0, 3, 5000) 

X = np.array([x, y]) 
print X.shape 

angle = 28 
theta = np.pi * angle/180 

rotation = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) 
X1 = np.dot(rotation, X) 
print X1.shape 

fig = plt.figure(figsize=(16, 8)) 
fig.add_subplot(2, 1, 1).scatter(x, y) 
fig.add_subplot(2, 1, 2).scatter(X1[0], X1[:1]) 

plt.show() 

我希望看到什麼這裏是高斯第一散點圖,然後第二個幾乎一樣,但通過28度旋轉。而是我看到這一點:

enter image description here

+0

湯姆的答案是正確的。如果您對繪製雙變量分佈的其他方式感興趣,請參閱https://geonet.esri.com/blogs/dan_patterson/2015/06/16/before-i-forget-8-bivariate-distribution – 2015-09-07 15:56:36

回答

2

你只需要在你的方式指數X1錯誤。

目前,您繪製X1[0]X1[:1],但X1[:1]是一樣的X1[0],因爲你說「的第一維度的所有指標高達1」(即0)。

你只需要擺脫冒號 - 即你需要繪製X1[0]X1[1]

這工作:

fig.add_subplot(2, 1, 2).scatter(X1[0], X1[1]) 

enter image description here

相關問題