2

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() 
Code_aholic
  • 117
  • 9
  • similar to: http://stackoverflow.com/questions/6556361/add-polar-axes-to-cartesian-plot-in-matplotlib – kaminsknator Jun 30 '15 at 17:02
  • 2
    This is not, fundamentally, a *matplotlib* question. Eventually you may need to write the angle on the plot, for which you will use `matplotlib.pyplot.text` (equivalent to `matplotlib.axes.Axes.text`). Until you get there, this is a *geometry* question. See [here](http://math.stackexchange.com/questions/1265695/finding-the-exact-values-of-trig-functions-in-a-quadrant) or [here](http://math.stackexchange.com/questions/352655/question-on-inverse-trig-functions-and-quadrants-please-help). Those are not exact answers to your problem, but provide good hints. – rjonnal Jun 30 '15 at 17:45

1 Answers1

1

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.


Output

Picture of graph with the example points annotated

Community
  • 1
  • 1
J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
  • @Code_aholic does this do what you want? If there are any issues - feel free to leave a comment and we can probably address. Otherwise - if you're not sure what to do to mark the question resolved, please see [what to do when someone answers my question](http://stackoverflow.com/help/someone-answers) – J Richard Snape Jul 01 '15 at 16:33