SaintTail
SaintTail

Reputation: 6470

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

enter image description here

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?

Upvotes: 2

Views: 2174

Answers (1)

Amy Teegarden
Amy Teegarden

Reputation: 3972

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

Graph with mark_inset box above plotted line

Upvotes: 4

Related Questions