Do the x values actually matter, or can they be any sequence of increasing values?


#1

Question

When plotting a bar chart in Matplotlib, do the x values actually matter, or can they be any sequence of increasing values?

Answer

The x values passed in to the plt.bar() function determine the positioning of the bars, so they cannot be any arbitrary list of increasing values.

However, as long as the differences between each value are the same, the graph will essentially appear the same, other than the values shown on the x axis.

For example, both of these methods would plot the bars in the same shape and spacing,
plt.bar([0, 1, 2, 3, 4], data)
and
plt.bar([10, 11, 12, 13, 14], data).
because the difference between each value is 1.

Changing the difference between each x value will space the bars differently. For example, if you did
plt.bar([0, 3, 6, 9, 12], data)

you will see each bar spaced further apart.

In summary, the positioning of the bars will essentially depend on the difference between each sequential x value.