📌  相关文章
📜  如何使用 Matplotlib 在Python中绘制角度?

📅  最后修改于: 2022-05-13 01:54:21.221000             🧑  作者: Mango

如何使用 Matplotlib 在Python中绘制角度?

在本文中,我们将学习如何在Python中绘制角度。众所周知,要画一个角,必须有两条相交的线,这样两条线之间才能在相交点处形成角度。在此,我们在两条相交的直线上绘制一个角度。所以,让我们先讨论一些概念:

  • NumPy是一个通用的数组处理包。它提供了一个高性能的多维数组对象和用于处理这些数组的工具。
  • Matplotlib是一个出色的Python可视化库,用于数组的 2D 绘图。 Matplotlib是一个基于 NumPy 数组的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈一起使用。它是由 John Hunter 在 2002 年推出的。

在这个 matplotlib 中,用于以图形方式绘制角度,它最适合 Numpy,而 NumPy 是用于执行高级数学的数值Python

需要的步骤

  1. 绘制两条相交线。
  2. 找到用颜色标记的交点。
  3. 以两条直线的交点与圆心相同的方式绘制圆。
  4. 将其标记为圆与直线相交的点,并绘制我们找到它们之间夹角的两点。
  5. 计算角度并绘制角度。

分步实施

1. 绘制两条相交线

  • 在这里,代码的前两行显示了Python的 matplotlib 和 NumPy 框架是导入的,我们将在后面的代码中使用内置函数。
  • 之后,取斜率和截距以绘制两条直线。在那之后,行空间( l )相对于间隔均匀地返回数字空间。
  • 之后 plt.figure() 用于创建我们绘制角度的区域,它的尺寸在代码中给出。
  • 之后为了绘制直线,我们必须定义轴。这里:X 轴:0-6 和 Y 轴:0-6
  • 标题是使用 plt.title() 赋予图形框的。

之后绘制两条线,如下面的输出所示:

Python3
# import packages
import matplotlib.pyplot as plt
import numpy as np
 
# slope  and intercepts
a1, b1 = (1/4), 1.0
a2, b2 = (3/4), 0.0
 
# The numpy.linspace() function returns
# number spaces evenly w.r.t interval
l = np.linspace(-6, 6, 100)
 
# use to create new figure
plt.figure(figsize=(8, 8))
 
# plotting
plt.xlim(0, 6)
plt.ylim(0, 6)
plt.title('Plot an angle using Python')
plt.plot(l, l*a1+b1)
plt.plot(l, l*a2+b2)
plt.show()


Python3
# import packages
import matplotlib.pyplot as plt
import numpy as np
 
# slope  and intercepts
a1, b1 = (1/4), 1.0
a2, b2 = (3/4), 0.0
 
# The numpy.linspace() function returns
# number spaces evenly w.r.t interval
l = np.linspace(-6, 6, 100)
 
# use to create new figure
plt.figure(figsize=(8, 8))
 
# plotting
plt.xlim(0, 6)
plt.ylim(0, 6)
plt.title('Plot an angle using Python')
plt.plot(l, l*a1+b1)
plt.plot(l, l*a2+b2)
 
# intersection point
x0 = (b2-b1)/(a1-a2)
y0 = a1*x0 + b1
plt.scatter(x0, y0, color='midnightblue')


Python3
# import packages
import matplotlib.pyplot as plt
import numpy as np
 
# slope  and intercepts
a1, b1 = (1/4), 1.0
a2, b2 = (3/4), 0.0
 
# The numpy.linspace() function returns
# number spaces evenly w.r.t interval
l = np.linspace(-6, 6, 100)
 
# use to create new figure
plt.figure(figsize=(8, 8))
 
# plotting
plt.xlim(0, 6)
plt.ylim(0, 6)
plt.title('Plot an angle using Python')
plt.plot(l, l*a1+b1)
plt.plot(l, l*a2+b2)
 
# intersection point
x0 = (b2-b1)/(a1-a2)
y0 = a1*x0 + b1
plt.scatter(x0, y0, color='midnightblue')
 
# circle for angle
theta = np.linspace(0, 2*np.pi, 100)
r = 1.0
x1 = r * np.cos(theta) + x0
x2 = r * np.sin(theta) + y0
plt.plot(x1, x2, color='green', linestyle='dotted')


