Python-如何在网格中排列多个直方图 [英] Python - How to arrange multiple histograms in a grid

查看:425
本文介绍了Python-如何在网格中排列多个直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码从numpy ndarray中读取每一行,并在同一图形上创建多个直方图:

The following code read each row from a numpy ndarray and create multiple histograms on the same figure:

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的网格中?

Instead of including all histograms in one plot, how can I arrange them in a grid 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将图放置在网格"中的不同单元"中

You have to use different ax to put plot in different "cell" in "grid"

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

这篇关于Python-如何在网格中排列多个直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