matplotlib画箭头

今天简单学习了一下怎么画箭头,感觉还是很有意思的
参考
https://stackoverflow.com/questions/43378730/how-to-get-an-open-and-scaling-arrow-head-in-matplotlib

import matplotlib.pyplot as plt 
# plt.arraw(x,y,dx,dy) # 注意这个参数表示的含义
plt.arrow(2,4,2,2,width=0.05)
plt.show()

结果如下
在这里插入图片描述
但这个ax.anotate也可以画箭头,但我还没学,有时间好好学一下

画不同的箭头的形状

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(4,4))

v = [-0.2, 0, .2, .4, .6, .8, 1]
for i, overhang in enumerate(v):
    ax.arrow(.1,overhang,.6,0, width=0.001, color="k", 
             head_width=0.1, head_length=0.15, overhang=overhang)

ax.set_yticks(v)
ax.set_xticks([])
ax.set_ylabel("overhang")
ax.set_ylim(-0.3,1.1)
plt.tight_layout()
plt.show()

在这里插入图片描述

画一个箭头

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(4,4))
overhang = 1.0
ax.arrow(0,0,1.0,1.0, width=0.001, color="k", 
         head_width=0.1, head_length=0.15, overhang=overhang)

#ax.add_line((0,0),(1,0))

ax.set_yticks([-0.2,0,0.4,0.8,1.0])
ax.set_xticks([-0.2,0,0.4,0.8,1.0])
ax.set_ylabel("overhang")
ax.set_ylim(-0.3,1.2)
ax.set_ylim(-0.3,1.2)
plt.tight_layout()
plt.show()

结果如下
在这里插入图片描述但是这个图有点问题,就是说这个箭头好像超出范围了

from matplotlib.lines import Line2D  
import matplotlib.pyplot as plt 
fig, ax = plt.subplots(figsize=(4,4))
overhang = 1.0
ax.arrow(0,0,1.0,1.0, width=0.001, color="k", 
         head_width=0.1, head_length=0.15, overhang=overhang)


### 添加这个线感觉不是很对的,箭头超出范围了
x1 =[0,1]
y1 =[1,1]
line1= Line2D(x1,y1)
ax.add_line(line1)
x2 =[1,1]
y2 =[0,1]
line2= Line2D(x2,y2)
ax.add_line(line2)

ax.set_yticks([-0.2,0,0.4,0.8,1.0])
ax.set_xticks([-0.2,0,0.4,0.8,1.0])
ax.set_ylabel("overhang")
ax.set_ylim(-0.3,1.2)
ax.set_ylim(-0.3,1.2)
plt.tight_layout()
plt.show()

在这里插入图片描述

修改箭头指示范围(length_includes_head=True)

from matplotlib.lines import Line2D  
import matplotlib.pyplot as plt 
fig, ax = plt.subplots(figsize=(4,4))
overhang = 1.0
ax.arrow(0,0,1.0,1.0, width=0.001, color="k", 
         head_width=0.1, head_length=0.15, overhang=overhang,length_includes_head=True)


### 添加这个线感觉不是很对的,箭头超出范围了
x1 =[0,1]
y1 =[1,1]
line1= Line2D(x1,y1)
ax.add_line(line1)
x2 =[1,1]
y2 =[0,1]
line2= Line2D(x2,y2)
ax.add_line(line2)

ax.set_yticks([-0.2,0,0.4,0.8,1.0])
ax.set_xticks([-0.2,0,0.4,0.8,1.0])
ax.set_ylabel("overhang")
ax.set_ylim(-0.3,1.2)
ax.set_ylim(-0.3,1.2)
plt.tight_layout()
plt.show()

在这里插入图片描述

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值