What if the number of x values doesn't match the number of y values?

Question

In Matplotlib, what if the number of x values doesn’t match the number of y values for our plot?

Answer

If the two values do not match dimensions, then this will usually end up in an error, and the graph not showing.

The dimensions of the x values must match the dimensions of the y values, meaning that they must have the same number of values passed in.

If using the Numpy library, you can obtain the shape of an Array using the .shape property.

Items = np.array([1, 2, 3, 4, 5])
Items.shape # (5, ) - which means a 1D array of 5 elements
3 Likes

if we’ve got a normal list will this work with the np library .shape? also would len() achieve the same results?

1 Like

Did you work out an answer to this?

why i cant use list comprehension instead of manually typing days = [1,2,3,4,5,6]

i could do days = [x + 1 for x in range(0,6)]

yeah, there’s lots of ways to do it, and if you had a much bigger number of observations it would make sense to use:

days = list(range(len(money_spent)))
# or more simply
days = list(range(7))