Python3
# import packages
import matplotlib.pyplot as plt
import numpy as np
 
# slope  and intercepts
a1, b1 = (1/4), 1.0
a2, b2 = (3/4), 0.0
 
# The numpy.linspace() function returns
# number spaces evenly w.r.t interval
l = np.linspace(-6, 6, 100)
 
# use to create new figure
plt.figure(figsize=(8, 8))
 
# plotting
plt.xlim(0, 6)
plt.ylim(0, 6)
plt.title('Plot an angle using Python')
plt.plot(l, l*a1+b1)
plt.plot(l, l*a2+b2)
 
# intersection point
x0 = (b2-b1)/(a1-a2)
y0 = a1*x0 + b1
plt.scatter(x0, y0, color='midnightblue')
 
# circle for angle
theta = np.linspace(0, 2*np.pi, 100)
r = 1.0
x1 = r * np.cos(theta) + x0
x2 = r * np.sin(theta) + y0
plt.plot(x1, x2, color='green', linestyle='dotted')
 
 
# intersection points
x_points = []
y_points = []
 
# Code for Intersecting points of circle with Straight Lines
def intersection_points(slope, intercept, x0, y0, radius):
    a = 1 + slope**2
    b = -2.0*x0 + 2*slope*(intercept - y0)
    c = x0**2 + (intercept-y0)**2 - radius**2
 
    # solving the quadratic equation:
    delta = b**2 - 4.0*a*c  # b^2 - 4ac
    x1 = (-b + np.sqrt(delta)) / (2.0 * a)
    x2 = (-b - np.sqrt(delta)) / (2.0 * a)
 
    x_points.append(x1)
    x_points.append(x2)
 
    y1 = slope*x1 + intercept
    y2 = slope*x2 + intercept
 
    y_points.append(y1)
    y_points.append(y2)
 
    return None
 
 
# Finding the intersection points for line1 with circle
intersection_points(a1, b1, x0, y0, r)
 
# Finding the intersection points for line1 with circle
intersection_points(a2, b2, x0, y0, r)
 
# Here we plot Two points in order to find angle between them
plt.scatter(x_points[0], y_points[0], color='crimson')
plt.scatter(x_points[2], y_points[2], color='crimson')
 
 
# Naming the points.
plt.text(x_points[0], y_points[0], '  Point_P1', color='black')
plt.text(x_points[2], y_points[2], '  Point_P2', color='black')


Python3
# import packages
import matplotlib.pyplot as plt
import numpy as np
 
# slope  and intercepts
a1, b1 = (1/4), 1.0
a2, b2 = (3/4), 0.0
 
# The numpy.linspace() function returns
# number spaces evenly w.r.t interval
l = np.linspace(-6, 6, 100)
 
# use to create new figure
plt.figure(figsize=(8, 8))
 
# plotting
plt.xlim(0, 6)
plt.ylim(0, 6)
plt.title('Plot an angle using Python')
plt.plot(l, l*a1+b1)
plt.plot(l, l*a2+b2)
 
# intersection point
x0 = (b2-b1)/(a1-a2)
y0 = a1*x0 + b1
plt.scatter(x0, y0, color='midnightblue')
 
# circle for angle
theta = np.linspace(0, 2*np.pi, 100)
r = 1.0
x1 = r * np.cos(theta) + x0
x2 = r * np.sin(theta) + y0
plt.plot(x1, x2, color='green', linestyle='dotted')
 
 
# intersection points
x_points = []
y_points = []
 
# Code for Intersecting points of circle with Straight Lines
def intersection_points(slope, intercept, x0, y0, radius):
    a = 1 + slope**2
    b = -2.0*x0 + 2*slope*(intercept - y0)
    c = x0**2 + (intercept-y0)**2 - radius**2
 
    # solving the quadratic equation:
    delta = b**2 - 4.0*a*c  # b^2 - 4ac
    x1 = (-b + np.sqrt(delta)) / (2.0 * a)
    x2 = (-b - np.sqrt(delta)) / (2.0 * a)
 
    x_points.append(x1)
    x_points.append(x2)
 
    y1 = slope*x1 + intercept
    y2 = slope*x2 + intercept
 
    y_points.append(y1)
    y_points.append(y2)
 
    return None
 
 
