从x轴和y轴绘制线到点。

huangapple go评论73阅读模式
英文:

Draw lines from x axis and y axis to points

问题

我想要从点到x和y轴画一条线:

从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_))

这是结果:

从x轴和y轴绘制线到点。

英文:

I want to draw the line from the point to the x and y axis:

从x轴和y轴绘制线到点。

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:

从x轴和y轴绘制线到点。

答案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的默认值来添加额外的线。

输出:
从x轴和y轴绘制线到点。

英文:

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.

Output:
从x轴和y轴绘制线到点。

答案2

得分: 0

  1. 在相对坐标轴单位 (xa, ya) 中找到点 (x, y) 的值。
  2. 从点 (x, y) 到坐标轴边界 (xa, 0)(0, ya) 绘制标注线。

请注意,它们在缩放时不会正确更新... 这也是可能的,但需要更多的工作。

英文:

You could draw the lines using annotations.

  1. find the values of the point (x, y) in relative axis-units (xa, ya)
  2. 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)

从x轴和y轴绘制线到点。

huangapple
  • 本文由 发表于 2023年5月15日 01:34:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定