如何在2条形图python之间添加空间

时间:2019-10-29 16:25:27

标签: python-3.x pandas matplotlib ggplot2 python-ggplot

我尝试在2个小节之间添加空格 plot.bar([[value(范围(len(y))+ 0.25]中的值,z,宽度= 0.4) 但显示错误“无法将列表转换为浮动”

x = x[xleftpos:xrightpos]
y = y[xleftpos:xrightpos]
z = z[xleftpos:xrightpos]


plot.bar([value for value in range(len(x))],y,width=0.4,)

plot.bar([value for value in range(len(x))], z, width=0.4)
plot.set_xticks([idx + 0.5 for idx in range(len(x))])
plot.set_xticklabels(x, rotation=35, ha='right', size=10)
xlocs = [idx + 0.5 for idx in range(len(x))]
for i, v in enumerate(y):
    plt.text(xlocs[i] - 0.99, v - 0.98, str(v))

plt.show()

enter image description here

1 个答案:

答案 0 :(得分:0)

您要水平或垂直分隔条形图吗?对于垂直分隔,请使用`bottom:

plt.bar(range(5), range(5))
plt.bar(range(5), range(-5,0), bottom=-0.25)

输出:

enter image description here

此外,您会注意到,您可以进行plt.bar(range(len(x)), y),而无需理解列表。

水平移动:

plt.bar([x+.25 for x in range(5)], range(5))
plt.bar(range(5), range(-5,0), bottom=-0.25)

您也可以使用np.arange(5) + 0.25

plt.bar(np.arange(5) + 0.25, range(5))
plt.bar(range(5), range(-5,0), bottom=-0.25)

输出:

enter image description here