如何避免matplotlib中多条形图中条形之间的重叠

时间:2015-05-05 23:52:13

标签: python matplotlib

我正在尝试使用可变x值绘制多条形图。我的情节看起来像这样

enter image description here

正如您所见,StartFFT_Gflops中的条形与x轴上的下一个刻度重叠。有什么方法可以增加刻度之间的间距,这样我的酒吧就不会重叠了吗?

我使用以下代码绘制

def plot_bars(hpcc_data, datapoints):

    fig, ax = plt.subplots()

    ind = np.arange(len(datapoints)) # array of x-ticks = no of datapoints to plot
    offset = 0 # offset distance between bars
    bar_legends = list()
    bar_obj = list()
    width = 0.35
    colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k'] # hopefully we won't need more than this
    color_idx = 0 

    for host in hpcc_data:
        host_data = hpcc_data[host]
        vals = list()
        for dp in datapoints:
            vals.append(host_data[dp])

        bar = ax.bar(ind + offset, vals, width, color=colors[color_idx])
        bar_legends.append(host)
        bar_obj.append(bar[0])
        color_idx += 1

        offset += width #for the next host



    ax.legend(bar_obj, bar_legends)
    ax.set_ylabel('Gflops')
    ax.set_title('Scores of hpcc benchmark')
    ax.set_xticks(ind + (len(hpcc_data.keys())/2)*width) #approx center the xticks
    ax.set_xticklabels(datapoints)

    plt.show()

1 个答案:

答案 0 :(得分:3)

正如@cphlewis指出的那样,x-ticks之间的宽度是一个数据值。动态生成width有助于解决重叠问题。而不是静态width = 0.35我现在使用

生成它

width = 1.0/(num_items + 2)

这是最终图表

uncrowded multi-bar chart

相关问题