这是一种可能的方法。使用给定的 x 和 y 创建一个三角形,并考虑角度和长度。如果默认方向(指向右侧的零角度)不是所需的方向,则可以将减号添加到正弦和/或余弦。此外,正弦和余弦的作用可以互换,以对角镜像。
为了防止角度看起来变形,可以使用set_aspect('equal')。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
bc = pd.DataFrame({'x': [2, 4, 6], 'y': [1, 3, 5], 'Length': [10, 25, 23], 'Direction': [-86, -85, -80]})
plt.figure(figsize=(14, 7))
delta = 15 # half of the aperture angle (in degrees)
length_factor = 1 / bc['Length'].max() # this makes the longest 1 unit long
for x, y, length, dir in zip(bc['x'], bc['y'], bc['Length'], bc['Direction']):
for is_backgr in (True, False):
if is_backgr:
arc_angles = np.linspace(dir + delta, dir + 360 - delta, 32)
else:
arc_angles = np.linspace(dir - delta, dir + delta, 10)
c_arr = np.cos(np.radians(arc_angles))
s_arr = np.sin(np.radians(arc_angles))
r = length * length_factor
x_arr = x + np.pad(c_arr * r, (1, 1))
y_arr = y + np.pad(s_arr * r, (1, 1))
plt.fill(x_arr, y_arr, c='grey' if is_backgr else 'crimson', alpha=0.2)
plt.plot(x_arr, y_arr, c='black' if is_backgr else 'crimson', lw=0.5 if is_backgr else 2)
ax = sns.scatterplot(x="x", y="y", data=bc, s=100)
ax.set_title('Scatter Plot of x and y')
ax.set_aspect('equal')
plt.show()