我有这段代码
ax = plt.subplot(222)
plt.plot(time_list, data[1], color='red')
plt.plot(time_list, y_offset, color='blue')
plt.axvline(x=0, color='black')
plt.axhline(y=0, color='black')
axins = zoomed_inset_axes(ax, 4.5, loc=4)
axins.plot(time_list, data[1], color='red')
axins.plot(time_list, y_offset, color='blue')
axins.axvline(x=0, color='black')
axins.axhline(y=0, color='black')
axins.axis([2, 3, -0.01, 0.01])
plt.yticks(visible=False)
plt.xticks(visible=False)
mark_inset(ax, axins, loc1=3, loc2=1, fc="none", ec="0.0")
绘制这样的图表
正如您所看到的那样,缩放框线位于红色图的后面,但链接线位于图的顶部,那么如何让缩放框线成为图的顶部?
答案 0 :(得分:2)
您可以使用Z-order使框显示在图形线的顶部。具有较高Z次序的艺术家被绘制在具有较低Z次序的艺术家之上。行的默认值为2,因此将zorder = 3
添加到mark_inset
。
完整代码:
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset
fig, ax = plt.subplots()
time_list = np.linspace(0, 7, 1000)
data = np.random.random(1000) - .5
plt.plot(time_list, data, color='red')
plt.axvline(x=0, color='black')
plt.axhline(y=0, color='black')
axins = zoomed_inset_axes(ax, 4.5, loc=4)
axins.plot(time_list, data, color='red')
axins.axvline(x=0, color='black')
axins.axhline(y=0, color='black')
axins.axis([2, 3, -0.01, 0.01])
plt.yticks(visible=False)
plt.xticks(visible=False)
mark_inset(ax, axins, loc1=3, loc2=1, fc="none", ec="0.0", zorder = 3)
plt.show()