【问题标题】:In a matplotlib plot consisting of histogram subplots, how can the height and bar edges of one histogram be changed?在由直方图子图组成的 matplotlib 图中,如何更改一个直方图的高度和条形边缘?
【发布时间】:2016-01-21 11:00:22
【问题描述】:

我有一个小函数可以生成一个包含两个子图的图。一个子图是两个直方图重叠,另一个子图是一个直方图除以另一个的结果。

对于第二个子图,我不知道如何删除直方图条之间的边缘(就像它上面的那个),我不知道如何降低它的高度(例如,它是高度的一半上面的那个)。我也不确定如何将标题设置为情节的最顶部。

这些事情是怎么做到的?

我的代码如下:

import numpy
import matplotlib.pyplot
import datavision # sudo pip install datavision
import shijian    # sudo pip install shijian

def main():

    a = numpy.random.normal(2, 2, size = 120)
    b = numpy.random.normal(2, 2, size = 120)

    save_histogram_comparison_matplotlib(
        values_1      = a,
        values_2      = b,
        label_1       = "a",
        label_2       = "b",
        normalize     = True,
        label_ratio_x = "frequency",
        label_y       = "",
        title         = "comparison of a and b",
        filename      = "test.png"
    )

def save_histogram_comparison_matplotlib(
    values_1       = None,
    values_2       = None,
    filename       = None,
    number_of_bins = None,
    normalize      = True,
    label_x        = "",
    label_y        = None,
    label_ratio_x  = "frequency",
    label_ratio_y  = "ratio",
    title          = None,
    label_1        = "1",
    label_2        = "2",
    overwrite      = True,
    LaTeX          = False
    ):

    matplotlib.pyplot.ioff()
    if LaTeX is True:
        matplotlib.pyplot.rc("text", usetex = True)
        matplotlib.pyplot.rc("font", family = "serif")
    if number_of_bins is None:
        number_of_bins_1 = datavision.propose_number_of_bins(values_1)
        number_of_bins_2 = datavision.propose_number_of_bins(values_2)
        number_of_bins   = int((number_of_bins_1 + number_of_bins_2) / 2)
    if filename is None:
        filename = shijian.propose_filename(
            filename  = title.replace(" ", "_") + ".png",
            overwrite = overwrite
        )

    values = []
    values.append(values_1)
    values.append(values_2)
    bar_width = 0.8
    figure, (axis_1, axis_2) = matplotlib.pyplot.subplots(nrows = 2)
    ns, bins, patches = axis_1.hist(
        values,
        normed    = normalize,
        histtype  = "stepfilled",
        bins      = number_of_bins,
        alpha     = 0.5,
        label     = [label_1, label_2],
        rwidth    = bar_width,
        linewidth = 0
    )
    axis_1.legend()
    axis_2.bar(
        bins[:-1],
        ns[0] / ns[1],
        edgecolor = "#ffffff", # "none"
        alpha = 1,
        width = bins[1] - bins[0]
    )
    axis_1.set_xlabel(label_x)
    axis_1.set_ylabel(label_y)
    axis_2.set_xlabel(label_ratio_x)
    axis_2.set_ylabel(label_ratio_y)
    matplotlib.pyplot.title(title)
    matplotlib.pyplot.savefig(filename)
    matplotlib.pyplot.close()

if __name__ == "__main__":
    main()

【问题讨论】:

    标签: matplotlib histogram


    【解决方案1】:

    您有 3 个问题:

    1。如何去除直方图条之间的边缘

    在这里,您可以将linewidth 设置为0 以调用bar

    axis_2.bar(
        bins[:-1],
        ns[0] / ns[1],
        linewidth=0,
        alpha = 1,
        width = bins[1] - bins[0]
    )
    

    2。如何降低第二个子图的高度

    在这里,我们可以在创建子图时将 kwargs 发送到gridspec。相关选项是height_ratios。我们使用gridspec_kw 选项将它们发送到subplots。如果我们将其设置为 (2,1),则第一个子图的高度是第二个子图的两倍。

    figure, (axis_1, axis_2) = matplotlib.pyplot.subplots(
        nrows = 2,
        gridspec_kw={'height_ratios':(2,1)}
        )
    

    3。如何将标题设置到情节的最顶部

    当您调用matplotlib.pyplot.title(title) 时,实际上是在设置当前活动子图轴的标题,在本例中为axis_2。设置整体图的标题,可以设置suptitle

    matplotlib.pyplot.suptitle(title)
    

    或者,由于您已经命名了您的人物,您可以使用:

    figure.suptitle(title)
    

    同样,您可以使用:

    figure.savefig(filename)
    

    节省一些击键次数。


    把它们放在一起:

    【讨论】:

      猜你喜欢
      • 2012-07-15
      • 1970-01-01
      • 2021-02-19
      • 2020-09-12
      • 2017-09-25
      • 2019-07-07
      • 1970-01-01
      • 2022-01-10
      • 2018-01-12
      相关资源
      最近更新 更多