英文:
Draw lines from x axis and y axis to points
问题
我想要从点到x和y轴画一条线:
这是我使用的代码:
from sklearn.decomposition import PCA
pca = PCA()
principalComponents = pca.fit_transform(x)
plt.figure()
plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('组件数量')
plt.ylabel('方差(%)') #每个组件的方差
plt.title('解释方差')
plt.show()
pca = PCA(n_components=0.8)
new_data = pca.fit_transform(x)
pca.explained_variance_ratio_
variance = np.cumsum(np.round(pca.explained_variance_ratio_, decimals=3))
variance
print(abs(pca.components_))
这是结果:
英文:
I want to draw the line from the point to the x and y axis:
Here is the code I used:
from sklearn.decomposition import PCA
pca = PCA()
principalComponents = pca.fit_transform(x)
plt.figure()
plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('Number of Components')
plt.ylabel('Variance (%)') #for each component
plt.title('Explained Variance')
plt.show()
pca = PCA(n_components=0.8)
new_data = pca.fit_transform(x)
pca.explained_variance_ratio_
variance = np.cumsum(np.round(pca.explained_variance_ratio_, decimals=3))
variance
print(abs( pca.components_ ))
Here is the result:
答案1
得分: 1
这是我在谈论的示例。请注意,我从图表的点开始,然后通过添加额外的线来进行“后处理”。
import matplotlib.pyplot as plt
points = [(0,0), (2,1), (4,3), (6,4)]
xx = for p in points]
yy = for p in points]
plt.xlabel('Number of Components')
plt.ylabel('Variance (%)')
plt.title('Explained Variance')
plt.plot(xx,yy)
for c,(x,y) in enumerate(points):
xx = [0, x, x]
yy = [y, y, 0]
plt.plot(xx,yy)
plt.show()
"为什么"你可能会问,"你为什么使用enumerate来获取一个你不使用的变量?"因为我认为我会需要用它来索引到一个颜色数组。在这里,我只是依赖于matplotlib的默认值来添加额外的线。
英文:
Here's an example of what I'm talking about. Note that I start with the points of the graph, then I "post process" that by adding the additional lines.
import matplotlib.pyplot as plt
points = [(0,0), (2,1), (4,3), (6,4)]
xx = for p in points]
yy =
for p in points]
plt.xlabel('Number of Components')
plt.ylabel('Variance (%)')
plt.title('Explained Variance')
plt.plot(xx,yy)
for c,(x,y) in enumerate(points):
xx = [0, x, x]
yy = [y, y, 0]
plt.plot(xx,yy)
plt.show()
"Why", you may ask, "did you use enumerate to get a variable you don't use?" Because I thought I would need that to index into an array of colors. Here, I'm just relying on the matplotlib defaults for additional lines.
答案2
得分: 0
- 在相对坐标轴单位
(xa, ya)
中找到点(x, y)
的值。 - 从点
(x, y)
到坐标轴边界(xa, 0)
和(0, ya)
绘制标注线。
请注意,它们在缩放时不会正确更新... 这也是可能的,但需要更多的工作。
英文:
You could draw the lines using annotations.
- find the values of the point
(x, y)
in relative axis-units(xa, ya)
- draw annotation lines from the point
(x, y
) to the axis-boundaries(xa, 0)
and(0, ya)
(note that they will NOT correctly update on zoom... this is also possible but requires a bit more work)
import matplotlib.pyplot as plt
import numpy as np
points = np.array([(0,0), (1,1), (2, 4)])
f, ax = plt.subplots()
ax.plot(*points.T)
def add_lines(points):
f.canvas.draw() # trigger a draw to make sure transformations are correct
for x, y in points:
# transform coordinates into relative axis-units (e.g. 0-1)
xa, ya = ax.transAxes.inverted().transform(ax.transData.transform((x, y)))
# add annotation-lines from the point to the x-axis
ax.annotate(
xy=(x, y), xycoords="data",
xytext=(xa,0), textcoords="axes fraction",
text="",
arrowprops=dict(arrowstyle="-", shrinkA=0, shrinkB=0, color="r"),
)
# add annotation-lines from the point to the y-axis
ax.annotate(
xy=(x, y), xycoords="data",
xytext=(0,ya), textcoords="axes fraction",
text="",
arrowprops=dict(arrowstyle="-", shrinkA=0, shrinkB=0, color="g")
)
add_lines(points)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论