• Register
0 votes
307 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,060 points

Please log in or register to answer this question.

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,950 points

Related questions

1 vote
1 answer 53 views
53 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, 2020 LizzyM 6.1k points
0 votes
1 answer 3 views
3 views
Problem: f=np.loadtxt('Single Small Angle 1.txt',unpack=True,skiprows=2) g=np.loadtxt('Single Small Angle 5.txt',unpack=True,skiprows=2) x = f-g[:,:11944] t=range(len(x)) m=math.log10(abs(x)) np.polyfit(t,m) plt.plot(t,abs(x)) plt.show() I'm just not sure on how to fix my issue. It keeps saying: m=math.log10(abs(x)) TypeError: only length-1 arrays can be converted to Python scalars
asked Jan 7 Mashhoodch 5.6k points
0 votes
1 answer 2.4K views
2.4K 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 13 views
13 views
Problem: I have such Python code: import numpy as np import matplotlib.pyplot as plt def f(x): return np.int(x) x = np.arange(1, 15.1, 0.1) plt.plot(x, f(x)) plt.show() And such error: TypeError: only length-1 arrays can be converted to Python scalars How can I fix it?
asked Jan 10 Mashhoodch 5.6k points
0 votes
1 answer 23 views
23 views
Problem: I'm trying to run a code: f=np.loadtxt('Single Small Angle 1.txt',unpack=True,skiprows=2) g=np.loadtxt('Single Small Angle 5.txt',unpack=True,skiprows=2) x = f-g[:,:11944] t=range(len(x)) m=math.log10(abs(x)) np.polyfit(t,m)plt.plot(t,abs(x)) plt.show() m=math.log10(abs(x)) TypeError: only length-1 arrays can be converted to Python scalars. I'm just not sure how to fix my problem.
asked Nov 5, 2020 Han Li 710 points
0 votes
1 answer 2.2K views
2.2K 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
0 votes
1 answer 670 views
670 views
Problem : I am having below error in a title, and I don't know what's wrong. It was working when I used np.hstack instead of the np.append, but I want to make this faster, so using append. time_list the list of floats heights is the 1d np.array of ... <module> axe_x.append(time_list[np.arange(j+n,j+(time_interval-n))]) TypeError: only integer arrays with one element can be converted to an index
asked Jan 24, 2020 jwilliam 3.9k points
2 votes
2 answers 570 views
570 views
Problem: I wrote the following code in python using numpy. But it gives me some error message. The code I wrote is: import numpy as np arrayone = np.array(['Human', 'Birds', 'Groots', 'Oats']) arraytwo = np.array(['Beach', 'Toast']) arraythree ... concatenate TypeError: only integer scalar arrays can be converted to a scalar index Can anyone sort out this error and solve this. Thanks in advance.
asked May 6, 2020 samhaz 5k points