怎么用python画指北针?

先放图
在这里插入图片描述
然后下面的python代码

from matplotlib.patches import *


def get_circle(x1, y1, x2, y2, x3, y3):
    """
    根据三个点,返回外接圆的中心和半径
    :param x1:
    :param y1:
    :param x2:
    :param y2:
    :param x3:
    :param y3:
    :return: x,y,r
    """
    a = x1 - x2
    b = y1 - y2
    c = x1 - x3
    d = y1 - y3
    a1 = ((x1 * x1 - x2 * x2) + (y1 * y1 - y2 * y2)) / 2.0
    a2 = ((x1 * x1 - x3 * x3) + (y1 * y1 - y3 * y3)) / 2.0
    theta = b * c - a * d
    if abs(theta) < 1e-7:
        raise ValueError("compute circle error!")
    x0 = (b * a2 - d * a1) / theta
    y0 = (c * a1 - a * a2) / theta
    r = np.sqrt(pow((x1 - x0), 2) + pow((y1 - y0), 2))
    return x0, y0, r


def add_north(ax, x, y, text_size=15, arrow_width=0.05,
              text_pad=0.01, arrow_height=None,
              line_width=1, add_circle=True):
    """
    给子图添加一个指北针,这里的长度,间隔等是0-1之间的小数
    :param ax: 子图句柄
    :param x: 指针尖的x坐标[0-1]
    :param y: 指针尖的y坐标[0-1]
    :param text_size: N字符大小
    :param arrow_width: 箭头的宽度
    :param text_pad: 文字和箭头的间隔
    :param arrow_height: 箭头的宽度
    :param line_width: 线条的宽度
    :param add_circle: 是否添加圆
    :return: None
    """
    if arrow_height is None:
        arrow_height = arrow_width * 1.87
    x_min, x_max = ax.get_xlim()
    y_min, y_max = ax.get_ylim()
    width = x_max - x_min
    height = y_max - y_min

    left = (x_min + width * (x - arrow_width * 0.5), y_min + height * (y - arrow_height))
    right = (x_min + width * (x + arrow_width * 0.5), left[1])
    top = (x_min + width * x, y_min + height * y)
    bottom_center = (top[0], left[1] + 0.27 * (top[1] - left[1]))
    left_patch = Polygon([left, top, bottom_center], color='k',
                         linewidth=line_width)
    right_patch = Polygon([bottom_center, top, right],
                          facecolor='none',
                          edgecolor='k', linewidth=line_width)
    ax.add_patch(left_patch)
    ax.add_patch(right_patch)
    if add_circle:
        circle_x, circle_y, r = get_circle(*top, *left, *right)

        circle_patch = Circle((circle_x, circle_y), r,
                              facecolor='none',
                              edgecolor='k',
                              linewidth=line_width)
        ax.add_patch(circle_patch)

    ax.text(s='N',
            x=top[0],
            y=top[1] + text_pad * height,
            fontsize=text_size,
            horizontalalignment='center',
            verticalalignment='bottom')

按照函数签名使用即可

你可能感兴趣的:(python,几何学)