[英]Python - How to arrange multiple histograms in a grid
以下代碼從numpy ndarray讀取每一行,並在同一圖形上創建多個直方圖:
fig, ax = plt.subplots(figsize=(10, 8))
fontP = FontProperties()
fontP.set_size('small')
for f in eval_list:
local_id = getIndexByIdentifier(f)
temp_sim = total_sim[local_id,:]
c=np.random.rand(3,1)
ax.hist(temp_sim, 10, ec=c, fc='none', lw=1.5, histtype='step', label=f)
ax.legend(loc="upper left", bbox_to_anchor=(1.1,1.1),prop = fontP)
而不是將所有直方圖包含在一個圖中,我如何將它們布置在5 x 5的網格中?
這是更新的代碼:
#Plot indvidual Histograms
SIZE = 10
all_data=[]
for f in eval_list:
local_id = getIndexByIdentifier(f)
temp_sim = total_sim[local_id,:]
all_data.append(temp_sim)
# create grid 10x10
fi, axi = plt.subplots(SIZE, SIZE,figsize=(50,50))
for idx, data in enumerate(all_data):
x = idx % SIZE
y = idx // SIZE
axi[y, x].hist(data)
您必須使用不同的ax
將圖放置在“網格”的不同“單元”中
import matplotlib.pyplot as plt
import random
SIZE = 5
# random data
all_data = []
for x in range(SIZE*SIZE):
all_data.append([ random.randint(0,10) for _ in range(10) ])
# create grid 5x5
f, ax = plt.subplots(SIZE, SIZE)
# put data in different cell
for idx, data in enumerate(all_data):
x = idx % SIZE
y = idx // SIZE
ax[y, x].hist(data)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.