Python 减少matplotlib中直方图的y记号间距

Python 减少matplotlib中直方图的y记号间距,python,matplotlib,histogram,Python,Matplotlib,Histogram,我正在使用matplotlib绘制直方图,其中每个箱子包含1个值。以下是一个示例: import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator def image(): # Generate a bunch of random data, here's an example TYPE1 = [128, 129, 132, 6, 136, 139, 12, 13, 142, 140]

我正在使用matplotlib绘制直方图,其中每个箱子包含1个值。以下是一个示例:

import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator


def image():
    # Generate a bunch of random data, here's an example
    TYPE1 = [128, 129, 132, 6, 136, 139, 12, 13, 142, 140]
    TYPE2 = [18, 147, 148, 23, 152, 26, 154, 156, 157, 158]
    TYPE3 = [159, 161, 165, 42, 44, 172, 176, 182, 188, 62]
    TYPE4 = [190, 193, 198, 199, 72, 77, 80, 82, 215, 216]
    TYPE5 = [218, 223, 95, 226, 101, 106, 108, 109, 113, 127]

    fig, ax = plt.subplots(1, 1)
    ax = plt.figure().gca()
    ax.yaxis.set_major_locator(MaxNLocator(integer=True))
    ax.hist([TYPE1, TYPE2, TYPE3, TYPE4, TYPE5],
            color=['white', 'white', '0.8', 'green', 'black'],
            label=['TYPE1', 'TYPE2', 'TYPE3', 'TYPE4', 'TYPE5'],
            stacked=True,
            bins=250)

    ax.legend(loc='upper right', bbox_to_anchor=(1, -0.15), fancybox=True)
    plt.tight_layout()
    plt.show()
如何将y轴上0和1之间的间距减小到实际高度的四分之一,使其看起来像是轴高度减小后的结果


您可以定义图形大小,以便使用较小的高度。此外,您不需要再次定义ax。我已注释掉多余的行。我还将使用关键字ncol=2为图例使用2列格式。看起来好多了

fig, ax = plt.subplots(1, 1, figsize=(6, 2.5))
#     ax = plt.figure().gca()
ax.yaxis.set_major_locator(MaxNLocator(integer=True))

ax.legend(loc='upper right', bbox_to_anchor=(1, -0.15), fancybox=True, ncol=2)

图,ax=plt.subplosfigsize=10,3和drop ax=plt.figure.gca?非常感谢。如果你愿意发布你的答案,我很乐意接受你的答案,因为在谢尔多的答案发布之前,你是第一个在这里的。@SudheeshSinganamalla:我们需要一分钟和第二个计数器@SudheeshSinganamalla请随意接受他的,反正是一样的。编码快乐。非常感谢你们两位。