自定义 inset_zoom 连接线

问题描述 投票:0回答:1

嘿,我是新来的,对 Python 编码和绘图也比较陌生,

我尝试使用 matplolib 的 inset_zoom 功能,但在连接线的定制方面遇到一些困难。我已经设法更改连接线的线型和线宽。现在我试图通过将 set.visible() 设置为 True 或 False 来更改绘制的默认线的选择。但它们并不是根本不显示设置的 False 连接线,而是以其原始线条样式绘制。此外,如果这些线没有连接矩形的相同角而是平行的侧角(见下文),我宁愿喜欢它。

这些情节是我的学士论文的,我的时间已经不多了,所以提前感谢您的帮助!

这是一个代码片段:

#inset axes
x1,x2,y1,y2 = 0, 0.2, 0, 0.2 # subregion of origanal image
axin1 = ax[1].inset_axes([0.56, 0.04, 0.4, 0.4], xlim=(x1,x2), ylim=(y1,y2), xticks=[], yticks=[], xticklabels=[], yticklabels=[])

axin1.plot(x_H2_real_test, x_H2_pred_test, '.', markersize = 4)
axin1.plot(x_NH3_real_test, x_NH3_pred_test, '.', markersize = 4)
axin1.plot([0, 1], [0, 1], "-", color = 'crimson',lw=1,zorder=1 )
axin1.plot([bounds[0],bounds[1]], [bounds[0] * 1.1, bounds[1] * 1.1], "k--", lw=1)
axin1.plot([bounds[0],bounds[1]], [bounds[0] * 0.9, bounds[1] * 0.9], "k--", lw=1)

box, c1 = ax[1].indicate_inset_zoom(axin1, edgecolor="black", alpha=1,lw=0.7)
c1[0].set_visible(False)
c1[1].set_visible(True)
c1[2].set_visible(True)
c1[3].set_visible(False)
ax[1].indicate_inset_zoom(axin1, edgecolor="black", alpha=1,lw=0.7)
plt.setp([c1[:]], linestyle=":", lw=0.7)

#plt.legend()
#fig.suptitle("Parity Plot")
plt.tight_layout()
plt.show()

现在的剧情:

读取线标记,我希望连接连接器线的位置:

matplotlib zooming
1个回答
0
投票

一种选择是使用一些

ConnectionPatch
实例创建您自己的连接,因为您可以准确控制它们的绘制位置。

由于我们不再需要原来的连接,所以我们现在可以使用

indicate_inset
而不是
indicate_inset_zoom
;为此,我们需要定义自己绘制的框的边界,我们可以使用插入轴的 x 和 y 限制来完成此操作。

以您的示例(没有数据绘图部分),此方法将如下所示:

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

fig, ax = plt.subplots(ncols=2)

#inset axes
x1,x2,y1,y2 = 0, 0.2, 0, 0.2 # subregion of origanal image
axin1 = ax[1].inset_axes([0.56, 0.04, 0.4, 0.4], xlim=(x1,x2), ylim=(y1,y2), xticks=[], yticks=[], xticklabels=[], yticklabels=[])

rect = (x1, y1, x2 - x1, y2 - y1)

box = ax[1].indicate_inset(rect, edgecolor="black", alpha=1,lw=0.7)

cp1 = ConnectionPatch(xyA=(0.2, 0.0), xyB=(0, 0), axesA=ax[1], axesB=axin1,
                      coordsA="data", coordsB="axes fraction", lw=0.7, ls=":")
cp2 = ConnectionPatch(xyA=(0.2, 0.2), xyB=(0, 1), axesA=ax[1], axesB=axin1,
                      coordsA="data", coordsB="axes fraction", lw=0.7, ls=":")

ax[1].add_patch(cp1)
ax[1].add_patch(cp2)

plt.tight_layout()
plt.show()

产生:

© www.soinside.com 2019 - 2024. All rights reserved.