Matplotlib error "cannot import name pyplot" on Ubuntu - python

I am trying to run this code.
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('games.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()
But I keep getting this error
Traceback (most recent call last):
File "mpl.py", line 3, in <module>
from matplotlib import pyplot as plt
File "/home/megha/matplotlib.py", line 3, in <module>
from matplotlib import pyplot as plt
ImportError: cannot import name pyplot
I googled a solution for this but I am only getting that the matplotlib version needs to be upgraded. I tried that too, and its still showing the same error.
I have also tried
import matplotlib.pyplot as plt but I have python 2.7 version, so that didnt work either.

Use import matplotlib.pyplot as plt instead.

Related

ImportError: cannot import name '_docstring' from partially initialized module 'matplotlib' (most likely due to a circular import)

I am trying to import matplotlib in .ipynb file but its not working, none of my files have the same name as _docstring yet I get this error and if I try something in .py file, it works fine.
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
I'm running this in a .ipynb file in VS Code
Output :
ImportError Traceback (most recent call last)
Cell In[3], line 2
1 import numpy as np
----> 2 import matplotlib.pyplot as plt
3 import cv2 as cv
File c:\Users\P****\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\__init__.py:113
109 from packaging.version import parse as parse_version
111 # cbook must import matplotlib only within function
112 # definitions, so it is safe to import from it here.
--> 113 from . import _api, _version, cbook, _docstring, rcsetup
114 from matplotlib.cbook import sanitize_sequence
115 from matplotlib._api import MatplotlibDeprecationWarning
ImportError: cannot import name '_docstring' from partially initialized module 'matplotlib' (most likely due to a circular import) (c:\Users\P****\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\__init__.py)
But if I try to import matplotlib in .py file like
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1,10,0.1)
y = x**2
plt.plot(x,y)
plt.show()
It runs fine, no issues.
Try deleting matplotlib folder and reinstalling it.
Full path: AppData\Roaming\Python\Python39\site-packages\matplotlib
It worked for me.

Python Error ImportError: No module named skimage.io

When I run this program in Python, it show me this error:
ImportError: No module named skimage.io.
I have already run the command pip install scikit-image, but I still get this error. Can you please help me?
This is my code:
import matplotlib.pyplot as plt
import skimage.io as io
import skimage.color as color
parrots = io.imread('C:\Python27\Example\parrots.bmp')
parrots_hsv= skcolor.convert_colorspace(parrots, 'RGB','HSV')
fig, ax= plt.subplots(nclos = 2, figsize=('8,4'))
ax[0].imshow(parrots)
ax[0].set_title('original image')
restored_image =skcolore.convert_colorspace(parrots_hsv, 'HSV','RGB')
ax[1].imshow(restored_image)
ax[1].set_title('restored image')
plt.show()`enter code here`
I reproduced this error like so:
import math.sqrt as sq
print(sq(4))
Error:
File ".\Solution.py", line 1, in <module>
import math.sqrt as sq
ModuleNotFoundError: No module named 'math.sqrt'; 'math' is not a package
repaired by:
from math import sqrt as sq
print(sq(4))
So I presume if you changed your imports to the below code it might fix it:
from matplotlib import pyplot as plt
from skimage import io as io
from skimage import color as color
In addition, you might want to read a bit here for a simple explanation about imports.

Plot from mglearn

How can I save plot from mglearn? I tried this code but it does not work.
import numpy as np
import matplotlib.pyplot as plt
import mglearn
from fpdf import FPDF
f = mglearn.plots.plot_knn_classification(n_neighbors=3)
f.savefig("n_neighbors.pdf", bbox_inches='tight')
The error was AttributeError: 'NoneType' object has no attribute 'savefig'.
Thanks
As the mglearn package uses matplotlib.pyplot's plotting mechanics you can use their saving mechanics, like so.
mglearn.plots.plot_knn_classification(n_neighbors=3)
plt.savefig("n_neighbors.pdf", bbox_inches='tight')
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
import mglearn.plots
import numpy as np
import mglearn
from fpdf import FPDF
X, y = mglearn.datasets.make_forge()
mglearn.plots.plot_knn_classification(n_neighbors=3)
plt.savefig("n_neighbors.pdf", bbox_inches='tight')
This code work.

Anaconda2 ImportError: No module named matplotlib.pylab

In Eclipse, I tried import "matplotlib", but it didn't work.
Traceback (most recent call last):
File "C:\Users\Hunter-\Desktop\A_Cognitive\neural_network\step_function.py",
line 2, in <module>
import matplotlib.pylab as plt
ImportError: No module named matplotlib.pylab
mycode is below
import numpy as np
import matplotlib.pylab as plt
def step_funtion(x):
return np.array(x>0,dtype=np.int)
x=np.arange(-5.0,5.0,0.1)
y=step_funtion(x)
plt.plot(x,y)
plt.ylim(-0.1,1.1)
plt.show
※I checked by Windows cmd "WHERE", and finally I gets result of exsist of matplotlib's file.↓
  >WHERE /r C:\ProgramData\Anaconda2 matplotlib.* _  
