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

如何解决matlibplot中两个向量之间的填充区域

以下代码计算以下向量: 方向 矢量 (红色)两个矢量(蓝色),它们是通过将红色矢量旋转 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,5,10])
 ax.set_yticks([-10,10])

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

 # Draw arrows
 ax.arrow(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,vector2[0][0],vector2[1][0],length_includes_head=True)

 ax.arrow(0,orienVector[0][0],orienVector[1][0],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],[-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 的新手

解决方法

您可以通过分析找出圆弧中的点(圆的一部分,因此 y = +- (1-x^2)**0.5),将它们添加到由红色的原点和末端定义的多边形中和黄色向量,然后使用 https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.fill.html

填充多边形的示例如下:https://matplotlib.org/stable/gallery/lines_bars_and_markers/fill.html#sphx-glr-gallery-lines-bars-and-markers-fill-py

,

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

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,*(v2 + v1),color = 'red')
wedge = patches.Wedge(base,r = 1,theta1 = theta2,theta2 = theta1,color = 'green')
ax.add_patch(wedge)

fig.show()

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res