Reputation: 117
I am fairly new to using matplotlib and cannot find any examples that show how to mark the angle of a point. I need to find the angle in all four quadrants i.e. if the point is (1,1) angle=45 degrees, (-1,1) angle= 135 degrees, (-1,-1) angle=225 degrees and for (1,-1) it should be 315 degrees.
Here is the function to which i need to add this to:
def visualize(val,ar):
plt.figure()
ax = plt.gca()
ax.plot([val-5],[ar-5], marker='o', color='r')
ax.set_xlim([-5,5])
ax.set_ylim([-5,5])
plt.draw()
plt.grid()
plt.show()
Upvotes: 2
Views: 1041
Reputation: 20344
I think you need to do the maths on your points yourself and then annotate the points on the plot using annotate()
. It's hard to tell from your example whether val
and ar
are single values or vectors - I think single values given the syntax you're using. Here's an example with a function to do the maths and an example use of annotate
- I've tried to keep the plotting bits the same as your code and just add the bits to calculate degrees and then put them on the axes
import math
import matplotlib.pyplot as plt
#This function does the maths - turns co-ordinates into angles
#with 0 on +ve x axis, increasing anti-clockwise
def get_theta(x,y):
theta = math.atan(y*1.0/x) / (2*math.pi) * 360
if x < 0:
theta += 180
if theta < 0:
theta += 360
return theta
def visualize(val,ar):
ax = plt.gca()
ax.plot([val-5],[ar-5], marker='o', color='r')
ax.set_xlim([-5,5])
ax.set_ylim([-5,5])
#insert the following to calculate the angles and
#then annotate them on the plot
x,y = val-5,ar-5
label = get_theta(x, y)
ax.annotate(str(label)+' degrees', xy = (x, y), xytext = (-20, 20),textcoords = 'offset points')
if __name__ == '__main__':
plt.figure()
x = [6,4,4,6]
y = [6,6,4,4]
for (val, ar) in zip(x,y):
visualize(val,ar)
plt.draw()
plt.grid()
plt.show()
Further variations on what you can do with annotations are in the docs.
Upvotes: 1