Anaconda - Spyder 3.3.6 - Python 2.7 - no module named matplotlib - python-2.7

I'm trying to use matplotlib on spyder 3.3.6 but I'm having trouble importing it in the first place, is there some way to install matplotlib beforehand? I don't have a very specific question, I guess I'm just requesting someone to help me with this. I'm pretty new to using python
import matplotlib.pyplot as plt
and it returns " ImportError: No module named matplotlib.pyplot "
when I run
pip install matplotlib
I get
pip install matplotlib
^
SyntaxError: invalid syntax

Related

Matplotlib cannot find Font family [u'sans-serif']

I installed matplotlib 2.2.3 using anaconda (with python 2.7.16). I always have this missing font problem on the higher versions of matplotlib (> 1.5.1).
/home/satadru/anaconda2/lib/python2.7/site-packages/matplotlib/font_manager.py:1331: UserWarning: findfont: Font family [u'serif'] not found. Falling back to DejaVu Sans(prop.get_family(), self.defaultFamily[fontext]))
I know there are many solutions available on the web but nothing solved my problem. I already tried the following suggestions, but none of them helped:
sudo apt-get install msttcorefonts -qq
sudo apt install font-manager
rm ~/.cache/matplotlib -fr
I just don't want to go back to an older version of matplotlib again just to avoid the problem. Please help. My code is
from matplotlib import rcParams
rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['Tahoma']
import matplotlib.pyplot as plt
.......................

python related (numpy and scipy)

when i run command import numpy as np or import scipy as sp, it gives me error like:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import numpy as np
ImportError: No module named numpy
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
import scipy as sp
ImportError: No module named scipy
(Disclaimer: there are already tons of well established way / tutorials on the internet. I'm merely posting this to hopefully help out quickly)
 What you want to achieve
To install a library (e.g. numpy, scipy) locally on a machine (e.g. laptop, server, etc.) and import that library from a Python code.
 One of the solutions: Anaconda
One of the popular / quick ways in the Python scientific community is to do this via Anaconda (disclaimer 2: I personally prefer Anaconda due to its ease of enabling me to switch / play with different Python environments). Here is the step by step instructions:
Download and install the Anaconda distribution onto the machine locally.
Create a file environment.yml and store it anywhere you like (e.g. a subdirectory within your home directory). The file looks like this gist file - tweak it to your taste (e.g. choose Python version 2.x vs 3.x, add/remove/edit dependencies, etc.)
Within the same directory where you've created the environment.yml, create a conda environment by: conda env create -f environment.yml. For this particular gist file, it will create a conda environment (called "helloworld") with the specific Python version (2.7) and the anaconda package (which includes the popular numpy and scipy libraries).
Activate the environment (i.e. "go inside that environment") by: source activate helloworld (replace "helloworld" with whatever name you specified in environment.yml).
Now you are in the "helloworld" conda environment, start up a python console: jupyter console.
Now try importing stuff within this console:
Python 2.7.13 |Anaconda 4.4.0 (x86_64)| (default, Dec 20 2016, 23:05:08)
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import numpy as np
In [2]: import scipy as sp
In [3]: np.version.version
Out[3]: '1.12.1'
In [4]: sp.version.version
Out[4]: '0.19.0'
(to quit console just do a Ctrl + D to go back to command line)
For step 5 above, also try out:
jupyter notebook
jupyter qtconsole
And play with the python commands.
When you are done with the conda environment, just "deactivate" (i.e. get out of) it by doing this in the command line: source deactivate.
Top tip: don't forget step 4 - this defines which conda environment you are in (i.e. which python version and libraries available etc.). I occasionally ommited step 4 by accident and get that error "no module named numpy", etc.)
See this Anaconda get started guide for more info.
The non Anaconda way
If you would like to avoid Anaconda all together, just simply do this in the command line:
Install the numpy and scipy libraries:
pip install numpy
pip install scipy
Start a Python interpreter:
python
Do the library import stuff within the Python interpreter:
>>> import numpy as np
>>> import scipy as sp
>>> np.version.version
'1.11.3'
>>> sp.version.version
'1.11.3'
 Additional alternatives
You could try out the Python VirtualEnv - though I've never really used it since I've started using Anaconda.

can't import package pandas, statsmodels and matplotlib on Jupyter for anaconda

I failed to import package pandas statsmodels and matplotlib in Jupyter in Anaconda 2,
import pandas
import statsmodels
import matplotlib as mpl
for example the error information for import pandas like this
--------------------------------------------------------------------------- ImportError Traceback (most recent call
last) in ()
----> 1 import pandas
/Applications/anaconda/lib/python2.7/site-packages/pandas/init.py
in ()
29 "pandas from the source directory, you may need to run "
30 "'python setup.py build_ext --inplace' to build the C "
---> 31 "extensions first.".format(module))
32
33 from datetime import datetime
ImportError: C extension: hashtable not built. If you want to import
pandas from the source directory, you may need to run 'python setup.py
build_ext --inplace' to build the C extensions first.
but I can import numpy, scipy,sklearn correctly
import scipy as sp
import numpy as np
import sklearn as sk
In addition, if I use Spyder or ipython on Anaconda, it works for all the 6 packages mentioned above. I used the Mac OS 10.10.5, Python 2.7.12 Anaconda 2 (x86_64). The question is similar to the question mentioned in the following link, but the answer seems not work for my problem (I have deleted the python 2.7 installed on the Mac by default).
Import pandas on jupyter ipython notebook fails
uninstall and reinstall pandas and try again. Also if there are useless files in your directory delete them.

