Fixing Python matplotlib ValueError: x and y must have same first dimension, but have shapes (…) and (…)

Python matplotlib ValueError: x and y must have same first dimension, but have shapes (…) and (…) is an error which occurs in python and matplotlib when the x and y arguments you are trying to plot do not have the same dimensions.

My goal here is to provide a detailed explanation of the error and how you can fix it, we will also check out other options that can help you get rid of this problem for good.

Exploring the Python matplotlib ValueError: x and y must have same first dimension, but have shapes (…) and (…)

This is an error which occurs in python and matplotlib when the x and y arguments you are trying to plot do not have the same dimensions.

Please check out the error message bellow and make sure you have the same message.

                                                                       #
ValueError: x and y must have same first dimension, but have shapes (...,) and (...,)
                                                                       #

I hope one of the solutions bellow will help you solve the problem and get rid of the error for good.

The Method that solved my issue : Correctly use .plot(x, y) with matplotlib

When you try to plot using Matplotlib and python you must use the same dimensions for the two arguments x and y.

The error takes place when the x and y arguments you are trying to plot have different dimensions.

I am going to show you how exactly the error can occur using a simple example and then we will solve it together.

You should always start your project with this import

                                                                       #
import matplotlib.pyplot as myplot
                                                                       #

We want to create a plot of Laptop brands and the dates they were out.

We have two arguments in plot. The first one is dates and the second is Brand.

dates has 10 values.

                                                                       #
dates = [1987, 1999, 2009, 2001, 2008, 1990, 2010, 2015, 2030, 2015]
                                                                       #

Brand has 5 values.

                                                                       #
Brand = [Hp, Dell, Mac, Lenovo, Toshiba]
                                                                       #

Now we can use plot like this in order to plot the graph.

                                                                       #
myplot.plot(dates, Brand)
show()
                                                                       #

We will get exactly the same error we are trying to get rid of.

Why ? THE NUMBER OF VALUES GIVEN IS NOT THE SAME for Brand and dates.

So, we should add 5 other values to Brand, just like this

                                                                       #
Brand = [Hp, Dell, Mac, Lenovo, Toshiba, Asus, Samsung, Razer, MSI, Acer]
                                                                       #

Now that the two argument have the same number of values the plotting should work with no errors.

I hope the fixes above fixed your problem. Thank you for reading this blog post to the end.

Summing-up : 

That is it guys, this is the end of this article/guide, I hope you found it useful in solving the error : Python matplotlib ValueError x and y must have same first dimension but have shapes (…) and (…) , you can support our work on our Kofi account.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/