Unable to import matplotlib.pyplot on Python 3 (MacOS) - python-3.x

I installed Python 3 on MacOS Mojavie 10.14 using:
brew install python3
Then using the following command I installed matplotlib
pip3 install matplotlib
Importing matplotlib.pyplot is successful on Python 2:
Python2 screenshot
but fails on Python 3:
Python3 screenshot
I have tried reinstalling python3 and matplotlib but failed. Could not find a solution.

This is a bug in matplotlib 3.0.0,
https://github.com/matplotlib/matplotlib/issues/12173
https://github.com/matplotlib/matplotlib/issues/12439
which will be fixed in an upcoming 3.0.1 release.
https://github.com/matplotlib/matplotlib/pull/12213
In lib/matplotlib/font_manager.py:
Alternatively, downgrade to matplotlib 2.2.3 until 3.0.1 is released.

Related

My command line keeps saying that I don't have matplotlib installed. I've tried everything that I saw on Stack Overflow

Here is the code that I used:
import math
import matplotlib.pyplot as plt
The error message was:
no module named 'matplotlib'
I have tried:
pip install matplotlib
python3 install mat plotlib
Tried updating Anaconda Navigator to the latest version.
Tried uninstalling and reinstalling Python 3.10.5
Tried using Visual Studio Code and individual Python files (I'll try Jupyter too).
conda update --all
python - version says that I have Python version 3.6.13
After all of that, I still get the same error that there is no module named matplotlib
The answer was that I had never actually attempted to access matplotlib outside of Jupyter Notebooks. I was only meant to access it inside of Jupyter. I created a virtual environment and that didn't work. I also did conda install matplotlib and that destroyed my Python install and I had to reinstall everything.

How to treat '<attribute 'dtype' of 'numpy.generic' objects>' error?

After installing pypfopt and u-numpy, dataframe.info() command shows this error.
TypeError: Cannot interpret '<attribute 'dtype' of 'numpy.generic' objects>' as a data type
I happened to mix my versions and I encountered the problem today. I managed to fix it.
Both codes in jupyter gave me an error: TypeError: Cannot interpret '<attribute 'dtype' of 'numpy.generic' objects>' as a data type
df.info()
df.categorical_column_name.value_counts().plot.bar()
I got the error: TypeError: Cannot interpret '<attribute 'dtype' of 'numpy.generic' objects>' as a data type
This is how i fixed it
Inside jupyter:
Check numpy version:
import numpy as np
print(np.__version__)
To upgrade:
!pip3 install numpy --upgrade
Inside Command line check numpy version:
python
import numpy
print(numpy.__version__)
if versions are not the same choose whether to upgrade/downgrade:
To upgrade:
$pip install numpy --upgrade
To downgrade just specify the version
If you have python environment installed:
Go to the right folder:
Check the installed version:
$pipenv --version
To verify if you have a pip environment installed for that folder:
On your terminal Go to the folder and type:
$pipenv --version
If there is a pipenv it will show the version and if there is none it won't.
check numpy version
$python
>>> import numpy
#prints the version
>>> print(numpy__version__)
To upgrade the version:
>>>exit()
#To install the latest version don't specify the version
$pipenv install numpy
#if you want to downgrade specify the version
$pipenv install numpy=version_type
Do the same for pandas. Note that with pandas if your pandas environment is 1.2.3 on the jupyter notebook upgrade with !pip install pandas==1.2.3 or just !pip install pandas --upgrade --user.
Note that if the commands are giving you an error always include --user at the end of the command.
To create a new environment using miniconda and install updated packages follow the link [https://pandas.pydata.org/pandas-docs/stable/getting_started/install.html][1]
Run the following commands from a terminal window:
conda create -n name_of_my_env python
This will create a minimal environment with only Python installed in it. To put your self inside this environment run:
source activate name_of_my_env
On Windows the command is:
2. activate name_of_my_env
The final step required is to install pandas. This can be done with the following command:
conda install pandas
To install a specific pandas version:
conda install pandas=0.20.3
I prefer using the latest version of pandas 1.2.3
However the first method should solve your problem. Always restart your notebook by closing and reopening it.
I will stick around to see if you are winning. But this will resolve your problem. The problem is caused by the versions of numpy and pandas
[1]: https://pandas.pydata.org/pandas-docs/stable/getting_started/install.html
I fixed this type error downgrading numpy version to 1.16.5.
Try it!
Use code below in your jupyter notebook to downgrade your numpy:
!pip install numpy==1.16.5
My pandas version: 0.24.2
Here's a link to the numpy issue associated with this error: https://github.com/numpy/numpy/issues/18355. A succinct fix is given there (in https://github.com/numpy/numpy/issues/18355#issuecomment-1029684903):
pip install --upgrade numpy
pip install --upgrade pandas
downgrading to numpy==1.19.5 works
Use below command to downgrade in anaconda prompt:
python -m pip install numpy==1.19.5
The issue is because of the non-compatibility of NumPy and pandas versions. I couldn't downgrade my NumPy for some odd reasons as others suggested from Anaconda. But found this link helpful
Downgrading pandas
to 1.3 and with the existing NumPy version set at 1.20.1, helped me to overcome this issue.
Just go to the terminal provided in Jupyter Notebook by clicking "New" and then "Terminal" inside "New" and type:
pip install pandas --upgrade
this will fix the error
as it worked for me.
I fixed it by updating all packages in my Anaconda Navigator Environments
In my case, restarting the kernel fixed the error. Hope it saves someone time as I was reading pandas source code to figure out what the hell was happening :))
U might have the latest version of NumPy which may not be compatible, so downgrade your NumPy version. I have downgraded to 1.17.2

