0

I might do something wrong (actually I most certainly do) but I do not know, why the xticks in my barplot do not align with the axis. Here is my code:

from matplotlib import pyplot as plt
cond1 = '455 nm'
cond2 = 'Dark'
temp = nodubs[nodubs.group == cond1]
bar1 = temp.value
yerr1 = [np.zeros(len(temp.stdev)),temp.stdev]
temp = nodubs[nodubs.group == cond2]
bar2 = temp.value
yerr2 = [np.zeros(len(temp.stdev)),temp.stdev]
r1 = np.arange(len(bar1))
bwidth = 0.3
r2 = [x + bwidth for x in r1]
plt.bar(r1, bar1, width = bwidth, color = 'blue', edgecolor = 'black', capsize=2, label=cond1, yerr = yerr1)
plt.bar(r2, bar2, width = bwidth, color = 'cyan', edgecolor = 'black', capsize=2, label=cond2, yerr = yerr2)
plt.xticks(r1, temp.condition, rotation=45)
plt.ylabel('SEAP/(U L^-1)')
plt.legend()
#plt.ylim((0,0.7))
plt.savefig('figure.png', dpi=600,bbox_inches='tight')
plt.show()

And here a picture of the plot:
Plot

In this example r1 is: [ 0 1 2 3 4 5 6 7 8 9 10 11]

and temp.condition:

7                               p.....(s)
15                     p........ + p.....
23                     p........ + p.....
31            p........ + p..... + p.....
39            p........ + p..... + p.....
47            p........ + p..... + p.....
55            p........ + p..... + p.....
63            p........ + p..... + p.....
71                              p........
79                     p........ + p.....
87            p........ + p..... + p.....
95    p........ + p............. + p.....
Name: condition, dtype: object

As plt.xticks() and plt.bar() receive the same location parameter, shouldn't they align with each other?

Thanks for any help Carroll

  • The long labels are probably the reason for your problem. Their center is aligned with the x-ticks. You can see this in your unfortunately cropped image - the short labels ending on (s) are before longer labels on the x-axis. – Mr. T Dec 19 '20 at 18:56
  • Okay, thanks for the input. I'll figure something out to get around the long labels. –  Dec 19 '20 at 19:04
  • Just add ` ha='right'` to `plt.xticks()`. – Mr. T Dec 19 '20 at 19:05
  • That works great! Thank you :) Do you want to add it as an answer, so I can flag it as resolved? EDIT: Saw it - sorry. –  Dec 19 '20 at 19:11
  • No, I marked it as a duplicate. – Mr. T Dec 19 '20 at 19:13

0 Answers0