In this article, we will learn how to plot an angle in Python. As we know that to draw an angle, there must be two intersecting lines so that angle can be made between those two lines at an intersecting point. In this, we plot an angle on two Intersecting Straight lines. So, lets first discuss some concepts :
- NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays.
- Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002.
In this matplotlib is used to plot angle graphically and it fits best with Numpy whereas NumPy is numerical Python which is used to perform Advance Mathematics.
- Plot two Intersecting lines.
- Find the point of Intersection marked with a color.
- Plot a circle in such a way that the point of Intersection of two lines is the same as Center of Circle.
- Mark it as a point where the circle will intersect with straight lines and plot that two points where we find the angle between them.
- Calculate the angle and plot the angle.
1. Plot two Intersecting lines
- In this, the first two lines of Code shows that matplotlib and NumPy Framework of Python is imported, from where we will use inbuilt functions in further code.
- After that, the slope and intercept are taken in order to plot two Straight lines. After that line-space ( l ) returns number spaces evenly with respect to the interval.
- After that plt.figure() is used to create the area where we plot the angle and it’s dimension is given in the code.
- After that In order to plot the Straight lines, we have to define the axis. Here : X-axis : 0-6 and Y-axis : 0-6
- The title is given to the figure box using plt.title().
After that two lines are plotted as shown in the output below:
2. Find the point of Intersection and marked with a color
Here x0,y0 denote the intersecting point of two Straight lines. The plotted two Straight lines are written as :
y1 = a1*x + b1 y2 = a2*x + b2.
On solving the above equations we get,
x0 = (b2-b1) / (a1-a2) -(i) y0 =a1*x0 + b1 -(ii)
From above equation (i) and (ii) we will get the point of intersection of Two Straight lines and after that, the color =’midnightblue’ is assigned to the point of intersection using plot.scatter() function.
3. Plot a circle in such a way that the point of intersection of two lines is the same as the Center of Circle
Here we plot a circle using the parametric equation of a Circle. The parametric equation of a circle is :
x1= r*cos(theta) x2=r*sin(theta)
If we want that the circle is not in the origin then we use :
x1= r*cos(theta) + h x2=r*sin(theta) + k
Here h and k are the coordinates of the center of the circle. So we use this above equation where h =x0 and k =y0 as shown. Also, here we provide the color to the circle ” blue” and its style is marked as “dotted”.
4. Mark it as a point where the circle will intersect with straight lines and plot those two points where we find the angle between them
Now, let’s find the points where the circle is intersecting the two straight lines. Read the comments below and understand how to mark points. After that color i.e. “crimson” color is provided where the circle will intersect two straight lines. After that names are provided to the points as Point_P1, Point_P2 where we find the angle between them and marked it as black colored as shown in the output.
5. Calculate the angle and Plot Angle
In the below code the angle between to points Point_P1 and Point_P2 is calculated and finally, it is plotted as shown in the output.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.