首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在matplotlib中绘制主图外的箭头和矩形(用于蛋白质秒结构)?

如何在matplotlib中绘制主图外的箭头和矩形(用于蛋白质秒结构)?
EN

Stack Overflow用户
提问于 2020-01-10 03:23:49
回答 1查看 496关注 0票数 3

我尝试在matplotlib (表示蛋白质二级结构)中,在图的y轴旁边绘制箭头和矩形,如下所示:

我从here得到了箭头部分,但我不知道如何在y轴之外绘制它。另外,除了箭头之外,还有什么方法可以画矩形吗?代码和输出如下:

代码语言:javascript
复制
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
x_tail = 0.0
y_tail = -0.1
x_head = 0.0
y_head = 0.9
dx = x_head - x_tail
dy = y_head - y_tail
fig, axs = plt.subplots(nrows=2)
arrow = mpatches.FancyArrowPatch((x_tail, y_tail), (dx, dy),
                                 mutation_scale=50,
                                 transform=axs[0].transAxes)
axs[0].add_patch(arrow)

arrow = mpatches.FancyArrowPatch((x_tail, y_tail), (dx, dy),
                                 mutation_scale=100,
                                 transform=axs[1].transAxes)
axs[1].add_patch(arrow)
axs[1].set_xlim(0, 1)
axs[1].set_ylim(0, 1)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-10 06:04:32

原来的方法看起来有点令人困惑。

虽然您可以通过mpatch.Rectangle绘制矩形,但我认为也可以通过FancyArrowPatch绘制矩形。这使得它们的行为和缩放方式类似,这对于设置宽度很有趣。类似地,垂直线也是使用FancyArrowPatch绘制的。

对于定位,it seems你可以只给出(tail_x, tail_y)head_x, head_y。通过arrowstyle=可以设置视觉尺寸。从样式中去掉head_length=似乎允许一个看起来像矩形的箭头。对于着色,有facecolor=edgecolor=。还有同时处理facecoloredgecolorcolor=

arrow1.set_clip_on(False)允许在页边空白处绘制箭头。其他函数可以有一个clip_on=False参数。当一条线绘制在另一条线上时,需要zorder=才能使正确的线可见。

下面是一些示例代码。矩形绘制了两次,这样垂直线就不会透过阴影显示出来。现在x定义在'axis coordinates'中,y定义在标准数据坐标中。“轴”坐标从0开始,左边框通常是y轴绘制到1,右边框。将x设置为-0.1表示y轴左侧10%。

代码语言:javascript
复制
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.transforms as mtransforms

x0 = -0.1

arrow_style="simple,head_length=15,head_width=30,tail_width=10"
rect_style="simple,tail_width=25"
line_style="simple,tail_width=1"

fig, ax = plt.subplots()

# the x coords of this transformation are axes, and the y coord are data
trans = mtransforms.blended_transform_factory(ax.transAxes, ax.transData)

y_tail = 5
y_head = 15
arrow1 = mpatches.FancyArrowPatch((x0, y_tail), (x0, y_head), arrowstyle=arrow_style, transform=trans)
arrow1.set_clip_on(False)
ax.add_patch(arrow1)

y_tail = 40
y_head = 60
arrow2 = mpatches.FancyArrowPatch((x0, y_tail), (x0, y_head), arrowstyle=arrow_style, facecolor='gold', edgecolor='black', linewidth=1, transform=trans)
arrow2.set_clip_on(False)
ax.add_patch(arrow2)

y_tail = 20
y_head = 40
rect_backgr = mpatches.FancyArrowPatch((x0, y_tail), (x0, y_head), arrowstyle=rect_style, color='white', zorder=0, transform=trans)
rect_backgr.set_clip_on(False)
rect = mpatches.FancyArrowPatch((x0, y_tail), (x0, y_head), arrowstyle=rect_style, fill=False, color='orange', hatch='///', transform=trans)
rect.set_clip_on(False)
ax.add_patch(rect_backgr)
ax.add_patch(rect)

line = mpatches.FancyArrowPatch((x0, 0), (x0, 80), arrowstyle=line_style, color='orange', transform=trans, zorder=-1)
line.set_clip_on(False)
ax.add_patch(line)

ax.set_xlim(0, 30)
ax.set_ylim(0, 80)
plt.show()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59670812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档