I am trying to plot an image in python using either PIL or Matplotlib. I have tried both
import matplotlib.pyplot as plt
and
from PIL import Image
But I get the same error:
ImportError: cannot import name '_imaging' from 'PIL'
I have updated pillow and matplotlib packages but no success
You can read the image using open cv and then plot it using matplotlib.
img_name = cv2.imread("image.jpeg")
plt.imshow(img_name)
plt.show()
This usually happens when folk use python and pip from different packages, distributions or versions. I mean you install a package with pip into one package/distribution/version and then try and use it from a different package/distribution/version of Python.
So, if you normally start Python using:
python
then run:
type python
Whereas if you normally start Python using:
python3
then run:
type python3
On my machine I get:
type python3
python3 is /Library/Frameworks/Python.framework/Versions/3.9/bin/python3
so now I know which Python I am using.
Now you need to see which pip you are using. So, if you normally start pip with:
pip
then run:
type pip
Whereas if you normally start pip with:
pip3
then run:
type pip3
On my machine I get:
type pip3
pip3 is /Library/Frameworks/Python.framework/Versions/3.9/bin/pip3
So now you can see that my pip3 is installing packages for python3 exactly where it can find them.
I got this message on Kaggle notebook
'Failed to import pydot. You must `pip install pydot` and install graphviz (https://graphviz.gitlab.io/download/), ', 'for `pydotprint` to work.'
Rendering NN structural graphs doesn't work. What is the problem? Is it a problem on Kaggle side? Code I was running was very simple.
The first thing to do is to turn on internet support on your kaggle notebook and install the following packages via pip packet manager:
!pip install pydot
!pip install pydotplus
!pip install graphviz
I installed the OSMNX package from GitHub with pip using
pip install git+git://github.com/gboeing/osmnx.git
and I confirmed OSMNX was installed as it showed up on pip list.
However, when trying to import osmnx I receive an error that NumPy cannot be found. I definitely have NumPy installed, and I confirmed that NumPy shows up in pip list, so I'm not sure why OSMNX can't find NumPy. Any ideas on how to make OSMNX recognize NumPy?
Here's the full error
Alpaca backtrader plot issue: I ran into this import issue and found this article, so I applied the code, but same issue not resolved. any one can help please?
My installed matplotlib version is 3.3.1
backtrader 1.9.76.123
python 3.8.5
the entire code posted below:
from matplotlib.dates
import (HOURS_PER_DAY, MIN_PER_HOUR, SEC_PER_MIN,MONTHS_PER_YEAR,
DAYS_PER_WEEK,SEC_PER_HOUR, SEC_PER_DAY,num2date, rrulewrapper,
YearLocator,MicrosecondLocator)
import alpaca_backtrader_api
import backtrader as bt
from datetime import datetime
#import matplotlib
ALPACA_API_KEY = "XXXXX"
ALPACA_SECRET_KEY = "XXXX"
ALPACA_PAPER = True
class SmaCross(bt.SignalStrategy):
def init(self):
sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)
crossover = bt.ind.CrossOver(sma1, sma2)
self.signal_add(bt.SIGNAL_LONG, crossover)
cerebro = bt.Cerebro()
cerebro.addstrategy(SmaCross)
store = alpaca_backtrader_api.AlpacaStore( key_id=ALPACA_API_KEY,secret_key=ALPACA_SECRET_KEY,paper=ALPACA_PAPER)
if not ALPACA_PAPER:
broker = store.getbroker() # or just alpaca_backtrader_api.AlpacaBroker()
cerebro.setbroker(broker)
DataFactory = store.getdata # or use alpaca_backtrader_api.AlpacaData
data0 = DataFactory(dataname='AAPL', historical=True, fromdate=datetime(2015, 1, 1), timeframe=bt.TimeFrame.Days)
cerebro.adddata(data0)
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.plot()
Downgrade to matplotlib 3.2.2 until the bug in backtrader is fixed.
Here is the fix pull request: https://github.com/mementum/backtrader/pull/418.
pip uninstall matplotlib # or conda
pip install matplotlib==3.2.2
I suffered the same problem like you did, your link provided has the perfect solution. just get rid of warnings from locator.py
https://community.backtrader.com/topic/981/importerror-cannot-import-name-min_per_hour-when-trying-to-plot/8
I couldn't install matplotlib==3.2.2 nor the patch without uninstalling backtrader first.
So, this worked for me in the end:
Uninstall backtrader:
pip uninstall backtrader
Install the patch provided in the above solution:
pip install git+https://github.com/mementum/backtrader.git#0fa63ef4a35dc53cc7320813f8b15480c8f85517#egg=backtrader
If necessary, install matplotlib again:
pip install matplotlib
As pointed out above, the issue is addressed in this pull request and the patch is the latest commit to master, but there hasn't been a release since 2019-05.
You can install the patched version like so:
pip install git+https://github.com/mementum/backtrader.git#0fa63ef4a35dc53cc7320813f8b15480c8f85517#egg=backtrader
You could alternatively specify the required commit in requirements.txt like so:
-e git+https://github.com/mementum/backtrader.git#0fa63ef4a35dc53cc7320813f8b15480c8f85517#egg=backtrader
…then pip install -r requirements.txt
After installing with either method, you can confirm the versions installed with pip freeze:
...
backtrader==1.9.76.123
...
How to install from git
Mac Big Sur
for me it only worked if:
Downgrade python3.9 to python 3.8
then I downgraded matplotlib==3.2.2
For both python 3.8.x and 3.9.x, I solved the problem by using specific version of matplotlib==3.2.2
pip install matplotlib==3.2.2
By default, I used matplotlib==3.4.x version and the problem occured.
All of the above answers are fine. The problem is not with Matplotlib though. The Backtrader library hasn't kept up with the Matplotlib updates. You can do the off-label Backtrader update suggested by Joel Brigate above...or you can make a simple mod to locator.py file (backtrader.plot):
Just change:
from matplotlib.dates import (HOURS_PER_DAY, MIN_PER_HOUR,
SEC_PER_MIN, MONTHS_PER_YEAR, DAYS_PER_WEEK, SEC_PER_HOUR,
SEC_PER_DAY, num2date, rrulewrapper, YearLocator,
MicrosecondLocator, warnings)
to:
from matplotlib import warnings
from matplotlib.dates import (HOURS_PER_DAY, MIN_PER_HOUR, SEC_PER_MIN,
MONTHS_PER_YEAR, DAYS_PER_WEEK, SEC_PER_HOUR,
SEC_PER_DAY, num2date, rrulewrapper,
YearLocator, MicrosecondLocator)
You'll note that the warnings import now comes directly out of matplotlib rather than matplotlib.dates. This is the offending issue within locator.py.
Here is my solution:
python -m pip uninstall matplotlib
python -m pip uninstall backtrader
python -m pip install backtrader
python -m pip install matplotlib==3.2.2
Enjoy!
I could not install matplotlib==3.2.2 with python 3.9 .
Here is how did I fix this issue:
$ pip uninstall backtrader
$ pip install git+https://github.com/mementum/backtrader.git#0fa63ef4a35dc53cc7320813f8b15480c8f85517#egg=backtrader
Reference:
Github: Fix ImportError from matplotlib.dates
#418
Mac Big Sur. I did the same: python 3.8.5, uninstall matplotlib, install matplotlib==3.2.2
I'm new at this so I first tried the easy way, through anaconda.org, but could not find version 3.2.2. Then tried it from the Jupyter notebook with conda install... didn't work. I finally did it straight through terminal, which worked fine.
#laffuste solution of downgrading to version 3.2.2 of matplotlib solved the issue for me. PR to fix the issue is still open, you can also follow this forum for more info on the problem:
I want to check if some packages are installed in the Colab. What is specific folder for storing the installed packages (e.g., keras)?
You can use the pip tool to list installed Python packages and their locations on the system:
!pip list -v | grep [Kk]eras
# Keras 2.2.5 /usr/local/lib/python3.6/dist-packages pip
# Keras-Applications 1.0.8 /usr/local/lib/python3.6/dist-packages pip
# Keras-Preprocessing 1.1.0 /usr/local/lib/python3.6/dist-packages pip
# keras-vis 0.4.1 /usr/local/lib/python3.6/dist-packages pip
Note that in Colab and other Jupyter notebook frontends, the ! character is used to execute a shell command.
If you are not able to find the package, you might have probably downloaded the package instead of installing it.
Incorrect command:
!pip download transformers
Correct command:
!pip install transformers
Next, in order to find the package run the following command:
!pip show transformers