Python matplotlib : connect two subplot diagrams

标签 python matplotlib connect diagram

我尝试创建一个 X 轴断开的图表。我使用了可以在 matplotlib 页面上找到的示例。

我的问题是:如何连接两个图?我需要图形的闭合线(并且两个 x 轴应该更接近)。

有办法得到这个吗?

import matplotlib.pyplot as plt

#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)

plt.show()

有没有办法得到“一个”图表?

Current diagramm of 2 subplots (png)

最佳答案

您可以在图形 对象上使用线条。 优点是这些图形线比轴上的线层次更高,所以不用担心为它画轴。 (我从这个 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()

enter image description here

关于Python matplotlib : connect two subplot diagrams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44010105/

相关文章:

python - 一个图例的两个独特标记符号

node.js - 简单的 Expressjs 应用程序没有响应

c++ - 使用后台 api 在安全模式下连接到 word

python - 如何格式化缺少日期的 x 轴时间序列刻度线

php - 启动进程,从 ps 隐藏参数

python - 如何在 MouseOver 上更改 wx.Panel 背景颜色?

python - 如何从 tkinter 条目为另一个函数生成位置参数

Python直方图误差条

javascript - 防止 favicon.ico 发出第二个请求 - Node.js

python - 如何在使用gunicorn的Google App Engine上运行长任务?