簡體   English   中英

python 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM