连接路径隐藏在subp后面

2024-09-30 08:17:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在两个对齐的子图上画一条线。因此,我使用其他答案中建议的matplotlib.patches.ConnectionPatch。它已经在其他例子中起作用了,但这里是第二次,这条线只是在第二个绘图区域被切断。你知道吗

如何确保ConnectionPatch绘制在前面?

我试着和佐德玩,但还没有找到解决办法。你知道吗

enter image description here

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

xes=[-2, 0, 2]
field=[0, -10, 0]
potential=[-20, 0, 20]

fig, axs = plt.subplots(2, 1, sharex=True)

axs[0].plot(xes, field)
axs[1].plot(xes, potential)

# line over both plots
_, ytop = axs[0].get_ylim()
ybot, _ = axs[1].get_ylim()
n_p_border = ConnectionPatch(xyA=(0., ytop), xyB=(0., ybot), 
                             coordsA='data', coordsB='data',
                             axesA=axs[0], axesB=axs[1], lw=3)
print(n_p_border)
axs[0].add_artist(n_p_border)

Tags: importfieldgetplotmatplotlibpltpotentialborder
1条回答
网友
1楼 · 发布于 2024-09-30 08:17:03

你需要反转两轴的作用。这也显示在Drawing lines between two plots in Matplotlib中。你知道吗

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

xes=[-2, 0, 2]
field=[0, -10, 0]
potential=[-20, 0, 20]

fig, axs = plt.subplots(2, 1, sharex=True)

axs[0].plot(xes, field)
axs[1].plot(xes, potential)

# line over both plots
_, ytop = axs[0].get_ylim()
ybot, _ = axs[1].get_ylim()
n_p_border = ConnectionPatch(xyA=(0., ybot), xyB=(0., ytop), 
                             coordsA='data', coordsB='data',
                             axesA=axs[1], axesB=axs[0], lw=3)

axs[1].add_artist(n_p_border)
plt.show()

enter image description here

相关问题 更多 >

    热门问题