Python: Imported matplotlib but cannot use imshow - python

I'm working in Ubuntu as Virtual-Box guest on top of Windows machine (as host), and I am attempting to run my python script from the terminal. I had difficulty installing matplotlib using pip install. I managed to install it using
sudo apt-get install python-matplotlib
However, I am unable to bring up an image I have created in my code:
import numpy as np
import matplotlib.pyplot as plt
import random as random
from random import randrange
image = plt.imshow(mymatrix)
plt.show()
If I import matplotlib as:
import matplotlib as plt
I am recieving the following error on attempting to run the script:
AttributeError: 'module' object has no attribute 'imshow'
If I import matplotlib as:
import matplotlib.pyplot as plt
I recieve the following error:
raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named _tkinter, please install the python-tk
package
On attempting to install python-tk using 'pip install python-tk' this is what I'm getting:
~/ising $ pip install python-tk Collecting python-tk Could not find
a version that satisfies the requirement python-tk (from versions: )
No matching distribution found for python-tk
I'm unsure if I have in fact incorrectly installed matplotlib from the beginning. I am aware pyplot is not automatically imported with matplotlib, could the same be true for installing it from the console? Seems like I have tried everything at this stage.

The problem appears to be that you do not have a graphical backend installed. The error you are getting about Python Tk is happening because normally Tk comes with any Python distribution, so you should have at least that. You can install any Python bindings for Tk, Pyqt4, Pyqt5, Wx, GTK, (possibly others) to get a working interactive graphical backend. Check the package repository for the actual package names to install.
Keep in mind that functions like imshow are part of the (sub)package matplotlib.pyplot, not matplotlib itself. import matplotlib as plt is simply wrong if you intend to do plt.imshow(...). The correct import is either from matplotlib import pyplot as plt or import matplotlib.pyplot as plt.

According to the documentation here try this
python -mpip install -U pip
python -mpip install -U matplotlib

Related

How can I install pyplot?

I tried to install pyplot using 'pip install pyplot' in command prompt while it was installing by mistake i closed command prompt then again i am trying to install pyplot using the same command but it was not installing.Can anyone guide me how to install pyplotKindly find the error in this image
error rectification in installing pyplot
pyplot is part of a matplotlib.
In order to install pyplot you should install matplotlib
pip install matplotlib
So you can "import matplotlib.pyplot"
You can go to https://pypi.org/project/matplotlib to see and install the version you want.
Then you can import the pylot
from matplotlib import pyplot

WinError 193 %1 is not a valid Win32 application

I'm trying to import these libraries in python using this code:
import matplotlib as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
I get this error when I start debugging
Message=[WinError 193] %1 is not a valid Win32 application
Source=C:\Users\basse\source\repos\PythonApplication1\PythonApplication1\PythonApplication1.py
StackTrace:
File "C:\Users\basse\source\repos\PythonApplication1\PythonApplication1\PythonApplication1.py", line 2, in <module> (Current frame)
import matplotlib as plt
These are the commands that I used in Visual Studio's terminal to install the libaries:
pip install matplotlib
pip install pandas
pip install numpy
pip install sklearn
pip install pandas-datareader
pip install beautifulsoup4
What should I do to solve this problem? Please consider that I'm kind of a beginner so make your answer detailed. Thanks!
Edit: I always get the same error with the 4 lines of code. For example, if the first line is import pandas as pd, I'll also get the same error.

Multiple errors importing and using mglearn

I'm getting multiple errors trying to install and import the mglearn library into a Jupyter notebook. I've installed mglearn using the command line using pip install mglearn and also directly into Jupyter using !pip install mglearn. However, when I try to import mglearn I get the error ModuleNotFoundError: No module named 'mglearn'. If I try to install it again I get a Requirement already satisfied response.
I then went into the python terminal with $python3 and tried import mglearn, which was successful. I checked the version and I get 0.1.7.
I've also tried the following code within Jupyter:
import sys
!{sys.executable} -m pip install mglearn
With that code I get a zsh:1: no matches found: error.
I know it's installed and I'm importing it. I'm out of ideas for how to fix this. Any help would be appreciated.

Python statsmodels and simple exponential smoothing in Jupyter and PyCharm

I am new to python, and trying to run this example in Jupyter notebook. Whenever I run following
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.api import SimpleExpSmoothing
It gives me following error
ImportError Traceback (most recent call last)
<ipython-input-5-a15788c08ca7> in <module>()
3 import pandas as pd
4 import matplotlib.pyplot as plt
----> 5 from statsmodels.tsa.api import SimpleExpSmoothing
ImportError: cannot import name 'SimpleExpSmoothing'
Although, I have installed statsmodels (0.8.0) by
pip install statsmodels
like other packages (numpy, pandas etc.). I checked on git, api file contains this method but my api file (obtained through pip) doesn't have this method. Maybe I am not getting the git version (seems latest one) through pip? I am working in windows and I also tried on mac OSX, and result is same. I tried to do a copy/paste attempt for missing files/code in files from git (not a good way) but it doesn't help. I would appreciate your suggestions here.
EDIT
So the solution for Jupyter (thanks to #user333700) is to install master branch directly from git by
pip install git+https://github.com/statsmodels/statsmodels.git
I am extending my question for PyCharm, how can I add a git package within PyCharm? This link does not help.
For future reference another solution which worked for me is to pip install the latest version directly:
pip install statsmodels==0.9.0rc1

How can I import odeint and arange in Python?

I am trying to import odeint in python by doing:
from scipy.integrate import odeint
but there is an error note:
ImportError: No module named scipy.integrate
I have looked through some websites and there is a module called scipy.integrate, I am not sure what is going wrong with my code?
I also tried
from scipy import arange
which also shows similar error message:
ImportError: No module named scipy
I am using Python 2.7.8. Is it due to the version of Python? How can I fix it then?
If I could not even import them, I won't be able to continue writing my code.
Could somebody please give some advice?
Thanks.
This happens when scipy is not installed.
Download & Install SciPy from : Scientific Python
Ubuntu & Debian : sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
Fedora: sudo yum install numpy scipy python-matplotlib ipython python-pandas sympy python-nose
Windows : Unofficial Windows Binaries for Python Extension Packages
SciPy source

Categories

Resources