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

标签 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()

最佳答案

正如@cphlewis 所指出的,x 刻度之间的宽度相隔一个数据值。动态生成 width 有助于解决重叠问题。我现在使用

生成它,而不是静态的 width = 0.35

宽度 = 1.0/(num_items + 2)

这是最终的图表

uncrowded multi-bar chart

关于python - 如何避免 matplotlib 中多条形图中条形之间的重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30065160/

相关文章:

python - 如何使用seaborn防止条形图重叠?

python - matplotlib savefig 线程安全吗?

python - 如何在 matplotlib (python) 中标记一条线?

python - 将总和中的数字替换为零

python - 防止在 Python 中四舍五入为零

python - 将 cython cdef 扩展数组设置为零

python - 在 2d 动画上绘制大小和位置可变的圆圈

python - 使用 Mypy 本地 stub

Python 按日期范围求和值

python - 尝试将 matplotlib 与 ABAQUS 结合使用。收到涉及 dateutil 的错误