【问题标题】:Draw arrows on matplotlib figure directly直接在matplotlib图上画箭头
【发布时间】:2016-04-26 16:49:36
【问题描述】:

我在网格上创建了​​一个包含多个子图的图。这些图在两个参数上有所不同,所以我希望它看起来像是在坐标系中排序的。

我设法使用 matplotlib.lines.Line2D() 直接在图上的子图旁边绘制线条。 但我更喜欢有一个箭头而不是一条线,以使其更清晰。 (我可以使用 fig.text() 添加具体的参数值。)

I'd like the blue lines to be arrows

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from itertools import product 

fig = plt.figure()
plotGrid = mpl.gridspec.GridSpec(2, 2)

x = np.linspace(0,10,10000)
y = [j* np.sin(x + i)  for i,j in product(range(2), range(1,3))]

for i in range(4):
    ax = plt.Subplot(fig, plotGrid[i])

    for sp in ax.spines.values():
        sp.set_visible(False)

    ax.plot(x,y[i], color = 'r')
    ax.set_xticks([])
    ax.set_yticks([])
    fig.add_subplot(ax)

all_axes = fig.get_axes()


#I would like these lines to be arrows  
blcorPosn = 0.08 #bottom corner position
l1 = mpl.lines.Line2D([blcorPosn,blcorPosn], [1, blcorPosn],
                      transform=fig.transFigure, fig)

l2 = mpl.lines.Line2D([blcorPosn, 1], [blcorPosn, blcorPosn],
                      transform=fig.transFigure, fig)

fig.lines.extend([l1, l2])

我不确定这是否是要走的路。但是我现在花了一天的时间,到目前为止,我看到的唯一绘制箭头的方法是将它们直接绘制在轴上,但据我所知,这对我来说不是一个选择。

这也是我在这里的第一篇文章,因此非常感谢有关如何提问的建议。 谢谢

【问题讨论】:

标签: python matplotlib plot


【解决方案1】:

您可以使用对FancyArrow 补丁的稍微修改的调用来替换沿每个轴的 Line2D。主要区别在于起点和终点 x,y 坐标被替换为起点 x,y 和要绘制的 x,y 距离。这些值也直接作为参数传递,而不是作为列表:

l1 = mpl.patches.FancyArrow(blcorPosn, blcorPosn, 1, 0,
                            transform=fig.transFigure, figure=fig)

l2 = mpl.patches.FancyArrow(blcorPosn, blcorPosn, 0, 1,
                            transform=fig.transFigure, figure=fig)

FancyArrow 补丁接受一些其他参数以允许您自定义箭头的外观,包括 width(用于线宽)、head_widthhead_length

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2017-03-17
    • 2014-05-17
    • 2016-02-10
    • 1970-01-01
    相关资源
    最近更新 更多