Error in keras - name 'Dense' is not defined

I'm new to Deep Neural Network libraries in python. I've installed Theano & keras in my windows system by following these steps(I already had anaconda):
Install TDM GCC x64.
Run the below code from command prompt
conda update conda
conda update --all
conda install mingw libpython
pip install git+git://github.com/Theano/Theano.git
pip install git+git://github.com/fchollet/keras.git
When I'm running the following code in Ipython,
import numpy as np
import keras.models
from keras.models import Sequential
model = Sequential()
model.add(Dense(32, input_shape=(784,)))
model.add(Activation('relu'))
it is showing the following error:
NameError
Traceback (most recent call last)
----> 1 model.add(Dense(32, input_shape=(784,)))
NameError: name 'Dense' is not defined
Here is the error message screenshot.
How come sequential was imported successfully and 'Dense' was not defined?
You need from keras.layers import Activation, Dense.
I had a similar problem in tensorflow 2.0 and solved it by using
from tensorflow.keras.layers import Dense
For TensorFlow 2.6 you should do this:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

Matplotlib Basemap Installation Troubles on OS X Yosemite

I've been having very serious issues installing Basemap from mpl_toolkits. Fortunately, it looks like I'm not the only one having these issues... I was wondering if someone could help me here.
I'm currently running OS X 10.10 (Yosemite).
Long story short, I was able to install Basemap via Anaconda after much difficulties. Now whenever I try to import Basemap, I get the following error:
28 from matplotlib.lines import Line2D
29 from matplotlib.transforms import Bbox
---> 30 from mpl_toolkits.basemap import pyproj
31 from mpl_toolkits.axes_grid1 import make_axes_locatable
32 from matplotlib.image import imread
ImportError: cannot import name pyproj
...which doesn't make sense to me at all. I have pyproj installed, and I physically see it in my Anaconda pkgs folder. It's called pyproj-1.9.3-0. I also have basemap-1.0.7-np19py27_0 in my pkgs folder.
Does anyone have any idea what's going on? Maybe I stupidly forgot to do something? Sorry if it's a dumb question - I'm an amateur when it comes to these sorts of things.
I just managed to install Basemap on Yosemite with Anaconda without any problems, so maybe there's something unusual about your set-up (what issues are you refering to?)
What I did was simply
conda install Basemap
But here are some things to think about:
Did you install anything after issuing this command (on my system, conda updated my matplotlib to 1.4.3 as part of this installation. You could try conda update, but you might need to do some fixing if you changed anything "by hand".
Are you using the right virtual environment (the one from which you installed Basemap)?
import Basemap with from mpl_toolkits.basemap import Basemap rather than the import Basemap that your question implies: see e.g. this example.
type which python and check that the output is along the lines of
/Users/<username>/anaconda/bin/python
or
/Users/<username>/anaconda/envs/<virtual-env>/bin/python
and that you're not using your Mac's system Python which would be something like
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python