简体   繁体   中英

Matplotlib: How to copy the line2D objects to the Zoom region using 'inset axes'

According to the matplotlib documents, I write a simple sample that shows the use of 'Zoom region inset axes'.

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt  
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import numpy as np


y_1 = np.random.rand(10)
y_2 = np.random.rand(10)
y_3 = np.random.rand(10)

fig = plt.figure(1,figsize=(8,6)) 
ax = fig.add_subplot(1,1,1)

ax.plot(y_1)
ax.plot(y_2)
ax.plot(y_3)


ax.set_ylim(-0.5, 3)

# inset axes
axins = inset_axes(ax, width=2, height=2, loc=1)

# replotting
axins.plot(y_1)
axins.plot(y_2)
axins.plot(y_3)

xlim_1, xlim_2, ylim_1, ylim_2 = 2, 4, 0, 1
axins.set_xlim(xlim_1, xlim_2)
axins.set_ylim(ylim_1, ylim_2)
mark_inset(ax, axins, loc1=3, loc2=2, fc="none", ec="0",lw=1.5)

enter image description here

I want to copy the line2D objects to the Zoom region use 'ax.add_line()' instead of replotting. That is

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt  
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import numpy as np


y_1 = np.random.rand(10)
y_2 = np.random.rand(10)
y_3 = np.random.rand(10)

fig = plt.figure(1,figsize=(8,6)) 
ax = fig.add_subplot(1,1,1)

line1 = ax.plot(y_1)
line2 = ax.plot(y_2)
line3 = ax.plot(y_3)


ax.set_ylim(-0.5, 3)

# inset axes
axins = inset_axes(ax, width=2, height=2, loc=1)

# add line
axins.add_line(line1)
axins.add_line(line2)
axins.add_line(line3)

xlim_1, xlim_2, ylim_1, ylim_2 = 2, 4, 0, 1
axins.set_xlim(xlim_1, xlim_2)
axins.set_ylim(ylim_1, ylim_2)
mark_inset(ax, axins, loc1=3, loc2=2, fc="none", ec="0",lw=1.5)

However, something was wrong:

list' object has no attribute 'set_figure'

So how should I modify the code?

I have tried another method though it isn't perfect.

import matplotlib.pyplot as plt  
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import numpy as np


y_1 = np.random.rand(10)
y_2 = np.random.rand(10)
y_3 = np.random.rand(10)

fig = plt.figure(1,figsize=(8,6)) 
ax = fig.add_subplot(1,1,1)

def demo_plot(ax):
    ax.plot(y_1)
    ax.plot(y_2)
    ax.plot(y_3)
if __name__ == "__main__":
    demo_plot(ax)

ax.set_ylim(-0.5, 3)

# inset axes
axins = inset_axes(ax, width=2, height=2, loc=1)

# 
demo_plot(axins)

xlim_1, xlim_2, ylim_1, ylim_2 = 2, 4, 0, 1
axins.set_xlim(xlim_1, xlim_2)
axins.set_ylim(ylim_1, ylim_2)
mark_inset(ax, axins, loc1=3, loc2=2, fc="none", ec="0",lw=1.5)

This was a problem I ran into recently and answered on a separate StackOverflow question. The solution completely avoids copying lines, and reuses them (so it should be even more useful, although it might be overkill).

Link to the solution: https://stackoverflow.com/a/62318872/13703745

It is quite a bit of code, so I recommend saving the code to a python file and importing it as I do in the example in the answer.

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