【问题标题】:Multiple bars in one bar plot一个条形图中的多个条形图
【发布时间】:2020-09-18 03:21:05
【问题描述】:

我想在同一个条形图中“组合”这些图。

这是我生成这些图形的代码

import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = (13,6)

def generate_histogram(distribucion):
    values = list(distribucion.values())
    intensities = list(distribucion.keys())

    # plotting configuration
    figure = plt.figure()
    axes = figure.add_axes([0,0,1,1])
    histogram = axes.bar(intensities,values,width=14)
    plt.xticks(intensities,intensities)

    # title and label
    axes.set_title('Intensity distribution')
    axes.set_xlabel('Intensity (0 to 255)')
    axes.set_ylabel('Probability')

    # fix scale
    plt.ylim(0, 0.55)
    plt.autoscale(False)

    # function that make the labels
    def autolabel(rects):
        """Attach a text label above each bar in *rects*, displaying its height."""
        for rect in rects:
            height = round((float)(rect.get_height()),4)
            axes.annotate('{}'.format(height),
                        xy=(rect.get_x() + rect.get_width() / 2, height),
                        xytext=(0, 3),  # 3 points vertical offset
                        textcoords="offset points",
                        ha='center', va='bottom',fontsize=10)

    # generate autolabels
    autolabel(histogram)

    # show graph
    plt.show()

generate_histogram(distribution_1)
generate_histogram(distribution_2)
generate_histogram(distribution_3)

这是我正在使用的数据

distribution_1 = {221: 0.360416255051639, 238: 0.19880422092501124, 204: 0.08321239335428827, 187: 0.05222900763358779, 170: 0.048701841041760216, 153: 0.04666771441400988, 136: 0.04238796587337225, 119: 0.03527929950606197, 255: 0.008626852267624607, 102: 0.029128423888639426, 85: 0.025297709923664122, 68: 0.025161652447238437, 51: 0.02414683430624158, 34: 0.015194881005837449, 17: 0.004271216883700045, 0: 0.00047373147732375395}
distribution_2 = {221: 0.4157265379434216, 238: 0.19262191288729233, 204: 0.07130848675348002, 187: 0.04102649303996408, 170: 0.041006286484059275, 153: 0.04099775482712169, 136: 0.03805253704535249, 119: 0.03213920071845532, 102: 0.0272240682532555, 255: 0.007630893578805568, 85: 0.024198473282442748, 68: 0.024454422990570275, 51: 0.023817691962281097, 34: 0.015106870229007634, 17: 0.004220925011225864, 0: 0.00046744499326448137}
distribution_3 = {255: 0.4824301751234845, 221: 0.0699272563987427, 187: 0.06918679838347552, 170: 0.050990121239335426, 153: 0.04777503367759318, 238: 0.024907049842837897, 136: 0.04586124831612034, 119: 0.041772339470139204, 102: 0.034856757970363715, 85: 0.022533453075886844, 68: 0.03175348001796138, 51: 0.025796587337224966, 34: 0.02501930848675348, 17: 0.015535698248765155, 0: 0.008881903906600808, 204: 0.002772788504714863}

我想要这样的东西,标签转 90 度,这样它们就不会重叠

【问题讨论】:

  • 你能分享你使用的数据吗?
  • 抱歉耽搁了。我已经编辑过了

标签: python matplotlib


【解决方案1】:

这个函数应该在另一个函数之外,所以它不是每次都被定义。

# function that make the labels
def autolabel(rects,axes):
    """Attach a text label above each bar in *rects*, displaying its height."""
    for rect in rects:
        height = round((float)(rect.get_height()),4)
        axes.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom',fontsize=10,rotation=90)

然后你必须将两组 x 值偏移条形的宽度:

  1. 第一组偏移 -.1(向左)
  2. 第二组没有偏移
  3. 第三组偏移 +.1(向右)
def generate_histogram(distribucion,axes=None,figure=None,h_adjustment=None):
    values = list(distribucion.values())
    intensities = list(distribucion.keys())
    width = .1

    # plotting configuration
    if not figure:
        figure = plt.figure(figsize=(13,6))
        axes = figure.add_axes([0,0,1,1])
    histogram = axes.bar([intensity + h_adjustment 
                          for intensity in intensities],values,width)
    #plt.xticks(intensities,intensities)

    # title and label
    axes.set_title('Intensity distribution')
    axes.set_xlabel('Intensity (0 to 255)')
    axes.set_ylabel('Probability')

    # fix scale
    plt.ylim(0, 0.55)
    plt.autoscale(False)

    # generate autolabels
    autolabel(histogram,axes)

    return figure, axes

这样调用函数:

a = {8:.5,7:.2,10:.11,6:.3}
figure,axes = generate_histogram(a,h_adjustment=-.1) 
b = {10:.35,7:.23,8:.25,6:.4}
figure,axes = generate_histogram(b,figure=figure,axes=axes,h_adjustment=0)
c = {10:.25,8:.44,9:.5,6:.45}
generate_histogram(c,figure=figure,axes=axes,h_adjustment=.1)

plt.show()

结果:

【讨论】:

  • 嗨,我尝试了这段代码并打印了一个小图像。我不知道为什么
  • @Mattii:我更新了代码,请再试一次。谢谢。
  • 还是一样。也许它与谷歌colab有关?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 2016-12-12
  • 1970-01-01
相关资源
最近更新 更多