180122 利用matplotlib绘制箭头的2种方法

matplotlib.axes.Axes.arrow¶
matplotlib.pyplot.arrow
pylab_examples example code: arrow_simple_demo.py
StackOver-matplotlib arrows between 3 points

这里写图片描述

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 22 16:00:51 2018

@author: brucelau
"""

import numpy as np
import matplotlib.pyplot as plt



def drawArrow1(A, B):
    '''
    Draws arrow on specified axis from (x, y) to (x + dx, y + dy). 
    Uses FancyArrow patch to construct the arrow.

    The resulting arrow is affected by the axes aspect ratio and limits. 
    This may produce an arrow whose head is not square with its stem. 
    To create an arrow whose head is square with its stem, use annotate() for example:
    Example:
        ax.annotate("", xy=(0.5, 0.5), xytext=(0, 0),
        arrowprops=dict(arrowstyle="->"))
    '''
    fig = plt.figure()
    ax = fig.add_subplot(121)
    # fc: filling color
    # ec: edge color
    ax.arrow(A[0], A[1], B[0]-A[0], B[1]-A[1],
             length_includes_head=True,# 增加的长度包含箭头部分
             head_width=0.25, head_length=0.5, fc='r', ec='b')
    # 注意: 默认显示范围[0,1][0,1],需要单独设置图形范围,以便显示箭头
    ax.set_xlim(0,5)
    ax.set_ylim(0,5)
    ax.grid()
    ax.set_aspect('equal') #x轴y轴等比例
    # Example:
    ax = fig.add_subplot(122)
    ax.annotate("", xy=(B[0], B[1]), xytext=(A[0], A[1]),arrowprops=dict(arrowstyle="->"))
    ax.set_xlim(0,5)
    ax.set_ylim(0,5)
    ax.grid()
    ax.set_aspect('equal') #x轴y轴等比例
    plt.show()
    plt.tight_layout()
    #保存图片,通过pad_inches控制多余边缘空白
    plt.savefig('arrow.png', transparent = True, bbox_inches = 'tight', pad_inches = 0.25) 

#%%
a = np.array([1,2])
b = np.array([3,4])
drawArrow1(a,b)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GuokLiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值