1

I tried to add space between 2 bars plot.bar([value for value in range(len(y))+0.25], z, width=0.4) but it was showing error "cant convert list to float"

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
  • this code worked for me plot.bar([value for value in range(len(x))],y,width=-0.3 , align='edge') plot.bar([value for value in range(len(x))], z, width=0.3, align='edge') Commented Oct 29, 2019 at 18:02

1 Answer 1

0

Do you want to separate the bars horizontally or vertically? For vertical separation, use `bottom:

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

Output:

enter image description here

Also, as you can notice, you can do plt.bar(range(len(x)), y) without list comprehension.

For horizontal shift:

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

You can also use np.arange(5) + 0.25:

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

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

need a horizontal gap and removing list makes my code different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.