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

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

Draw lines from x axis and y axis to points

问题

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

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

这是我使用的代码:

  1. from sklearn.decomposition import PCA
  2. pca = PCA()
  3. principalComponents = pca.fit_transform(x)
  4. plt.figure()
  5. plt.plot(np.cumsum(pca.explained_variance_ratio_))
  6. plt.xlabel('组件数量')
  7. plt.ylabel('方差(%)') #每个组件的方差
  8. plt.title('解释方差')
  9. plt.show()
  10. pca = PCA(n_components=0.8)
  11. new_data = pca.fit_transform(x)
  12. pca.explained_variance_ratio_
  13. variance = np.cumsum(np.round(pca.explained_variance_ratio_, decimals=3))
  14. variance
  15. 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:

  1. from sklearn.decomposition import PCA
  2. pca = PCA()
  3. principalComponents = pca.fit_transform(x)
  4. plt.figure()
  5. plt.plot(np.cumsum(pca.explained_variance_ratio_))
  6. plt.xlabel('Number of Components')
  7. plt.ylabel('Variance (%)') #for each component
  8. plt.title('Explained Variance')
  9. plt.show()
  10. pca = PCA(n_components=0.8)
  11. new_data = pca.fit_transform(x)
  12. pca.explained_variance_ratio_
  13. variance = np.cumsum(np.round(pca.explained_variance_ratio_, decimals=3))
  14. variance
  15. print(abs( pca.components_ ))

Here is the result:

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

答案1

得分: 1

这是我在谈论的示例。请注意,我从图表的点开始,然后通过添加额外的线来进行“后处理”。

  1. import matplotlib.pyplot as plt
  2. points = [(0,0), (2,1), (4,3), (6,4)]
  3. xx =

    for p in points]

  4. yy =

    for p in points]

  5. plt.xlabel('Number of Components')

  6. plt.ylabel('Variance (%)')

  7. plt.title('Explained Variance')

  8. plt.plot(xx,yy)

  9. for c,(x,y) in enumerate(points):

  10. xx = [0, x, x]

  11. yy = [y, y, 0]

  12. plt.plot(xx,yy)

  13. 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.

  1. import matplotlib.pyplot as plt
  2. points = [(0,0), (2,1), (4,3), (6,4)]
  3. xx =

    for p in points]

  4. yy =

    for p in points]

  5. plt.xlabel('Number of Components')

  6. plt.ylabel('Variance (%)')

  7. plt.title('Explained Variance')

  8. plt.plot(xx,yy)

  9. for c,(x,y) in enumerate(points):

  10. xx = [0, x, x]

  11. yy = [y, y, 0]

  12. plt.plot(xx,yy)

  13. 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)

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. points = np.array([(0,0), (1,1), (2, 4)])
  4. f, ax = plt.subplots()
  5. ax.plot(*points.T)
  6. def add_lines(points):
  7. f.canvas.draw() # trigger a draw to make sure transformations are correct
  8. for x, y in points:
  9. # transform coordinates into relative axis-units (e.g. 0-1)
  10. xa, ya = ax.transAxes.inverted().transform(ax.transData.transform((x, y)))
  11. # add annotation-lines from the point to the x-axis
  12. ax.annotate(
  13. xy=(x, y), xycoords="data",
  14. xytext=(xa,0), textcoords="axes fraction",
  15. text="",
  16. arrowprops=dict(arrowstyle="-", shrinkA=0, shrinkB=0, color="r"),
  17. )
  18. # add annotation-lines from the point to the y-axis
  19. ax.annotate(
  20. xy=(x, y), xycoords="data",
  21. xytext=(0,ya), textcoords="axes fraction",
  22. text="",
  23. arrowprops=dict(arrowstyle="-", shrinkA=0, shrinkB=0, color="g")
  24. )
  25. 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:

确定