简体   繁体   中英

zoomed_inset_axes for histogram in python matplotlib is not working

I have been struggling to get the zoomed_iseet_axis for histogram using matplotlib working.

Here is my code:

import matplotlib.pyplot as plt
 
x = [1,1,2,3,3,5,7,8,9,10,
     10,11,11,13,13,15,16,17,18,18,
     18,19,20,21,21,23,24,24,25,25,
     25,25,26,26,26,27,27,27,27,27,
     29,30,30,31,33,34,34,34,35,36,
     36,37,37,38,38,39,40,41,41,42,
     43,44,45,45,46,47,48,48,49,50,
     51,52,53,54,55,55,56,57,58,60,
     61,63,64,65,66,68,70,71,72,74,
     75,77,81,83,84,87,89,90,90,91
     ]

fig =plt.figure(figsize=(30,30), dpi=100)
slk_plt=fig.add_subplot(221)

n, num_of_bins, patches = slk_plt.hist(x, 2, color='#75A2BF',edgecolor='black', alpha=0.75)

axins = zoomed_inset_axes(slk_plt, 1.5, loc= 'lower left', bbox_to_anchor=(0,0), borderpad=3)

#bins = list(npy.arange(8,13,0.5))

n1, num_of_bins1, patches1 = axins.hist(x, 3, color='#75A2BF',edgecolor='black', alpha=0.75)

axins.set_xlim([40,80])

axins.set_ylim([30,50])

mark_inset(slk_plt, axins, loc1=2, loc2=4, fc="none", ec="0.5")

print(n1)

print(num_of_bins1)


plt.show()

Here is the output

You should make the same histogram in your inset plot.
Currently, you use 2 bins in your main figure ( slk_plt.hist(x, 2, ... ), but 3 bins in your inset ( axins.hist(x, 3, ... ), thus the inset will not correspond to your figure.

I have done this below. Note that, give your code, I can't reproduce your figure exactly, though close enough perhaps. And for practical reasons, I have reduced the figure size, and used only one subplot:

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

x = [1,1,2,3,3,5,7,8,9,10,
     10,11,11,13,13,15,16,17,18,18,
     18,19,20,21,21,23,24,24,25,25,
     25,25,26,26,26,27,27,27,27,27,
     29,30,30,31,33,34,34,34,35,36,
     36,37,37,38,38,39,40,41,41,42,
     43,44,45,45,46,47,48,48,49,50,
     51,52,53,54,55,55,56,57,58,60,
     61,63,64,65,66,68,70,71,72,74,
     75,77,81,83,84,87,89,90,90,91
     ]

fig =plt.figure(figsize=(6,6), dpi=100)
slk_plt=fig.add_subplot(111)
n, num_of_bins, patches = slk_plt.hist(x, 2, color='#75A2BF',edgecolor='black', alpha=0.75)
axins = zoomed_inset_axes(slk_plt, 1.5, loc= 'lower left', bbox_to_anchor=(0,0), borderpad=3)
n1, num_of_bins1, patches1 = axins.hist(x, 2, color='#75A2BF',edgecolor='black', alpha=0.75)
axins.set_xlim([40,80])
axins.set_ylim([30,50])
mark_inset(slk_plt, axins, loc1=2, loc2=4, fc="none", ec="0.5")

plt.show()

在此处输入图像描述

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