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

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.

2 Likes

I have plotted Bar graph then Line graph. Then even after deleting the command

plt.plot

how come Line graph is still showing … I have faced this problem many times…even after commenting or deleting and even refreshing both the plots show… I don’t know why is this happening.

import codecademylib
from matplotlib import pyplot as plt

drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
sales =  [91, 76, 56, 66, 52, 27]

plt.bar(drinks,sales)
plt.show()

It’s supposed to show error right…look what is it showing… i have even deleted the plot command still showing both the Graph

Please look into it @jephos249 @mtf

What happens after you refresh?

After Refresh also this issue is still remaining. I have also tried closing the Web Browser and again after opening the exercise…it’s still there. I have even restarted my own Laptop and the issue is still there.

here’s the link - Click_me

At the time of typing, the RUN button of this exercise is not working anymore, Thanks @awsomecodecademy to point out the issue, but too bad that I can not test it.

Unfortunately, this seems to be happening a lot to me as well. Many times after getting the solution, even if I change the code and run it again, the output doesn’t change which is very frustrating. The same like you I tried refreshing, restarting the browser and the device, even changing to another device to no avail. The only thing that helps is to reset the code but that deletes the whole code written up to that moment and helps you start anew.