gpt4 book ai didi

python - matlibplot中两个向量之间的填充区域

转载 作者:行者123 更新时间:2023-12-04 07:44:44 25 4
gpt4 key购买 nike

以下代码计算以下向量:
方向矢量 (红色) 两个向量(蓝调)这是通过将红色矢量旋转 60 度时钟和逆时针而产生的。

import matplotlib.pyplot as plt
import numpy as np


def Visualize(orienVector,vector1,vector2):
# Create figure and subplot
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

# Plot data points
#ax.scatter(vector1[0], vector1[1], color='blue')
#ax.scatter(vector2[0], vector2[1], color='orange')

# Set limits for x and y axes
plt.xlim(-1, 1)
plt.ylim(-1, 1)

# ===== Important bits start here =====

# Set properties of spines
ax.spines['top'].set_color('none')
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')

# Set axis tick positions
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

# Set specific tick locations
ax.set_xticks([-10,-5, 0, 5 , 10])
ax.set_yticks([-10,-5, 0, 5 , 10])

# ===== End Of Important Bits =====

# Draw arrows
ax.arrow(0, 0 , vector1[0][0], vector1[1][0],
head_width=0.03,
head_length=0.1,
lw=1,
fc='blue',
ec='blue',
length_includes_head=True)

ax.arrow(0, 0 , vector2[0][0], vector2[1][0],
head_width=0.03,
head_length=0.1,
lw=1,
fc='blue',
ec='blue',
length_includes_head=True)

ax.arrow(0, 0 , orienVector[0][0], orienVector[1][0],
head_width=0.03,
head_length=0.1,
lw=2,
fc='red',
ec='red',
length_includes_head=True)

plt.show()

# rotation matrix clockwise
def rotMatrixClockWise(angle):
c, s = np.cos(angle), np.sin(angle)
R = np.array([[c, -s], [s, c]])
return R

# rotation matrix clockwise
def rotMatrixCounterClockWise(angle):
c, s = np.cos(angle), np.sin(angle)
R = np.array([[c, s], [-s, c]])
return R

# center of the poit of interests POIS
POI_X = [10,12,15,17,20,50]
POI_Y = [20,30,25,22,19,35]

# position of the pedestrian
pedPostion = np.array([[3],[4]])

# range of the horisontal angel view
spanningAngle = np.radians(60)

# calculate the cone
for angle in range(0,360,5):


# calculating the component of orientation vector V0, where the length |V0| = 1
x0 = 5*np.cos(np.radians(angle))
y0 = 5*np.sin(np.radians(angle))
v0 = np.array([[x0],[y0]])

v1 = rotMatrixCounterClockWise(spanningAngle).dot(v0)
v2 = rotMatrixClockWise(spanningAngle).dot(v0)


Visualize(v0,v1,v2)
这个向量的输出看起来像
output
我正在尝试填充蓝色向量之间的区域以获得如下所示的锥体:
concept Output
圆锥头 (0,0) 与圆弧之间的距离始终为 5
但是,我无法让它工作。我是 Matlibplot 的新手

最佳答案

也许不完全是你想要的,但你可以在向量之间创建一个楔子。拟合椭圆并填充其间的坐标会更合适。这是楔形的示例。

import matplotlib.pyplot as plt, numpy as np
from matplotlib import patches
v1 = np.array((1, 2))
v2 = np.array((1, -2))
base = np.array((0, 0))
theta1 = np.rad2deg(np.arctan(v1[1] / v1[0]))
theta2 = np.rad2deg(np.arctan(v2[1] / v2[0]))

fig, ax = plt.subplots()

a, b = theta1, theta2
if b > a:
a, b = b, a
artist = patches.Arc(base,
width = 1,
height = 1,
theta1 = a,
theta2 = b,
color = 'green')
ax.arrow(*base, *v1, color = 'black')
ax.arrow(*base, *v2, color = 'black')
ax.arrow(*base, *(v2 + v1), color = 'red')
wedge = patches.Wedge(base,
r = 1,
width = 1,
theta1 = theta2,
theta2 = theta1,
color = 'green')
ax.add_patch(wedge)

fig.show()
enter image description here

关于python - matlibplot中两个向量之间的填充区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67254855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com