# Finding the intersection points for line1 with circle
intersection_points(a1, b1, x0, y0, r)
 
# Finding the intersection points for line1 with circle
intersection_points(a2, b2, x0, y0, r)
 
# Here we plot Two ponts in order to find angle between them
plt.scatter(x_points[0], y_points[0], color='crimson')
plt.scatter(x_points[2], y_points[2], color='crimson')
 
 
# Naming the points.
plt.text(x_points[0], y_points[0], '  Point_P1', color='black')
plt.text(x_points[2], y_points[2], '  Point_P2', color='black')
 
 
# plot angle value
 
def get_angle(x, y, x0, y0, radius):
 
    base = x - x0
    hypotenuse = radius
 
    # calculating the angle for a intersection point
    # which is equal to the cosine inverse of (base / hypotenuse)
    theta = np.arccos(base / hypotenuse)
 
    if y-y0 < 0:
        theta = 2*np.pi - theta
 
    print('theta=', theta, ',theta in degree=', np.rad2deg(theta), '\n')
 
    return theta
 
 
theta_list = []
 
for i in range(len(x_points)):
 
    x = x_points[i]
    y = y_points[i]
 
    print('intersection point p{}'.format(i))
    theta_list.append(get_angle(x, y, x0, y0, r))
 
    # angle for intersection point1 ( here point p1 is taken)
p1 = theta_list[0]
 
# angle for intersection point2 ( here point p4 is taken)
p2 = theta_list[2]
 
# all the angles between the two intersection points
theta = np.linspace(p1, p2, 100)
 
# calculate the x and y points for
# each angle between the two intersection points
x1 = r * np.cos(theta) + x0
x2 = r * np.sin(theta) + y0
 
# plot the angle
plt.plot(x1, x2, color='black')
 
# Code to print the angle at the midpoint of the arc.
mid_angle = (p1 + p2) / 2.0
 
x_mid_angle = (r-0.5) * np.cos(mid_angle) + x0
y_mid_angle = (r-0.5) * np.sin(mid_angle) + y0
 
angle_in_degree = round(np.rad2deg(abs(p1-p2)), 1)
 
plt.text(x_mid_angle, y_mid_angle, angle_in_degree, fontsize=12)
 
# plotting the intersection points
plt.scatter(x_points[0], y_points[0], color='red')
plt.scatter(x_points[2], y_points[2], color='red')
plt.show()


输出 :

2. 找到交点并用颜色标记

这里 x0,y0 表示两条直线的交点。绘制的两条直线写为:

y1 = a1*x + b1
y2 = a2*x + b2.

求解上述方程,我们得到,

x0 = (b2-b1) / (a1-a2)   -(i)
y0 =a1*x0 + b1             -(ii)

从上面的方程 (i) 和 (ii) 我们将得到两条直线的交点,然后,使用 plot.scatter()函数将颜色 ='midnightblue' 分配给交点。

蟒蛇3

# import packages
import matplotlib.pyplot as plt
import numpy as np
 
# slope  and intercepts
a1, b1 = (1/4), 1.0
a2, b2 = (3/4), 0.0
 
# The numpy.linspace() function returns
# number spaces evenly w.r.t interval
l = np.linspace(-6, 6, 100)
 
# use to create new figure
plt.figure(figsize=(8, 8))
 
# plotting
plt.xlim(0, 6)
plt.ylim(0, 6)
plt.title('Plot an angle using Python')
plt.plot(l, l*a1+b1)
plt.plot(l, l*a2+b2)
 
# intersection point
x0 = (b2-b1)/(a1-a2)
y0 = a1*x0 + b1
plt.scatter(x0, y0, color='midnightblue')

输出:

3. 画一个圆,使两条线的交点与圆心相同

在这里,我们使用 Circle 的参数方程绘制一个圆。圆的参数方程为:

x1= r*cos(theta)
x2=r*sin(theta)

如果我们希望圆不在原点,那么我们使用:

x1= r*cos(theta) + h
x2=r*sin(theta) + k