C:\ProgramData\Anaconda2\pkgs\anaconda-navigator-1.9.2-py27_0\Lib\site-
packages\anaconda_navigator\static\images\logos\matplotlib.png
C:\ProgramData\Anaconda2\pkgs\matplotlib-2.2.3-py27h263d877_0\Lib\site-
packages\matplotlib\mpl-data\images\matplotlib.pdf
C:\ProgramData\Anaconda2\pkgs\matplotlib-2.2.3-py27h263d877_0\Lib\site-
packages\matplotlib\mpl-data\images\matplotlib.png
C:\ProgramData\Anaconda2\pkgs\matplotlib-2.2.3-py27h263d877_0\Lib\site-
packages\matplotlib\mpl-data\images\matplotlib.ppm
C:\ProgramData\Anaconda2\pkgs\matplotlib-2.2.3-py27h263d877_0\Lib\site-
packages\matplotlib\mpl-data\images\matplotlib.svg
C:\ProgramData\Anaconda2\pkgs\spyder-3.3.1-py27_1\Lib\site-
packages\spyder\images\matplotlib.png
I think you meant:
import matplotlib.pyplot as plt

pybinding cannot import name allow_rasterization

I just install pybinding and I'm trying to run the first example that is proposed in the documentation of this library.
import pybinding as pb
import numpy as np
import matplotlib.pyplot as plt
import pybinding as pb
d = 0.2 # [nm] unit cell length
t = 1 # [eV] hopping energy
# create a simple 2D lattice with vectors a1 and a2
lattice = pb.Lattice(a1=[d, 0], a2=[0, d])
lattice.add_sublattices(
('A', [0, 0]) # add an atom called 'A' at position [0, 0]
)
lattice.add_hoppings(
# (relative_index, from_sublattice, to_sublattice, energy)
([0, 1], 'A', 'A', t),
([1, 0], 'A', 'A', t)
)
lattice.plot()
plt.show()
I have already installed what is required in the documentation (for Windows OS) and the sicrpt run pretty well until it has to do the lattice.plot() throwing the following error
Traceback (most recent call last):
File "prueba.py", line 25, in <module>
lattice.plot()
File "C:\xampp7\Python\lib\site-packages\pybinding\lattice.py", line 463, in plot
axes=axes))
File "C:\xampp7\Python\lib\site-packages\pybinding\results.py", line 598, in plot
plot_sites(self.positions, self.sublattices, **props['site'])
File "C:\xampp7\Python\lib\site-packages\pybinding\system.py", line 285, in plot_sites
from pybinding.support.collections import CircleCollection
File "C:\xampp7\Python\lib\site-packages\pybinding\support\collections.py", line 2, in <module>
from matplotlib.collections import Collection, allow_rasterization
ImportError: cannot import name 'allow_rasterization'
I have already check and matplotlib is correctly installed (I try some plots recommended in the documentation of matplotlib and worked pretty well). Also a looked for the file collections.py in the pybinding library and the mistake is in the second line
import numpy as np
from matplotlib.collections import Collection, allow_rasterization
And looking at collections.py of the matplolib and searching for 'allow_rasterization' I found the duplicated 6 time the following function
#artist.allow_rasterization
def draw(self, renderer):
I am pretty new at python so I'm don't know if I'm looking at what I should. Thanks in advance
I got the same issue. I'm using Linux and the package pybinding are installed on
/usr/lib/python3.6/site-packages/pybinding/support/
You can correct the error changing the import modules of allow_rasterization module, changing the file colletion.py
/usr/lib/python3.6/site-packages/pybinding/support/colletion.py
the firsts lines from
import numpy as np
from matplotlib.collections import Collection, allow_rasterization
to
import numpy as np
from matplotlib.collections import Collection
from matplotlib.artist import allow_rasterization
You can correct the plot issue related to previous versions on matplotlib 1.1.2, because now the pybinding packages use matplotlib 2.2.2.
I uninstalled version 2.2.2 of matplotlib and installed version 1.1.2 of marplotlib.
I am now able to do put.show()
go into the file "C:\xampp7\Python\lib\site-packages\pybinding\support\collections.py" and modify the command line as:
"from matplotlib.collections import Collection#, allow_rasterization
from matplotlib.artist import allow_rasterization"
Go into the directory:
~/miniconda3/lib/python3.7/site-packages/pybinding/support
In the file: collections.py
change line 2:
from matplotlib.collections import Collection, allow_rasterization
to
from matplotlib.collections import Collection
from matplotlib.artist import allow_rasterization

Categories

Resources