Skip to content Skip to sidebar Skip to footer

Python - How To Arrange Multiple Histograms In A Grid

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_

Solution 1:

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

enter image description here


Post a Comment for "Python - How To Arrange Multiple Histograms In A Grid"