Jupyter:如何绘制多个固定高度的直方图?

时间:2018-09-23 16:06:58

标签: python pandas matplotlib jupyter

我有一个包含多列的数据框,我想一次绘制它们的直方图。 DataFrame对象有一个非常不错的0参数,可以在每个轴上绘制每个变量:

subplots

给我:

3 histograms

问题是当我尝试绘制更多列时:

%matplotlib inline # same problem with %matplotlib notebook
df.iloc[:,-3:].plot.hist(subplots=True);

Ugly multiple columns

我希望能够设置每个轴的固定高度,并具有很大的图像,或者能够滚动查看它们。

如何做到?

1 个答案:

答案 0 :(得分:1)

一种方法是通过在绘图之前调用plt.subplots()来扩展图形的垂直尺寸,如下所示:

nvars = 12  # Example number of variables

# subplots(number of vertically stacked axis positions,
#          number of horizontally stacked axis positions,
#          figsize=(width, height))
fig, ax = plt.subplots(nvars, 1, figsize=(6, 4*nvars))

# Need to pass axis handle to df.plot()
df.iloc[:,-nvars:].plot.hist(subplots=True, ax=ax);