【问题标题】:Jupyter: how to plot multiple histograms each with a fixed height?Jupyter:如何绘制多个具有固定高度的直方图?
【发布时间】:2018-09-23 16:06:57
【问题描述】:

我有一个包含多列的数据框,我想一次绘制所有列的直方图。 DataFrame 对象有一个非常好的subplots 参数,可以在其自己的轴上绘制每个变量:

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

给我:

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

%matplotlib inline
# 15 variables
df.iloc[:,-15:].plot.hist(subplots=True);

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

怎么做?

【问题讨论】:

    标签: python pandas matplotlib jupyter


    【解决方案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);
    

    【讨论】:

    • 不应该是df.iloc[:,-nvars:]吗?
    猜你喜欢
    • 2018-05-07
    • 2013-01-21
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2016-07-23
    相关资源
    最近更新 更多