0

I am new to Python. I borrowed this code from an example, and put in my own numbers:

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()

This works great, it shows me a chart, and it shows 4 points on the chart. But is it possible to show the line going from point [0,0] to these lines? I wanted to make a point about cosine similarity, but I am failing badly.

1 Answer 1

2

In case you want a straight line from (0,0) to a point (x,y) you can plot it simply via

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

So the whole code may look like this

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.