I'm simulating projectile motion with python and there's one thing I need help with. As I use plt.legend()
to show the angles corresponded to each lines(on the upper right corner of my graph), they appear in a radian form. How do I convert them to make them appear in an angle form, like 30 degree
, 60 degree
etc?
The graph:
import numpy as np
import matplotlib.pyplot as plt
import math
#theta=pi/3
V=38.5 #speed
t=np.arange(0,19,0.1)
angle = math.pi/12
angle_list=[]
while angle < math.pi/2:
x=V*np.cos(angle)*t
y=V*np.sin(angle)*t+(0.5*-9.8*t**2)
plt.ylim([0,80])
plt.xlim([0,170])
print(x,y)
plt.plot(x,y)
angle_list.append(angle)
angle+=math.pi/12
plt.xlabel("range")
plt.ylabel("height")
plt.title('Projectile Motion With Multiple Angles')
plt.legend(angle_list)
plt.show()
You can use np.rad2deg
or do the multiplication yourself:
angle_list.append(np.rad2deg(angle))
OR
angle_list.append(angle * 180.0 / np.pi)
np.rad2deg
is equivalent to np.degrees
.
You can format your angles by adding them to the legends as formatted strings. For example, since your angles are all multiples of 15 degrees, you don't need any decimal places:
angle_list.append(rf'{np.rad2deg(angle):.0f}$\degree$')
The result: