python 连线图_Python matplotlib:连接两个子图

您可以在您的figure对象上使用lines。

优点是这些图形线的层次比轴上的高,所以不必为绘制轴而担心。

(我从这个stackoverflow的答案中得到了这个想法:ref)

应用于代码,如下所示:import matplotlib.pyplot as plt

import matplotlib as mpl

#x-axis

x_axis = [0,1,2,3,4,5,6,7,8,19,20,21,22,23,24,25]

# Line 1

line = [99,91,86,80,80,76,72,72,73,74,76,78,79,80,80,80]

# create broken x-axis

f, (ax, ax2) = plt.subplots(1, 2, sharey=True)

ax.step(x_axis, line, color='red')

ax2.step(x_axis, line, color='red')

ax.set_xlim(0, 10.5)

ax2.set_xlim(18.5, 30)

ax.spines['right'].set_visible(False)

ax.spines['top'].set_visible(False)

ax2.spines['left'].set_visible(False)

ax2.spines['top'].set_visible(False)

ax2.spines['right'].set_visible(False)

ax2.tick_params(

axis='y',

which='both',

left='off')

# seperators for x-axis

d = .015

kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)

ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)

kwargs.update(transform=ax2.transAxes)

ax2.plot ( (0-d ,0+d),(-d, +d ), **kwargs)

# transFigure: used to transform the coordinates from your subplots

# to coordinates on the figure

# points are hardcoded as an example

transFigure = f.transFigure.inverted()

# last point on your left figure

coord1 = transFigure.transform(ax.transData.transform([8,74]))

# first point on your right figure

coord2 = transFigure.transform(ax2.transData.transform([19,74]))

# generate your line with the transformed coordinates

line = mpl.lines.Line2D((coord1[0],coord2[0]),(coord1[1],coord2[1]),

transform=f.transFigure, color='red')

# add your line to the figure

f.lines.append(line)

plt.show()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值