且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在matplotlib/pyplot/numpy中使用hist()函数创建图片的子图?

更新时间:2023-11-21 16:04:46

除非您还不熟悉它,否则应该看看

Unless you are not already familiar with it, you should have a look at the matplotlib gallery.
There you will find lots of examples hot to use the add_subplot and subplots commands.

您可能想要实现的目标的一个最小示例是

A minimal example of what you propably want to achieve is

import matplotlib.pyplot as plt
import numpy as np

data=np.random.random((4,10))
xaxes = ['x1','x2','x3','x4']
yaxes = ['y1','y2','y3','y4']
titles = ['t1','t2','t3','t4'] 

f,a = plt.subplots(2,2)
a = a.ravel()
for idx,ax in enumerate(a):
    ax.hist(data[idx])
    ax.set_title(titles[idx])
    ax.set_xlabel(xaxes[idx])
    ax.set_ylabel(yaxes[idx])
plt.tight_layout()

这将导致像

which results in a plot like