python nltk plots are not showing up - python

I am using Python 2.7.1+ in my Ubuntu 11.04 natty laptop. I installed nltk and all the dependencies to plot graph:
matplotlib==1.1.0
nltk==2.0b9
numpy==1.5.1
scipy==0.8.0
However, when I try to plot a graph (as follows), nothing is showing up (no error code as well):
user#user-laptop-ubuntu:~$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.plot(range(10), range(10))
[<matplotlib.lines.Line2D object at 0x2770fd0>]
>>> plt.title("Simple Plot")
<matplotlib.text.Text object at 0x2607610>
>>> plt.show()
>>>
or, this code:
>>> from nltk.book import *
>>> text4.dispersion_plot(["citizens", "democracy"])
Funny thing is, I have the same configuration in my desktop Ubuntu 11.04 and the plot is showing up there. I have similar libraries installed in both the computer.
I am totally puzzled now. Any suggestion will be cordially welcomed :-)
Gias

import matplot.pyplot as plt
from nltk.book import *
text4.dispersion_plot(["citizens", "democracy"])
plt.show()
I dont understand your question. But this trick works for me to plot nltk dispersion plot. Explain your question in detail.
Thank You

Related

Python and iPython submodule import differences

I recently migrated my solution using pytz to dateutil.tz. I've tested everything with iPython, and it worked nicely. However, when in production with python... ERROR!
Can anyone explain why Python and iPython deal differently with the imports, and if there is a way to make python behave the same way as iPython does?
$ python3
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
Type "help", "copyright", "credits" or "license" for more information.
>>> import dateutil
>>> dateutil.__file__
'/usr/local/lib/python3.8/site-packages/dateutil/__init__.py'
>>> dateutil.tz
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'dateutil' has no attribute 'tz'
>>>
$ ipython
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import dateutil
In [2]: dateutil.__file__
Out[2]: '/usr/local/lib/python3.8/site-packages/dateutil/__init__.py'
In [3]: dateutil.tz
Out[3]: <module 'dateutil.tz' from '/usr/local/lib/python3.8/site-packages/dateutil/tz/__init__.py'>
Because IPython wants to be too clever. For any reason it has already imported dateutil.tz and when you manually import dateutil, then dateutil.tz is available in you main namespace.
But as is is a submodule, you should consistently import it if you want to use it. So you should replace import dateutil with import dateutil.tz and your code will work fine on both environment.
$ python3
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
Type "help", "copyright", "credits" or "license" for more information.
>>> import dateutil.tz
>>> dateutil.__file__
'/usr/local/lib/python3.8/site-packages/dateutil/__init__.py'
>>> dateutil.tz
<module 'dateutil.tz' from '/usr/local/lib/python3.8/site-packages/dateutil/tz/__init__.py'>

matplotlib 'cairo.Context' error

While running on my virtualenv I wrote this code on terminal and plot via matplotlib and shows me a 'Figure 1' named window with no drawing/content and throws 'TypeError' meanwhile:
(cv) pi#raspberrypi:~ $ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> from matplotlib import pyplot as plt
>>> x=[1,2,3,4]
>>> y=[2,4,6,8]
>>> plt.plot(x,y)
[<matplotlib.lines.Line2D object at 0x6fd4ca10>]
>>> plt.show()
TypeError: Couldn't find foreign struct converter for 'cairo.Context'
TypeError: Couldn't find foreign struct converter for 'cairo.Context'
TypeError: Couldn't find foreign struct converter for 'cairo.Context'
TypeError: Couldn't find foreign struct converter for 'cairo.Context'
I'm using Stretch version of RaspBian. I had installed matplotlib with sudo pip install matplotlib and tried this, also installed some dependent packages. What can I do?
Secondly are there any other way to draw histogram of image in opencv without matplotlib library?

numpy cannot call function library directly

I'm using python 2.7 on ubuntu 16.04. As described in the code below, I can't use any function from the np.matlib, but after I import, then it can be used. Is there any way to troubleshoot the problem? Thanks in advance!
$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.matlib.repmat([1,1],1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'matlib'
>>> import numpy.matlib as npm
>>> a = npm.repmat([1,1],1,2)
>>> print a
[[1 1 1 1]]
>>>
I think this is a library clash, and if so, how do I know which clashes against which?
The Python import system does not automatically load submodules of a package when a package is imported. NumPy's __init__.py does automatically load most NumPy submodules on a plain import numpy, but numpy.matlib is not included.
Until some code somewhere in the program explicitly imports numpy.matlib, numpy.matlib will not exist, and its contents will not be accessible.
import numpy.matlib as npm
This does not introduce the name numpy.matlib into the namespace; it only introduces the name npm. Python 2.7 doc reference.
If you want the module to be available through both numpy.matlib and npm, you can just define it that way, i.e. npm = numpy.matlib.

from pylab import plot

I have installed python 3.5 on a computer running Scientific Linux 6.5. I have set up a virtual environment, and I have installed numpy and pylab using pip version 8.1.2.
My problem is this, when I try to do the simplest of tasks, i.e. import plot from pylab, I get this:
$ python
Python 3.5.1 (default, May 10 2016, 12:41:42)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pylab import plot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'plot'
>>>
Here is some other information:
>>> pylab.__version__
'0.1.0'
>>> matplotlib.__version__
'1.5.1'
>>>
I don't find any help through Google, and the only question similar to this doesn't seem relevant to me. What is going on?
After coming back over a weekend break and trying again with iPython, I got a different error message, which led to another Google search...
I found the answer here: Pylab - 'module' object has no attribute 'Figure'
My mistake was to think that pip install pylab would work as expected!
Installing pylab installs matplotlib, which apparently also installs pylab. This recursion causes the error. For me, uninstalling pylab and matplotlib and re-installing matplotlib alone fixed the error. I hope this helps!

IDLE in virtualenv does not function correctly?

I installed numpy and other packages in virutalenv vpy1. I can correct import those packages if I invoke python command in console after activated the vpy1.
However, if I use IDLE, it cannot import those packages:
>>> import numpy
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import numpy
ImportError: No module named numpy
I then found out the IDLE does not have correct virtualenv setting. If I invoke python in console, the setting will be like this:
(vpy1)xxx ~ $ python
Python 2.7.6 (default, Jun 1 2014, 03:20:25)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.real_prefix
'/home/xxx/python-2.7.6'
>>> sys.executable
'/home/xxx/virtualenvs/vpy1/bin/python'
if I use IDLE:
(vpy1)xxx ~ $ which idle
/home/xxx/virtualenvs/vpy1/bin/idle
(vpy1)xxx ~ $ idle
#in IDLE
Python 2.7.6 (default, Jun 1 2014, 03:20:25)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> import sys
>>> sys.executable
'/home/xxx/python-2.7.6/bin/python2.7'
>>> hasattr(sys,'real_prefix')
False
How can I fix this?
Finally solved this problem. In the vpy1/bin the idle is directly link to /home/xxx/python-2.7.6/bin/idle, which using a wrong shebang. Change the shebang to #!/usr/bin/env python solve this problem.

Categories

Resources