• Register
0 votes
254 views

I was trying to write a programm using numpy and matplotlib. I am new to python programming. The code I wrote is:

import numpy
import matplotlib.pyplot
def ThisFunction(a):
  return numpy.int(a)

a = numpy.arange(3, 13.5, 0.7)
matplotlib.pyplot.plot(a, ThisFunction(a))
matplotlib.pyplot.show()

And it gives me error:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    matplotlib.pyplot.plot(a, ThisFunction(a))
  File "main.py", line 4, in ThisFunction
    return numpy.int(a)
TypeError: only size-1 arrays can be converted to Python scalars

how can I solve this problem?

10 7 2
6,050 points

1 Answer

0 votes

In your programm you are getting this kind of error beacuse you called int(a) which is a single value to the function and the value for a you are parsing causing this error. You should write your code like:

import numpy
import matplotlib.pyplot
def ThisFunction(a):
  return numpy.int(a)

ThisFunction2 = numpy.vectorize(ThisFunction)
a = numpy.arange(3, 13.5, 0.7)
matplotlib.pyplot.plot(a, ThisFunction2(a))
matplotlib.pyplot.show()

 

11 6 4
34,930 points

Related questions

1 vote
1 answer 35 views
35 views
Problem: I am doing a online course on python. I got an assginement from there. So I was trying to write a programm using numpy and matplotlib. I am new to python programming. The code I wrote is: import numpy import matplotlib.pyplot def ThisFunction(a): return ... .int(a) TypeError: only size-1 arrays can be converted to Python scalars Do anyone know why this error is happening, please help.
asked Apr 3 LizzyM 6.1k points
0 votes
1 answer 2K views
2K views
Problem : I am getting below error: TypeError: only length-1 arrays can be converted to Python scalars How can I fix it?
asked Oct 21, 2019 peterlaw 6.9k points
0 votes
1 answer 1.7K views
1.7K views
Problem : I am trying to convert my list into an array using Python, But I got below error ValueError: only one element tensors can be converted to Python scalars.
asked Oct 22, 2019 peterlaw 6.9k points