0

I am drawing a bar plot.

All the bars should have the same width. This is specified in the code.

Actually, some bars have different width and I can't understand why.

Here my reproducible code:

var_width=1
offset=1.5
x_ax=np.arange(0, 254*8,8)

x_ax1=x_ax+offset

random_seed=np.random.seed(20)
random_2=np.random.randint(0,100,size=len(x_ax))
fig, ax = plt.subplots(figsize=(55,10))
ax.bar(x_ax1, random_2, width=var_width, color='blue', label='Budget19')

ax.set_xticks(x_ax1)
ax.set_xticklabels(x_ax1,rotation=90)
ax.legend()

plt.show()

zoom out of the output

zoom in of the output

Andrea Ciufo
  • 359
  • 1
  • 3
  • 19
  • 2
    This is due to pixel snapping. A bar can either be 2 pixels wide or 3, but nothing in between. So when your bar width comes out to be 2.5 pixels, half of the bars will be 2 pixels and half will be 3 pixels wide. This can be circumvented by using a much larger dpi. – ImportanceOfBeingErnest Jan 10 '20 at 17:00
  • Thanks, as usually :) I was confused by the possibility to use width smaller then 1 like in [this matplot official tutorial](https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/bar_stacked.html#sphx-glr-gallery-lines-bars-and-markers-bar-stacked-py) I don't have any knowledge about how2use larger dpi, I found this two questions and I will start from here. [1](https://stackoverflow.com/questions/51937381/increase-dpi-of-matplotlib-show-in-jupyter-notebook/51937431) [2](https://stackoverflow.com/questions/53821417/increase-dpi-in-matplotlib-chart-without-changing-its-size) – Andrea Ciufo Jan 10 '20 at 17:09

0 Answers0