简体   繁体   中英

Zoom in the pyplot selection with inset_axes

I have a plot consisting of multiple elements and I wish to have a selection enlarged with inset_axes . I have followed the manual and several other posts but it is only creating an empty square.

I have 1500 lines of code where I add elements to that plot at different places, thus I wish to create a zoom at the end to the whole plot.

Here is what is my output:

阴谋

and here is the code that leads to this plot (data omitted, too large)

import matplotlib.pyplot as plt



fig, ax = plt.subplots()

#  this is done in a separate function (called only once)
#  variables defined earlier, omitting - some variables are set manually, some are observed
for i_plot_buildings in range(0, len(Building_center_coords_main)):
    ax.plot(x_building_corners_main[i_plot_buildings], y_building_corners_main[i_plot_buildings], 'k-')
    plt.scatter(UEs_coordinates[:, 1], UEs_coordinates[:, 0], s=50, marker='.', c="c")

    plt.scatter(BSs_coordinates[:, 0], BSs_coordinates[:, 1], marker='^', c="r", zorder=3)
    ax.set_aspect('equal', adjustable='box')
    Plot_last_drop_indicator = 0

arry = np.empty((1000, 1000), int) #this fills with info about wind in later code (omitted) 
plt.imshow(arry, cmap=plt.cm.Greens, interpolation='nearest')

plt.ylabel("Y [m]")
plt.xlabel("X [m]")

bar = plt.colorbar()
bar.set_label(r'Wind speed $[ms^{-1}]$', rotation=270)  

#and now I want the zoom, not working... 
axins = ax.inset_axes([0.55, 0.55, 0.4, 0.4]) # set the area where to enlarge selection

# the selection
x1, x2, y1, y2 = 0,100,0,100
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
axins.set_xticklabels([])
axins.set_yticklabels([])

ax.indicate_inset_zoom(axins, edgecolor="black")

In case anyone would ever try to do some complex stuff like me I keep this question and provide answer I have managed to run. As mentioned by comments, you have to add a brand new plot inside the original plot with new data. Here is the code:

fig, ax = plt.subplots()

#  this is done in a separate function (called only once)
#  variables defined earlier, omitting - some variables are set manually, some are observed
for i_plot_buildings in range(0, len(Building_center_coords_main)):
    ax.plot(x_building_corners_main[i_plot_buildings], y_building_corners_main[i_plot_buildings], 'k-')
    plt.scatter(UEs_coordinates[:, 1], UEs_coordinates[:, 0], s=50, marker='.', c="c")

    plt.scatter(BSs_coordinates[:, 0], BSs_coordinates[:, 1], marker='^', c="r", zorder=3)
    ax.set_aspect('equal', adjustable='box')
    Plot_last_drop_indicator = 0

arry = np.empty((1000, 1000), int) #this fills with info about wind in later code (omitted) 
plt.imshow(arry, cmap=plt.cm.Greens, interpolation='nearest')

plt.ylabel("Y [m]")
plt.xlabel("X [m]")

bar = plt.colorbar()
bar.set_label(r'Wind speed $[ms^{-1}]$', rotation=270)  

# adding circles and crosses
plt.scatter(47, 64, marker='x',c="g")
circle1 = plt.Circle((47, 64), 8*4, color='g', fill=False)
plt.gca().add_patch(circle1)
plt.scatter(41, 56, marker='x',c="r")
circle1 = plt.Circle((41, 56), 6*4, color='r', fill=False)
plt.gca().add_patch(circle1)


plt.scatter(726, 672, marker='x',c="g")
plt.scatter(755, 658, marker='x',c="r")
circle1 = plt.Circle((726, 672), 9*4, color='g' ,fill=False)
plt.gca().add_patch(circle1)
circle1 = plt.Circle((755, 658), 7*4, color='r', fill=False)
plt.gca().add_patch(circle1)


plt.scatter(920, 51, marker='x',c="g")
circle1 = plt.Circle((920, 51), 8*4, color='g', fill=False)
plt.gca().add_patch(circle1)
plt.scatter(927, 41, marker='x',c="r")
circle1 = plt.Circle((927, 41), 5*4, color='r', fill=False)
plt.gca().add_patch(circle1)

#inverting y axis 
plt.gca().invert_yaxis()

#selecting parent axis for later use
parent_axes = plt.gca()

axins = ax.inset_axes([0.1, 0.5, 0.4, 0.4]) # enlargement area

# area to zoom
x1, x2, y1, y2 = 680,780,620,720
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)

# new ax 
ax3 = plt.gcf().add_axes([50,500,400,400])

# adding arry selection to the new plot
ax3.imshow(arry[680:780, 620:720], cmap=plt.cm.Greens, interpolation='nearest')

running through coordinates to select which to add (yes this is slow and can be done more smart but I am lazy) 
for coord in UEcoords:
    if coord[1] >= 680 and coord [1] < 780:
        if coord[0] >= 620 and coord [0] < 720:
            ax3.scatter(coord[1]-680, coord[0]-620, s=100, marker='.', c="c")

#show selected crosses and circles in area
ax3.scatter(726-680, 672-620, marker='x',c="g")
ax3.scatter(755-680, 658-620, marker='x',c="r")
circle1 = plt.Circle((726-680, 672-620), 9*4, color='g' ,fill=False)
ax3.add_patch(circle1)
circle1 =plt.Circle((755-680, 658-620), 7*4, color='r', fill=False)
ax3.add_patch(circle1)

#set limits and turn off labels
ax3.set_xlim(0,100)
ax3.set_ylim(0,100)
ax3.set_xticklabels([])
ax3.set_yticklabels([])

#set up inset position
ip = InsetPosition(parent_axes,[0.1, 0.5, 0.4, 0.4])
axins.set_axes_locator(ip)
axins.set_xticklabels([])
axins.set_yticklabels([])
# set the new axes (ax3) to the position of the linked axes
ax3.set_axes_locator(ip)

ax.indicate_inset_zoom(axins, edgecolor="black")

The final output looks like this:

输出

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