FancyArrowPatch未绘制标签 [英] FancyArrowPatch not drawing label

查看:25
本文介绍了FancyArrowPatch未绘制标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让 FancyArrowPatch 显示传递给 label kwarg 的文本.我看不到我在做什么错.有什么想法吗?

I can't get the FancyArrowPatch to display the text passed to the label kwarg. I don't see what I am doing wrong. Any idea?

设置:

import matplotlib.pyplot as plt
from matplotlib import patches
import numpy as np

fig = plt.figure()

X, Y = np.mgrid[-1:1:.1, -1:1:.1]
Z = X+Y

ax1 = fig.add_subplot(311)
ax2 = fig.add_subplot(313)
ax1.contourf(X, Y, Z)
ax2.contourf(X, Y, -Z)

箭头代码:

ax1tr = ax1.transData # Axis 0 -> Display
ax2tr = ax2.transData # Axis 1 -> Display
figtr = fig.transFigure.inverted() # Display -> Figure
ptB = figtr.transform(ax1tr.transform((0., 0.)))
ptE = figtr.transform(ax2tr.transform((0., 0.)))
arrow = patches.FancyArrowPatch(
    ptB, ptE, transform=fig.transFigure,  # Place arrow in figure coord system
    connectionstyle="arc3,rad=0", arrowstyle='simple',
    mutation_scale = 15., label='some arrow'
)
fig.patches.append(arrow)

fig.show()

我希望绘制一些箭头作为箭头的标签.但事实并非如此.

I would expect some arrow to be drawn as the label of the arrow. But it is not.

推荐答案

原则上,它应该在代码中添加 plt.legend()行之后才能工作.但是,有一个解决方法如下,您可以显式传递 arrow 补丁和标签文本.

In principle it should work after adding plt.legend() line in your code. However, there is a workaround solution as follows where you explicitly pass the arrow patch and the label text.

arrow = patches.FancyArrowPatch(
    ptB, ptE, transform=fig.transFigure,  # Place arrow in figure coord system
    connectionstyle="arc3,rad=0", arrowstyle='simple',
    mutation_scale = 15., label='some arrow'
)
fig.patches.append(arrow)

plt.legend([arrow], fontsize=16)

另一种选择是

plt.legend(handles=[arrow], fontsize=16)

根据我的评论, label 的目的不是将文本放在箭头补丁旁边.使用 plt.text 可以轻松实现您想要的.但是,如果您不想使用它,则可以使用以下解决方法使当前代码按需工作

Based on the comments, as I said, the purpose of label is not to put the text next to the arrow patch. What you want can be easily achieved using plt.text. However, if you don't want to use it, you can make the current code work as desired using the following workaround

leg = plt.legend(handles = [arrow], fontsize=16, frameon=False, loc=(0.45, 0.5))

for item in leg.legendHandles:
    item.set_visible(False)

这篇关于FancyArrowPatch未绘制标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