这里 h 和 k 是圆心的坐标。所以我们使用上面的等式,其中 h =x0 和 k =y0 如图所示。此外,这里我们为圆圈提供颜色“蓝色”,其样式标记为“虚线”。

蟒蛇3

# import packages
import matplotlib.pyplot as plt
import numpy as np
 
# slope  and intercepts
a1, b1 = (1/4), 1.0
a2, b2 = (3/4), 0.0
 
# The numpy.linspace() function returns
# number spaces evenly w.r.t interval
l = np.linspace(-6, 6, 100)
 
# use to create new figure
plt.figure(figsize=(8, 8))
 
# plotting
plt.xlim(0, 6)
plt.ylim(0, 6)
plt.title('Plot an angle using Python')
plt.plot(l, l*a1+b1)
plt.plot(l, l*a2+b2)
 
# intersection point
x0 = (b2-b1)/(a1-a2)
y0 = a1*x0 + b1
plt.scatter(x0, y0, color='midnightblue')
 
# circle for angle
theta = np.linspace(0, 2*np.pi, 100)
r = 1.0
x1 = r * np.cos(theta) + x0
x2 = r * np.sin(theta) + y0
plt.plot(x1, x2, color='green', linestyle='dotted')

输出:

4. 将其标记为圆与直线相交的点,并绘制我们找到它们之间夹角的那两个点

现在,让我们找出圆与两条直线相交的点。阅读下面的评论并了解如何标记点。在该颜色之后,即在圆将与两条直线相交的地方提供“深红色”颜色。在将名称作为 Point_P1、Point_P2 提供给点之后,我们找到它们之间的角度并将其标记为黑色,如输出所示。

蟒蛇3

# import packages
import matplotlib.pyplot as plt
import numpy as np
 
# slope  and intercepts
a1, b1 = (1/4), 1.0
a2, b2 = (3/4), 0.0
 
# The numpy.linspace() function returns
# number spaces evenly w.r.t interval
l = np.linspace(-6, 6, 100)
 
# use to create new figure
plt.figure(figsize=(8, 8))
 
# plotting
plt.xlim(0, 6)
plt.ylim(0, 6)
plt.title('Plot an angle using Python')
plt.plot(l, l*a1+b1)
plt.plot(l, l*a2+b2)
 
# intersection point
x0 = (b2-b1)/(a1-a2)
y0 = a1*x0 + b1
plt.scatter(x0, y0, color='midnightblue')
 
# circle for angle
theta = np.linspace(0, 2*np.pi, 100)
r = 1.0
x1 = r * np.cos(theta) + x0
x2 = r * np.sin(theta) + y0
plt.plot(x1, x2, color='green', linestyle='dotted')
 
 
# intersection points
x_points = []
y_points = []
 
# Code for Intersecting points of circle with Straight Lines
def intersection_points(slope, intercept, x0, y0, radius):
    a = 1 + slope**2
    b = -2.0*x0 + 2*slope*(intercept - y0)
    c = x0**2 + (intercept-y0)**2 - radius**2
 
    # solving the quadratic equation:
    delta = b**2 - 4.0*a*c  # b^2 - 4ac
    x1 = (-b + np.sqrt(delta)) / (2.0 * a)
    x2 = (-b - np.sqrt(delta)) / (2.0 * a)
 
    x_points.append(x1)
    x_points.append(x2)
 
    y1 = slope*x1 + intercept
    y2 = slope*x2 + intercept
 
    y_points.append(y1)
    y_points.append(y2)
 
    return None
 
 
# Finding the intersection points for line1 with circle
intersection_points(a1, b1, x0, y0, r)
 
# Finding the intersection points for line1 with circle
intersection_points(a2, b2, x0, y0, r)
 
# Here we plot Two points in order to find angle between them
plt.scatter(x_points[0], y_points[0], color='crimson')
plt.scatter(x_points[2], y_points[2], color='crimson')
 
 
# Naming the points.
plt.text(x_points[0], y_points[0], '  Point_P1', color='black')
plt.text(x_points[2], y_points[2], '  Point_P2', color='black')

输出:

5.计算角度和Plot Angle

在下面的代码中,点 Point_P1 和 Point_P2 之间的角度被计算,最后,它被绘制成输出所示。

蟒蛇3

