我使用的是holoviews+bokeh,我想用标准差来包围我的散点图数据。不幸的是,我似乎没能正确地安排好方向。我对现有的描述感到困惑:
在笛卡儿坐标系中的方位,在第一轴和水平之间的弧度逆时针角度,你可以设置这个方向(弧度,逆时针旋转)。
我的脚本和数据示例:
def create_plot(x, y, nstd=5):
x, y = np.asarray(x), np.asarray(y)
cov_matrix = np.cov([x, y])
eigenvalues, eigenvectors = np.linalg.eig(cov_matrix)
order = eigenvalues.argsort()[0]
angle = np.arctan2(eigenvectors[1, order], eigenvectors[1, order])
x0 = np.mean(x)
y0 = np.mean(y)
x_dir = np.cos(angle) * x - np.sin(angle) * y
y_dir = np.sin(angle) * x + np.cos(angle) * y
w = nstd * np.std(x_dir)
h = nstd * np.std(y_dir)
return hv.Ellipse(x0, y0, (w, h), orientation=-angle) * hv.Scatter((x, y))
c2x = np.random.normal(loc=-2, scale=0.6, size=200)
c2y = np.random.normal(loc=-2, scale=0.1, size=200)
combined = create_plot(c2x, c2y)
combined.opts(shared_axes=False)发布于 2022-10-13 15:42:44
这里有一个解决方案,它将Ellipse绘制在数据周围。你的数学只是简化了。
import numpy as np
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')
x = np.random.normal(loc=-2, scale=0.6, size=200)
y = np.random.normal(loc=-2, scale=0.1, size=200)
def create_plot(x, y, nstd=5):
x, y = np.asarray(x), np.asarray(y)
x0 = np.mean(x)
y0 = np.mean(y)
w = np.std(x)*nstd
h = np.std(y)*nstd
return hv.Ellipse(x0, y0, (w, h)) * hv.Scatter((x, y))
combined = create_plot(c2x, c2y)
combined.opts()这给了你一个看起来像圆圈的情节。为了更清楚地知道这是一个椭圆,您可以生成这个情节调用
def hook(plot, element):
plot.handles['x_range'].start = -4
plot.handles['x_range'].end = 0
plot.handles['y_range'].start = -2.5
plot.handles['y_range'].end = -1
combined.opts(hooks=[hook])设置固定的范围并使自动对焦失效。
在您的示例中,w和h几乎相同,这意味着您绘制了一个证书。orientation没有任何效果。使用上面的代码,您可以像
hv.Ellipse(x0, y0, (w, h), orientation=np.pi/2)看它是否有效,但没有必要再这样做了。
https://stackoverflow.com/questions/74043927
复制相似问题