简体   繁体   中英

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 . Artists with higher Z-order are plotted on top of artists with lower Z-order. The default for lines is 2, so add zorder = 3 to 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框的图形

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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