# import packages
import matplotlib.pyplot as plt
import numpy as np
 
# slope  and intercepts
a1, b1 = (1/4), 1.0
a2, b2 = (3/4), 0.0
 
# The numpy.linspace() function returns
# number spaces evenly w.r.t interval
l = np.linspace(-6, 6, 100)
 
# use to create new figure
plt.figure(figsize=(8, 8))
 
# plotting
plt.xlim(0, 6)
plt.ylim(0, 6)
plt.title('Plot an angle using Python')
plt.plot(l, l*a1+b1)
plt.plot(l, l*a2+b2)
 
# intersection point
x0 = (b2-b1)/(a1-a2)
y0 = a1*x0 + b1
plt.scatter(x0, y0, color='midnightblue')
 
# circle for angle
theta = np.linspace(0, 2*np.pi, 100)
r = 1.0
x1 = r * np.cos(theta) + x0
x2 = r * np.sin(theta) + y0
plt.plot(x1, x2, color='green', linestyle='dotted')
 
 
# intersection points
x_points = []
y_points = []
 
# Code for Intersecting points of circle with Straight Lines
def intersection_points(slope, intercept, x0, y0, radius):
    a = 1 + slope**2
    b = -2.0*x0 + 2*slope*(intercept - y0)
    c = x0**2 + (intercept-y0)**2 - radius**2
 
    # solving the quadratic equation:
    delta = b**2 - 4.0*a*c  # b^2 - 4ac
    x1 = (-b + np.sqrt(delta)) / (2.0 * a)
    x2 = (-b - np.sqrt(delta)) / (2.0 * a)
 
    x_points.append(x1)
    x_points.append(x2)
 
    y1 = slope*x1 + intercept
    y2 = slope*x2 + intercept
 
    y_points.append(y1)
    y_points.append(y2)
 
    return None
 
 
# Finding the intersection points for line1 with circle
intersection_points(a1, b1, x0, y0, r)
 
# Finding the intersection points for line1 with circle
intersection_points(a2, b2, x0, y0, r)
 
# Here we plot Two ponts in order to find angle between them
plt.scatter(x_points[0], y_points[0], color='crimson')
plt.scatter(x_points[2], y_points[2], color='crimson')
 
 
# Naming the points.
plt.text(x_points[0], y_points[0], '  Point_P1', color='black')
plt.text(x_points[2], y_points[2], '  Point_P2', color='black')
 
 
# plot angle value
 
def get_angle(x, y, x0, y0, radius):
 
    base = x - x0
    hypotenuse = radius
 
    # calculating the angle for a intersection point
    # which is equal to the cosine inverse of (base / hypotenuse)
    theta = np.arccos(base / hypotenuse)
 
    if y-y0 < 0:
        theta = 2*np.pi - theta
 
    print('theta=', theta, ',theta in degree=', np.rad2deg(theta), '\n')
 
    return theta
 
 
theta_list = []
 
for i in range(len(x_points)):
 
    x = x_points[i]
    y = y_points[i]
 
    print('intersection point p{}'.format(i))
    theta_list.append(get_angle(x, y, x0, y0, r))
 
    # angle for intersection point1 ( here point p1 is taken)
p1 = theta_list[0]
 
# angle for intersection point2 ( here point p4 is taken)
p2 = theta_list[2]
 
# all the angles between the two intersection points
theta = np.linspace(p1, p2, 100)
 
# calculate the x and y points for
# each angle between the two intersection points
x1 = r * np.cos(theta) + x0
x2 = r * np.sin(theta) + y0
 
# plot the angle
plt.plot(x1, x2, color='black')
 
# Code to print the angle at the midpoint of the arc.
mid_angle = (p1 + p2) / 2.0
 
x_mid_angle = (r-0.5) * np.cos(mid_angle) + x0
y_mid_angle = (r-0.5) * np.sin(mid_angle) + y0
 
angle_in_degree = round(np.rad2deg(abs(p1-p2)), 1)
 
plt.text(x_mid_angle, y_mid_angle, angle_in_degree, fontsize=12)
 
# plotting the intersection points
plt.scatter(x_points[0], y_points[0], color='red')
plt.scatter(x_points[2], y_points[2], color='red')
plt.show()

输出: