DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Plotting a circle in python with angles.

Plotting a circle in python with angles.

I have got some hard time dealing with angles in space, and because i skipped all of my geometry class i had no idea how to imagine angles in a circle so i decided to find a way to plot them using python

First lets Import Modules

import matplotlib.pyplot as pl
from numpy import sin, cos, pi, linspacet
Enter fullscreen mode Exit fullscreen mode

Adding a center point

plt.plot(0,0, color = 'red', marker = 'o')
Enter fullscreen mode Exit fullscreen mode

Image description

Adding a circle

r = 1.5
angles = linspace(0 * pi, 2 * pi, 100) 
print(angles)
xs = cos(angles)
ys = sin(angles)


plt.plot(xs, ys, color = 'green')
plt.xlim(-r, r)
plt.ylim(-r, r)
plt.gca().set_aspect('equal')
Enter fullscreen mode Exit fullscreen mode

Image description

Drawing diameter

plt.plot(r-0.5, 0, marker = 'P', color = 'blue')
plt.plot(-r+0.5, 0, marker = 'o', color = 'red')
plt.plot([r, -r], [0, 0], color = 'red')
Enter fullscreen mode Exit fullscreen mode

Image description

A function to convert from Degree to Radian

def deg2rad(deg):
    return deg * pi / 180
Enter fullscreen mode Exit fullscreen mode

Now lets draw two lines at 90Β° and 45Β°

plt.plot([0, r * cos(deg2rad(90))], [0, r * sin( deg2rad(90))], color = "red")
plt.plot([0, r * cos(deg2rad(45))], [0, r * sin( deg2rad(45))], color = "black")

Enter fullscreen mode Exit fullscreen mode

Show the final result

Image description

plt.savefig('angles.png')
Enter fullscreen mode Exit fullscreen mode

Hope that article would save some time.

Top comments (0)

Head to your account's Settings to...

🌚 Enable dark mode
πŸ”  Change your default font
πŸ“š Adjust your experience level to see more relevant content