Solving TypeError: only length-1 arrays can be converted to Python scalars while showing a plot

TypeError: only length-1 arrays can be converted to Python scalars while showing a plot is an error which occurs in Python when the function expects a single value but you pass an array instead.

In this article I am going to show you why this error is happening and how you can solve this error. I am also going to share with you other possible solutions which have worked for many developers who suffered from this error.

Exploring the TypeError: only length-1 arrays can be converted to Python scalars while showing a plot

The problem happens when we have a function that expects a single value but you pass an array instead of passing a single value.

The error you should have should be similar to the error bellow. Please make sure you do not confuse between errors.

                                                                       #
TypeError: only length-1 arrays can be converted to Python scalars
                                                                       #

The python code that causes the error is simple.

                                                                       #
import numpy as np
import matplotlib.pyplot as plt
def f(y):
    return np.int(y)
y = np.arange(1, 15.1, 0.1)
plt.plot(y, f(y))
plt.show()
                                                                       #

In the sections bellow we will explain the root of the error more and propose some possible fixes.

Solution 1 : use astype().

The First solution is to use astype(). Bellow is how you can include astype() in your code.

                                                                       #
x.astype(int)
                                                                       #

If using astype() did not solve your problem you can try the second solution.

Solution 2 : change the dataframe column to a list.

Another solution is to change the data frame column to a list.

In order to achieve that you need to use dataframe[‘column’].squeeze().

Changing the data frame column to a list like we did above should solve the problem, if that is not enough you can try the solution bellow.

Solution 3 : use np.vectorize().

If the above fails, you can use np.vectorize in order to apply a function to each element of the array.

Bellow is a demonstration of how you can use np.vectorize() to achive the same objective.

                                                                       #
import numpy as np
import matplotlib.pyplot as plt
def f(x):
    return int(x)
f2 = np.vectorize(f)
x = np.arange(1, 15.1, 0.1)
plt.plot(x, f2(x))
plt.show()
                                                                       #

It is worth to note that astype(int) is better for huge arrays compared to using np.vectorize().

Summing-up

That’s it fellow developers, this is the end of this guide, I hope you found this useful in solving your particular issue, if you have the means and want to help, please support our work on our Kofi account, you do not have to, but you can donate if you want.

Thank you, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/