简体   繁体   English

python matplotlib 中直方图的 zoomed_inset_axes 不起作用

[英]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.我一直在努力使用 matplotlib 工作来获得直方图的zoomed_iseet_axis

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这是output

You should make the same histogram in your inset plot.您应该在插图 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.目前,您在主图中使用 2 个箱( slk_plt.hist(x, 2, ... ),但在插图中使用 3 个箱( axins.hist(x, 3, ... ),因此插图将不对应到你的身材。

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM