pylab: " NameError: global name 'log' is not defined " how to import log function in pylab?

399 views
Skip to first unread message

typetoken

unread,
Aug 28, 2012, 1:25:26 AM8/28/12
to nltk-...@googlegroups.com
I want to draw a plot with the y axis in a 'logarithmic scale'. However, it pops up " NameError: global name 'log' is not defined ". Does anyone know how to import log in this case? Thanks indeed.

>>> def zif(text):
fdist = nltk.FreqDist(text)
import pylab
word = fdist.keys()
x = []
y = []
index = []
for i in range(0,len(set(text)),1):
x.extend(word[i:i+1])
y.extend([fdist[word[i]]])
index.extend([i+1])
pylab.plot(index, log(y),'-bo')
pylab.title('zipf law')
pylab.xlabel('word rank')
pylab.ylabel('word frequency')
pylab.show()

>>> zif(text)

Traceback (most recent call last):
  File "<pyshell#141>", line 1, in <module>
    zif(text)
  File "<pyshell#140>", line 12, in zif
    pylab.plot(index, log(y),'-bo')
NameError: global name 'log' is not defined

Correa Denzil

unread,
Aug 28, 2012, 5:22:12 AM8/28/12
to nltk-...@googlegroups.com
To be quite honest, I doubt this group is an appropriate place to ask questions which are not related to NLTK. But, here's my off hand try to your solution

import numpy as np

y_np = np.array(y)
np.log(y_np)

--Regards,
Denzil




--
You received this message because you are subscribed to the Google Groups "nltk-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nltk-users/-/ePWnZjb5IRUJ.
To post to this group, send email to nltk-...@googlegroups.com.
To unsubscribe from this group, send email to nltk-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nltk-users?hl=en.

John H. Li

unread,
Aug 28, 2012, 8:47:10 AM8/28/12
to nltk-...@googlegroups.com
Thanks. I've found other solutions from the later chapter of the nltk book (cf P218):

replace pylab.plot with
pylab.semilogy() for log in y axis, or
pylab.semilogx() for log in x axis

But what if we want log in both x and y axis?
Reply all
Reply to author
Forward
0 new messages