[英]How to add lines from 0,0 to each scatter plot point using matplotlib?
I'm trying to recreate the following chart with matplotlib, but I can't figure it out how to create the lines from the 0,0(origin) to each point我正在尝试使用 matplotlib 重新创建以下图表,但我无法弄清楚如何创建从 0,0(原点)到每个点的线
my current code is:我当前的代码是:
plt.figure(figsize=(10,7))
plt.grid()
plt.xlabel('Movie 1')
plt.ylabel('Movie 2')
A = [1.0, 2.0]
B = [2.0, 4.0]
C = [2.5, 4.0]
D = [4.5, 5.0]
xs = [A[0], B[0], C[0], D[0]]
ys = [A[1], B[1], C[1], D[1]]
users = ['A', 'B', 'C', 'D']
for i, user in enumerate(users):
x = xs[i]
y = ys[i]
plt.scatter(x, y, marker = 'o')
plt.plot(x,y,0,0, linestyle = '--' )
plt.text(x+0.01, y+0.01, user, fontsize=19)
plt.xlim(0,7)
plt.ylim(0,7)
plt.show()
The code only returns the points, but with no lines... how do I create the lines?代码只返回点,但没有线......如何创建线?
Thanks!谢谢!
you can do this by plotting the lines manually in your for loop:您可以通过在 for 循环中手动绘制线条来做到这一点:
for i, user in enumerate(users):
x = xs[i]
y = ys[i]
plt.scatter(x, y, marker = 'o')
# V this adds the lines V
plt.plot([0,xs[i]], [0, ys[i]], color="black")
# ^ this adds the lines ^
plt.plot(x,y,0,0, linestyle = '--' )
plt.text(x+0.01, y+0.01, user, fontsize=19)
which results in this graph这导致了这个图表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.