python图片顶端_python-在子图顶部绘制线条以呈现缩放效果

实际上,我发现了另一个与我想要的东西非常相似的主题:

Use subplots to zoom into timeseries or how I can draw lines outside of axis borders

所以这里的代码:

import matplotlib.pyplot as plt

import numpy as np

from matplotlib.patches import Rectangle

def zoomingBox(ax1, roi, ax2, color='red', linewidth=2):

ax1.add_patch(Rectangle([roi[0],roi[2]], roi[1]-roi[0], roi[3]-roi[2],**dict([('fill',False), ('linestyle','dashed'), ('color',color), ('linewidth',linewidth)]) ))

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.max[1]):

src = [0, 3]; dst = [0, 3]

elif (dstBB.max[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=dict([('arrowstyle','-'), ('color',color), ('linewidth',linewidth)]))

t=np.arange(1000)

x=np.sin(np.arange(1000)/10)

plt.figure()

ax1 = plt.subplot(2,1,1)

plt.plot(t,x)

ax2 = plt.subplot(2,2,3)

plt.plot(t[100:200],x[100:200])

ax3 = plt.subplot(2,2,4)

plt.plot(t[600:700],x[600:700])

zoomingBox(ax1, [100,200,-1.1,1.1], ax2)

zoomingBox(ax1, [600,700,-1.1,1.1], ax3)

plt.show()

这给出了:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值