gpt4 book ai didi

python - 使用子图放大时间序列或我如何在轴边界外画线

转载 作者:太空狗 更新时间:2023-10-30 00:07:07 26 4
gpt4 key购买 nike

我想用 matplotlib 生成这样的图

timescale http://www.imagenetz.de/img.php?file=37d8879009.jpg&pid=

目前我只是使用 matplotlib 绘制 3 个子图,并在 inkscape 中添加红线。我发现我可以用 Rectangle 创建虚线矩形.

ax.add_patch(Rectangle((25, -1.4), 3, 1.3, edgecolor='red',
fill=False, linestyle='dashed'))

我还没有找到任何东西来画出连接地 block 的线条。是否有可以跨轴边界绘制的函数?

最佳答案

简答

我们可以利用 plt.annotate() 在图形坐标中的轴边界外绘制线。

长答案

首先定义一个辅助函数:

from matplotlib.patches import Rectangle

def zoomingBox(ax1, roi, ax2, color='red', linewidth=2, roiKwargs={}, arrowKwargs={}):
'''
**Notes (for reasons unknown to me)**
1. Sometimes the zorder of the axes need to be adjusted manually...
2. The figure fraction is accurate only with qt backend but not inline...
'''
roiKwargs = dict([('fill',False), ('linestyle','dashed'), ('color',color), ('linewidth',linewidth)] + roiKwargs.items())
ax1.add_patch(Rectangle([roi[0],roi[2]], roi[1]-roi[0], roi[3]-roi[2], **roiKwargs))
arrowKwargs = dict([('arrowstyle','-'), ('color',color), ('linewidth',linewidth)] + arrowKwargs.items())
srcCorners = [[roi[0],roi[2]], [roi[0],roi[3]], [roi[1],roi[2]], [roi[1],roi[3]]]
dstCorners = ax2.get_position().corners()
srcBB = ax1.get_position()
dstBB = ax2.get_position()
if (dstBB.min[0]>srcBB.max[0] and dstBB.max[1]<srcBB.min[1]) or (dstBB.max[0]<srcBB.min[0] and dstBB.min[1]>srcBB.max[1]):
src = [0, 3]; dst = [0, 3]
elif (dstBB.max[0]<srcBB.min[0] and dstBB.max[1]<srcBB.min[1]) or (dstBB.min[0]>srcBB.max[0] and dstBB.min[1]>srcBB.max[1]):
src = [1, 2]; dst = [1, 2]
elif dstBB.max[1] < srcBB.min[1]:
src = [0, 2]; dst = [1, 3]
elif dstBB.min[1] > srcBB.max[1]:
src = [1, 3]; dst = [0, 2]
elif dstBB.max[0] < srcBB.min[0]:
src = [0, 1]; dst = [2, 3]
elif dstBB.min[0] > srcBB.max[0]:
src = [2, 3]; dst = [0, 1]
for k in range(2):
ax1.annotate('', xy=dstCorners[dst[k]], xytext=srcCorners[src[k]], xycoords='figure fraction', textcoords='data', arrowprops=arrowKwargs)

然后我们可以这样做:

import matplotlib.pyplot as plt

axs = plt.subplots(2, 2)[1]
axs[1,1].plot(rand(100))
zoomingBox(axs[1,1], [40,60,0.1,0.9], axs[0,0])
zoomingBox(axs[1,1], [10,30,0.1,0.9], axs[1,0], color='orange')

enter image description here

关于python - 使用子图放大时间序列或我如何在轴边界外画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24477220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com