I'm trying to draw plots in Python with Scipy module. According to http://docs.scipy.org/doc/scipy/reference/special.html I wrote code with scipy.special.spherical_jn(n,x,0):
import matplotlib.pyplot as plt
import numpy as np
import scipy.special as sp
from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})
def odrazTE(a,o,d):
temp1 = sp.spherical_jn[1,a,0]
temp2 = 1
return abs(temp1/temp2)**2
t = np.arange(0.001, 2, 0.001)
plt.plot(t,odrazTE(t,t,1),label='TE1')
plt.show()
While I'm compiling the program, all I get is this error:
Traceback (most recent call last):
File "standing-sphere.py", line 33, in <module>
plt.plot(t,odrazTE(t,t,1),label='TE1')
File "standing-sphere.py", line 15, in odrazTE
temp1 = sp.spherical_jn[1,a,0]
AttributeError: 'module' object has no attribute 'spherical_jn'
There is way how to do it with regular Bessel function and relationship between Bessel and spherical Bessel function, but I don't like this solution because of derivative of sph.bess. function that I need too.
Is there any chance I have set something wrongly and it can be fixed to scipy.special.spherical_jn work?
scipy.special.spherical_jn was added in scipy version 0.18.0, which was released on July 25, 2016. My guess is you are using an older version of scipy. To check, run
import scipy
print(scipy.__version__)
Related
I have the following code:
import matplotlib.pyplot as plt
plt.cm.set_cmap("Blues")
This gives me an error:
Traceback (most recent call last):
File ".\lorenz_explorer.py", line 12, in <module>
plt.cm.set_cmap("Blues")
AttributeError: module 'matplotlib.cm' has no attribute 'set_cmap'
My matplotlib version is 3.3.1, and the function certainly exists in the documentation for 3.3.1: Link
Then am I doing something wrong or is this a bug? Do I need to import matplotlib.cm separately or something along those lines?
As the documentation link you provide shows, the name of the function is matplotlib.pyplot.set_cmap, not matplotlib.pyplot.cm.set_cmap. So you can call it with plt.set_cmap("Blues").
In other words, the function is not part of the cm library, which is somewhat counter-intuitive.
A very simple thing - I downloaded python 3.8 and installed numpy. Upon making a very basic program that uses a numpy function, I get an error. I captured all the info that I think is relevant for now:
Traceback (most recent call last):
File "C:/Python/numpytest.py", line 6, in <module>
a=sigmoid(2)
File "C:/Python/numpytest.py", line 4, in sigmoid
return 1/(1+exp(-x))
NameError: name 'exp' is not defined
I'm guessing it isn't even importing numpy but no idea why.
Use np.exp(x) to access Numpy's exp() function.
Otherwise, import Numpy as:
from numpy import *
to use exp() without any prefix.
For example,
import numpy as np
def sigmoid(x):
return 1/(1+np.exp(-x))
a=sigmoid(2)
print(a)
You need to call the np class then the exp function
Also, it is better to copy the text rather than take a picture of it.
I was able to use the ConvexHull function in previous versions of Scipy. The function, in the version I had, would not provide the Volume of the convex hull so I decided to install the new version of Scipy. But after installing Scipy 0.16.1 I noticed that the Spatial library is rearranged and now I get an error when trying to call ConvexHull function.
This code would work in previous versions of Scipy:
import numpy as np
import scipy as sp
X = np.random.randint(0,200,(30,2))
hull = sp.spatial.qhull.Delaunay(X).convex_hull
Now, for the code above, I get the error:
AttribureError: 'module' object has no attribute 'spatial'.
It seems there is nothing inside .qhull.
I have checked this in VS,Spider,Ubuntu
You have to explicitly import the spatial subpackage. This should work:
import numpy as np
import scipy as sp
import scipy.spatial
X = np.random.randint(0,200,(30,2))
hull = sp.spatial.qhull.Delaunay(X).convex_hull
I am new to sympy and still naive about python.... I wanted to solve a trigonometric equation, to find its zeroes. (Once I have syntax, then I will use a more complex function.)
I cannot find the right syntax yet. Here is what I tried at the iPython console in Spyder (Python 2.7):
from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
solve(sin(x), x)
I got this error:
Traceback (most recent call last):
File "", line 1, in
solve(sin(x), x)
NameError: name 'sin' is not defined
OK, so I need to have the correct reference to the sine function.
According to the sympy documentation, I thought this was in mpath, but this did not work:
from mpmath import *
Traceback (most recent call last):
File "<ipython-input-7-8dcdd12d9679>", line 1, in <module>
from mpmath import *
ImportError: No module named mpmath
How do I load/access mpmath or some other way to get the sine function?
This fixed it:
from sympy import sin
To access mpmath do this
from sympy.mpmath import *
I use python 2.7.8 with the Anaconda distribution and I have problems with scipy.
Let A be a sparse matrix; I want to calculate its eigenvalues but if I write:
import scipy
scipy.sparse.linalg.eigs(A)
I get the error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
What is the problem? (The version of scipy is 0.15.1)
Does this work for you?
from scipy import sparse
import scipy.sparse.linalg as sp_linalg
B = np.random.rand(10,10)
A_dense = np.dot(B.T, B)
A_sparse = sparse.lil_matrix(A_dense)
sp_linalg.eigs(A_sparse, 3)
It seems that you have to explicitly import the submodules. scipy does not load those per default.