简体   繁体   中英

Box gets truncated using zoomed_inset_axes

I want to use zoomed_inset_axes but the box gets truncated as soon as it passes the frame of the main figure. I could not get any better with

  • f.tight_layout()
  • f.subplots_adjust(bottom=...)
  • 'figure.autolayout': True
  • not even with hidden (white) text using f.text somewhere outside.

Does anyone know how to do this properly?

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

X = np.random.normal(.5,10,1000)
Y = np.random.normal(.5,10,1000)

f, ax = plt.subplots(1, figsize=(10,6))

ax.scatter(X,Y)

# # Setup zoom window
axins = zoomed_inset_axes(ax, 2, loc="center", bbox_to_anchor=(0,0))
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
axins.set_xlim([-15,0])
axins.set_ylim([-12,-3])

# # Plot zoom window
axins.scatter(X,Y)

f.tight_layout()
f.savefig('test.png', dpi=70)

插图被截断

Using subplots_adjust goes in the right direction. Don't use tight_layout afterwards as this would overwrite any settings done via subplots_adjust .

You may decide to opt for something like

fig.subplots_adjust(left=0.2, bottom=0.2)

to make some space for the inset in the lower left corner of the figure.

Then you need to position the inset. Since here you're working in the lower left corner, this is relatively easy. The loc parameter needs to be set to the lower left corner and you may stick to the bbox_to_anchor=(0,0) position. Then just add some padding via borderpad=3 (in units of font size), such that the inset axes' labels are still visible,

zoomed_inset_axes(ax, 2, loc='lower left', bbox_to_anchor=(0,0), borderpad=3)

Complete code:

 import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset X = np.random.normal(.5,10,1000) Y = np.random.normal(.5,10,1000) fig, ax = plt.subplots(1, figsize=(10,6)) fig.subplots_adjust(left=0.2, bottom=0.2) ax.scatter(X,Y) # # Setup zoom window axins = zoomed_inset_axes(ax, 2, loc='lower left', bbox_to_anchor=(0,0), borderpad=3) mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") axins.set_xlim([-15,0]) axins.set_ylim([-12,-3]) # # Plot zoom window axins.scatter(X,Y) #fig.savefig('test.png', dpi=70) plt.show() 

在此处输入图片说明

In general, you have a lot of options to position and size the inset. I recently created a new example on the matplotlib page: Inset Locator Demo , which is currently only available in the devdocs, to show the interplay between the different parameters (in that case for inset_axes - but it totally applies to zoomed_inset_axes as well).

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