matplotlib - module 'sip' has no attribute 'setapi'

I just set up the VS Code with all the Python extensions. Python version is 3.8.3 through Anaconda. The interpreter is venv. When I run this code
import matplotlib.pyplot as plt
An error shows -
Exception has occurred: AttributeError
module 'sip' has no attribute 'setapi'
File "C:\test.py", line 145, in <module>
import matplotlib.pyplot as plt
I've tried uninstalling and reinstalling matplotlib, but to no avail.
This worked for me.
python -m pip install matplotlib==3.2
There seems to be an incompatibility issue using Matplotlib version 3.3 with IPython. For now, you can fix it by installing Matplotlib 3.2.
I had the same problem. I was using PyCharm. Installing PyQt5 worked for me.
I also had the same issue, and none of the above answers worked for me. After some trial and error, it seems that pyqt and qt were causing my issues. I know you are using venv, but if you don't mind using Anaconda, the following environment should work for you:
conda create -n matplotlib python==3.8.3 matplotlib==3.3.4 pyqt==5.9.2 qt==5.9.7
I tested this solution on python==3.8.3 and python==3.7.10 using Windows 10.
PySide2 is the official Qt (v5) for Python package which is officially supported by the Qt Company.
Install this module when using matplotlib and Jupyter/IPython
pip install pyside2
There is also PySide6 which is the package for Qt (v6)
Don't ask me why pyside2 is for qt5...
I reinstalled matplotlib to the latest version and it came out fine.
pip uninstall matplotlib
pip install matplotlib
This upgrades the matplotlib version from 3.3.4 to 3.4.2.
*I'm using python 3.7.6, windows, in a virtual environment.

Couldn't install matplotlib.pylot

So I installed the NumPy, SciPy and Matplotlib on Windows 10, with the following codes. I wanted to install matplotlib.pylot and I got an error message.
I have already tried to reinstalling the matplotlib. Any solutions?
(work) C:\DEV>python -m pip install numpy #success
...#success
(work) C:\DEV>python -m pip install scipy #success
...#success
(work) C:\DEV>python -m pip install matplotlib
...#success
(work) C:\DEV>python -m pip install matplotlib.pyplot
Collecting matplotlib.pyplot
ERROR: Could not find a version that satisfies the requirement matplotlib.pyplot (from versions: none)
ERROR: No matching distribution found for matplotlib.pyplot
Once, when I encountered this kind of an error, the problem was Python version. Python isn't backwards-compatilbe like Java, you can't just install the most recent version and hope for the best. Please always make sure the software you're trying to download is available for Python you use. If it isn't- downgrade Python.
Have a look at Wheels
git clone https://github.com/matplotlib/matplotlib
git clone https://github.com/jbmohler/matplotlib-winbuild
python matplotlib-winbuild\buildall.py

How to install pandas for python 3.5 on windows 10

i am new to python and want to learn data analysis through python 3.5, while installing pandas through cmd it is showing warnings.
pip install pandas
in cmd works fine. However you can refer to https://pandas.pydata.org/pandas-docs/stable/install.html for installation of pandas with Anaconda or Miniconda.
Install Anaconda and all your problems will be gone you can install 720+ packages related to python.
conda install pandas
conda install numpy
conda install ......

Resources