简体   繁体   English

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

[英]How do I avoid overlap between bars in a multi-bar chart in matplotlib

I am trying to plot a multiple bar chart with variable x-values. 我正在尝试绘制具有可变x值的多个条形图。 My plot looks like this 我的情节看起来像这样

在此处输入图片说明

As you can see the bars from StartFFT_Gflops overlap the next tick on the x axis. 如您所见, StartFFT_Gflops的条与x轴上的下一个刻度重叠。 Is there some way I can increase the spacing between the ticks so that my bars don't overlap? 有什么办法可以增加刻度线之间的间距,以使我的柱线不重叠?

I am using the following code to plot 我正在使用以下代码进行绘制

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

As @cphlewis pointed out, the width between x-ticks is one data-value apart. 正如@cphlewis所指出的,x标记之间的宽度相隔一个数据值。 Dynamically generating the width helps to solve the overlap problem. 动态生成width有助于解决重叠问题。 Instead of having a static width = 0.35 I am now generating it using 我现在不是使用静态width = 0.35而是使用

width = 1.0/(num_items + 2)

Here is the final chart 这是最终的图表

不拥挤的多条形图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取 matplotlib 条形图中的所有条形? - How do I get all bars in a matplotlib bar chart? 在MatplotLib中创建多条plot - Creating a multi-bar plot in MatplotLib matplotlib当x轴相隔1周时,如何减少堆积条形图中条形之间的空间量? - matplotlib how do I reduce the amount of space between bars in a stacked bar chart when x-axis are dates 1-week apart? 我在matplotlib(python)中有一个带有误差条的条形图,但我希望误差条位于该条的中间。 我该怎么做呢? 谢谢 - I have a bar chart with error bars in matplotlib (python), but i want the error bars in the middle of the bar. how do i do this? Thanks 如何在刻度(matplotlib)之间对齐条形图中的条? - How to align the bars in a bar chart between ticks (matplotlib)? 如何消除 Matplotlib 条形图中条形之间的间隙 - How to remove gaps between bars in Matplotlib bar chart 如何在Matplotlib条形图中删除宽度不等的条之间的间隙 - How to remove gaps between bars with unequal widths in Matplotlib bar chart 图表 barh matplotlib - 重叠条 - Chart barh matplotlib - overlap bars 如何使用 matplotlib 在此条形图的相应条形上方显示这些值? 我的尝试不起作用 - How do I display these values above their respective bars on this bar chart with matplotlib? My attempts are not working 如何将第二个轴添加到 matplotlib/seaborn 条形图并使次要点与正确的条形对齐? - How do I add a second axis to a matplotlib/seaborn bar chart and have the secondary points align with the correct bars?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM