'numpy.float64' object has no attribute 'plot' - python

I have a very simple code but at the end i found problem which I couldn't solve or find any solution.
I can't draw plot. All I get is error AttributeError: 'numpy.float64' object has no attribute 'plot'
import pylab as p
import numpy as np
import sympy as s
import matplotlib
from random import random
X=np.arange(0,1000)
y=np.random.randint(100,size=1000)
if len(X)==len(y):
print "ok"
else:
print "not ok"
polyfit=np.polyfit(X,y,6)
poly1d=np.poly1d(polyfit)
print poly1d
i=1
my=[]
for i in X:
p=poly1d(i)
my.append(p)
print my
p.plot(X,my)
p.show()
I look after docs but I found nothing,google also can't help me.

You've overwritten the pylab module accidentally later on in your code by assigning something else to p. You can avoid this by just importing pylab and using, for example, pylab.plot.
You've also got some indentation issues, remember that indentation matters in Python.
Using matplotlib.pyplot is generally recommended as opposed to using pylab. As such I've modified the code below to use pyplot over pylab. I've also removed some unneeded parts of the code and generally tidied it up.
import matplotlib.pyplot as plt
import numpy as np
from random import random
X=np.arange(0,1000)
y=np.random.randint(100,size=1000)
if len(X)==len(y):
print("ok")
else:
print("not ok")
polyfit=np.polyfit(X,y,6)
poly1d=np.poly1d(polyfit)
my=[]
for i in X:
p=poly1d(i)
my.append(p)
plt.plot(X,my)
plt.show()

Related

'numpy.int64' object is not callable when trying to print array

I am getting the error message : 'numpy.int64' object is not callable when trying to print a numpy array. This has never happened before and the error randomly appeared and I have not been able to resolve it.
I have tried restarting my computer. Jupyter notebook printed the array once, then when I ran the code again, the error reappeared.
My code:
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
myArray=np.linspace(0,4,4)
print(myArray)

module 'numpy' has no attribute 'testing'

I want to write a program which performs a periodogram on a series of measurement values listed in the file 'flux.txt' but I get the error:
module 'numpy' has no attribute 'testing'
The error also appears if I comment the whole code. I tried to update numpy but it's still updated. May someone help me please?
from scipy import signal
import numpy as np
import matplotlib.pyplot as plt
with open('flux.txt','r') as f:
item = f.readlines
print(item)
signal.periodogram(item)
plt.show()

Codes in Ipython vs Pycharm

I am a newbie and the following question may be dumb and not well written.
I tried the following block of codes in Ipython:
%pylab qt5
x = randn(100,100)
y = mean(x,0)
import seaborn
plot(y)
And it delivered a plot. Everything was fine.
However, when I copied and pasted those same lines of codes to Pycharm and tried running, syntax error messages appeared.
For instance,
%pylab was not recognized.
Then I tried to import numpy and matplotlib one by one. But then,
randn(.,.) was not recognized.
You can use IPython/Jupyter notebooks in PyCharm by following this guide:
https://www.jetbrains.com/help/pycharm/using-ipython-jupyter-notebook-with-pycharm.html
You may modify code like the snippet below in order to run in PyCharm:
from numpy.random import randn
from numpy import mean
import seaborn
x = randn(10, 10)
y = mean(x, 0)
seaborn.plt.plot(x)
seaborn.plt.show()

scipy equivalent for MATLAB spy

I have been porting code for an isomap algorithm from MATLAB to Python. I am trying to visualize the sparsity pattern using the spy function.
MATLAB command:
spy(sparse(A));
drawnow;
Python command:
matplotlib.pyplot.spy(scipy.sparse.csr_matrix(A))
plt.show()
I am not able to reproduce the MATLAB result in Python using the above command. Using the command with only A in non-sparse format gives quite similar result to MATLAB. But it's taking quite long (A being 2000-by-2000). What would be the MATLAB equivalent of a sparse function for scipy?
Maybe it's your version of matplotlib that makes trouble, as for me scipy.sparse and matplotlib.pylab work well together.
See sample code below that produces the 'spy' plot attached.
import matplotlib.pylab as plt
import scipy.sparse as sps
A = sps.rand(10000,10000, density=0.00001)
M = sps.csr_matrix(A)
plt.spy(M)
plt.show()
# Returns here '1.3.0'
matplotlib.__version__
This gives this plot:
I just released betterspy, which arguably does a better job here. Install with
pip install betterspy
and run with
import betterspy
from scipy import sparse
A = sparse.rand(20, 20, density=0.1)
betterspy.show(A)
betterspy.write_png("out.png", A)
With smaller markers:
import matplotlib.pylab as pl
import scipy.sparse as sps
import scipy.io
import sys
A=scipy.io.mmread(sys.argv[1])
pl.spy(A,precision=0.01, markersize=1)
pl.show()

Disable the output of matplotlib pyplot

I have an array A of shape (1000, 2000). I use matplotlib.pyplot to plot the array, which means 1000 curves, using
import matplotlib.pyplot as plt
plt(A)
The figure is fine but there are a thousand lines of:
<matplotlib.lines.Line2D at 0xXXXXXXXX>
Can I disable this output?
This output is what the plt function is returning (I presume here you meant to write plt.plot(A)). To suppress this output assign the return object a name:
_ = plt.plot(A)
_ is often used to indicate a temporary object which is not going to be used later on. Note that this output you are seeing will only appear in the interpreter, and not when you run the script from outside the interpreter.
You can also suppress the output by use of ; at the end (assuming you are doing this in some sort of interactive environment)
plot(A);
plt.show()
This way there is no need to create unnecessary variables.
E.g.:
import matplotlib.pyplot as plt
plt.plot(A)
plt.show()
use a semi-colon after the plot command
eg:
plt.imshow(image,cmap);
will display the graph and stop the verbose
To ignore warnings
import warnings
warnings.filterwarnings("ignore")
This will resolve your issue.

Categories

Resources