本文介绍了ConnectionPath隐藏在子图的后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在两个对齐的子图上标记一条线.因此,我按照其他答案中的建议使用 matplotlib.patches.ConnectionPatch .它已经在其他示例中起作用,但在这里第二次,该线在第二个绘图区域被切断.
如何确保将ConnectionPatch绘制在前面?
我尝试使用zorder,但尚未找到解决方案.
来自matplotlib.patches的 导入ConnectionPatch导入matplotlib.pyplot作为pltxes=[-2, 0, 2]字段=[0, -10, 0]电位= [-20,0,20]图, axs = plt.subplots(2, 1, sharex=True)axs[0].plot(xes, field)axs [1] .plot(xes,势能)# 在两个图上划线_,ytop = axs [0] .get_ylim()ybot,_ = axs [1] .get_ylim()n_p_border = ConnectionPatch(xyA=(0., ytop), xyB=(0., ybot),coordsA='data', coordsB='data',轴A=轴[0],轴B=轴[1],lw=3)打印(n_p_border)斧头[0] .add_artist(n_p_border) 解决方案
您需要颠倒两个轴的角色.这也显示在
I want to mark a line over two aligned subplots. Therefore, I use matplotlib.patches.ConnectionPatch as suggested in other answers. It worked already in other examples, but here for the second time, the line just is cut off at the second plot area.
How do I assure that the ConnectionPatch is plotted in the front?
I tried playing around with zorder, but did not find a solution yet.
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)解决方案
You would need to inverse the role of the two axes. This is also shown in 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()更多推荐
ConnectionPath隐藏在子图的后面
发布评论