条件によって変化するF(x)をプロットしたいがエラーが出てしまう。
python
1from scipy.special import kv 2import matplotlib.pyplot as plt 3from scipy.integrate import quad 4import numpy as np 5import math 6from math import gamma 7 8xs = np.linspace(0, 100, 10000) 9f = lambda z: kv(5/3,z) 10F = [quad(f,x,np.inf)[0]*x for x in xs] 11 12a = gamma(1/3) 13G = [(4*math.pi/np.sqrt(3)/a)*(x/2)**(1/3) for x in xs] 14H = [((math.pi/2)**(1/2))*(x**(1/2))*(math.exp(-x))for x in xs] 15 16 17def A(x): 18 if x <= 5.0*1e-3: 19 return G 20 elif 5.0*1-3 < x < 30: 21 return F 22 elif 30 <= x: 23 return H 24 25y = [A(i) for i in xs] 26 27fig = plt.figure() 28ax = fig.add_subplot(1,1,1) 29ax.grid() 30ax.plot(xs,y) 31ax.set_xlim(1e-3, 1e1) 32ax.set_ylim(1e-3, 1e0) 33ax.set_yscale('log') 34ax.set_xscale('log') 35ax.set_title('log') 36ax.set_xlabel('x') 37ax.set_ylabel('') 38plt.show()
python
1エラー 2TypeError Traceback (most recent call last) 3TypeError: float() argument must be a string or a number, not 'list' 4 5The above exception was the direct cause of the following exception: 6 7ValueError Traceback (most recent call last) 8<ipython-input-14-37ff64188e32> in <module> 9 28 ax = fig.add_subplot(1,1,1) 10 29 ax.grid() 11---> 30 ax.plot(xs,y) 12 31 ax.set_xlim(1e-3, 5*1e1) 13 32 ax.set_ylim(1e-3, 1e0) 14 15C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs) 16 1665 lines = [*self._get_lines(*args, data=data, **kwargs)] 17 1666 for line in lines: 18-> 1667 self.add_line(line) 19 1668 self.autoscale_view(scalex=scalex, scaley=scaley) 20 1669 return lines 21 22C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in add_line(self, line) 23 1900 line.set_clip_path(self.patch) 24 1901 25-> 1902 self._update_line_limits(line) 26 1903 if not line.get_label(): 27 1904 line.set_label('_line%d' % len(self.lines)) 28 29C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _update_line_limits(self, line) 30 1922 Figures out the data limit of the given line, updating self.dataLim. 31 1923 """ 32-> 1924 path = line.get_path() 33 1925 if path.vertices.size == 0: 34 1926 return 35 36C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\lines.py in get_path(self) 37 1025 """ 38 1026 if self._invalidy or self._invalidx: 39-> 1027 self.recache() 40 1028 return self._path 41 1029 42 43C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\lines.py in recache(self, always) 44 673 if always or self._invalidy: 45 674 yconv = self.convert_yunits(self._yorig) 46--> 675 y = _to_unmasked_float_array(yconv).ravel() 47 676 else: 48 677 y = self._y 49 50C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in _to_unmasked_float_array(x) 51 1388 return np.ma.asarray(x, float).filled(np.nan) 52 1389 else: 53-> 1390 return np.asarray(x, float) 54 1391 55 1392 56 57C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\_asarray.py in asarray(a, dtype, order) 58 83 59 84 """ 60---> 85 return array(a, dtype, copy=False, order=order) 61 86 62 87 63 64ValueError: setting an array element with a sequence.
python
1from scipy.special import kv 2import matplotlib.pyplot as plt 3from scipy.integrate import quad 4import numpy as np 5import math 6from math import gamma 7 8xs = np.linspace(0, 50, 10000) 9f = lambda z: kv(5/3,z) 10F = [quad(f,x,np.inf)[0]*x for x in xs] 11 12a = gamma(1/3) 13G = [(4*math.pi/np.sqrt(3)/a)*(x/2)**(1/3) for x in xs] 14H = [((math.pi/2)**(1/2))*(x**(1/2))*(math.exp(-x))for x in xs] 15 16 17def A(x): 18 if x <= 5.0*1e-3: 19 return (4*math.pi/np.sqrt(3)/a)*(x/2)**(1/3) 20 elif 5.0*1-3 < x < 30: 21 return quad(f,x,np.inf)[0]*x 22 elif 30 <= x: 23 return ((math.pi/2)**(1/2))*(x**(1/2))*(math.exp(-x)) 24 25y = [A(i) for i in xs] 26 27fig = plt.figure() 28ax = fig.add_subplot(1,1,1) 29ax.grid() 30ax.plot(xs,y) 31ax.set_xlim(1e-3, 5*1e1) 32ax.set_ylim(1e-3, 1e0) 33ax.set_yscale('log') 34ax.set_xscale('log') 35ax.set_title('log') 36ax.set_xlabel('x') 37ax.set_ylabel('') 38plt.show()
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/04 08:33