[英]Having just one legend when using matplotlib zoomed_inset_axes
[英]zoomed_inset_axes for histogram in python matplotlib is not working
我一直在努力使用 matplotlib 工作来获得直方图的zoomed_iseet_axis
。
这是我的代码:
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()
这是output
您应该在插图 plot 中制作相同的直方图。
目前,您在主图中使用 2 个箱( slk_plt.hist(x, 2, ...
),但在插图中使用 3 个箱( axins.hist(x, 3, ...
),因此插图将不对应到你的身材。
我在下面做了这个。 请注意,给出您的代码,我无法准确地重现您的数字,尽管可能足够接近。 出于实际原因,我减小了图形大小,只使用了一个子图:
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.