2016-11-07 74 views
0

我是Python新手。我借这个代码的例子,把我自己的号码:如何使用matplotlib.pyplot通过各种点绘制图表?

import matplotlib.pyplot as plt 

plt.plot(1, 9, 'rs', 2, 8, 'bs', 7, 3, 'g^', 9, 1, 'r^') 
plt.title('Chart of 4 companies') 
plt.axis([0, 12, 0, 12]) 
plt.grid(True) 
plt.show() 

这个伟大的工程,它让我看到一个图表,并将其显示在图表上4分。但是有可能显示从点[0,0]到这些线的线吗?我想提一点关于余弦相似性的观点,但我失败的很糟糕。

回答

2

如果您想从(0,0)的直线到一个点(X,Y),你可以绘制它只需通过

plt.plot([0,x],[0,y]) 

所以整个代码可能看起来像这样

import matplotlib.pyplot as plt 

plt.plot(1, 9, 'rs', 2, 8, 'bs', 7, 3, 'g^', 9, 1, 'r^') 
plt.plot([0,1], [0,9], 'r') 
plt.plot([0,2], [0,8], 'b') 
plt.plot([0,7], [0,3], 'g') 
plt.plot([0,9], [0,1], 'r') 
plt.title('Chart of 4 companies') 
plt.axis([0, 12, 0, 12]) 
plt.grid(True) 
plt.show() 

enter image description here

相关问题