

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Best way to plot an angle between two lines in Matplotlib
The best way to plot an angle between two lines in Matplotlib is to use the Arc class to make an angle arc to plot the angle in between.
Steps
- Set the figure size and adjust the padding between and around the subplots.
- Create a new figure or activate an existing figure using figure() method.
- Add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method.
- Create 2D line instances as l1 and l2.
- Add lines to the current axes.
- To plot an angle, call a user-defined method that returns an elliptical arc. Arc length could be created using slopes of the lines..
- Add an artist, i.e., arc using add_patch() method.
- To display the figure, use show() method.
Example
from matplotlib import pyplot as plt, patches import math plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True def angle_plot(line1, line2, offset=1.0, color=None, origin=(0, 0), len_x_axis=1, len_y_axis=1): xy1 = line1.get_xydata() xy2 = line2.get_xydata() slope1 = (xy1[1][1] - xy1[0][1]) / float(xy1[1][0] - xy1[0][0]) angle1 = abs(math.degrees(math.atan(slope1))) slope2 = (xy2[1][1] - xy2[0][1]) / float(xy2[1][0] - xy2[0][0]) angle2 = abs(math.degrees(math.atan(slope2))) theta1 = min(angle1, angle2) theta2 = max(angle1, angle2) angle = theta2 - theta1 if color is None: color = line1.get_color() return patches.Arc(origin, len_x_axis * offset, len_y_axis * offset, 0, theta1, theta2, color=color, label=str(angle) + u"\u00b0") fig = plt.figure() ax = fig.add_subplot(1, 1, 1) l1 = plt.Line2D([0, 1], [0, 4], linewidth=1, linestyle="-", color="green") l2 = plt.Line2D([0, 4.5], [0, 3], linewidth=1, linestyle="-", color="red") ax.add_line(l1) ax.add_line(l2) angle = angle_plot(l1, l2, 0.25) ax.add_patch(angle) plt.show()
Output
- Related Questions & Answers
- How to plot an angle spectrum using Matplotlib in Python?
- Fill between two vertical lines in matplotlib
- Drawing lines between two plots in Matplotlib
- How to plot overlapping lines in Matplotlib?
- Shading an area between two points in a Matplotlib plot
- How to plot two dotted lines and set marker using Matplotlib?
- How to remove lines in a Matplotlib plot?
- Plot horizontal and vertical lines passing through a point that is an intersection point of two lines in Matplotlib
- Matplotlib Plot Lines with Colors through Colormap
- How to draw axis lines inside a plot in Matplotlib?
- Angle between two Planes in 3D in C++?
- How to plot the lines first and points last in Matplotlib?
- How to name different lines in the same plot of Matplotlib?
- How to add vertical lines to a distribution plot (sns.distplot) in Matplotlib?
- What is the best way to compare two strings in JavaScript?
Advertisements