[英]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.