简体   繁体   English

如何使matplotlib mark_inset在图形顶部显示线?

[英]How can I make matplotlib mark_inset display line on top of the graph?

I have this code 我有这个代码

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

which plot the graph like this 这样绘制图

在此处输入图片说明

as you can see the zoom box line is behind the red plot but the link line is on top of the plot so how can I make the zoom box line become on top of the plot? 如您所见,缩放框线位于红色图的后面,而链接线位于图的顶部,那么如何使缩放框线成为在图的顶部?

You can cause the box to appear on top of the graphed line by using Z-order . 您可以使用Z-order使框显示在图形线的顶部。 Artists with higher Z-order are plotted on top of artists with lower Z-order. Z顺序较高的艺术家被绘制在Z顺序较低的艺术家的顶部。 The default for lines is 2, so add zorder = 3 to mark_inset . 行的默认值为2,因此将zorder = 3添加到mark_inset

Full code: 完整代码:

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

在标绘线上方带有mark_inset框的图形

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM