I'm trying in vain to make NLTK show a Frequency Distribution Plot using FreqDist.plot(). I've followed the steps from the first chapter of the NLTK book (scroll down to "3.1 Frequency Distributions"), but fdist.plot() doesn't show anything. If there's already a matplotlib window open, plot tries to overwrite that existing plot.
Minimal example with Python 3.12, matplotlib 3.10.5 and nltk 3.9.1 (all current):
import nltk
import matplotlib
nltk.download()
from nltk.book import *
fdist1 = FreqDist(text1)
fdist1.plot(50, cumulative=True) # <-- no matplotlib window!
Extending this example by two more commands:
text4.dispersion_plot(["citizens", "democracy"]) # <-- works, dispersion plot shown
fdist1.plot(50, cumulative=True) # will draw freqdist plot over dispersion plot
will show that the fdist.plot() in fact does something, but it will modify the dispersion plot instead of creating a new FreqDist plot (as desired).
How to get the Frequency Distribution Plot shown correctly?
text4.dispersion_plot... actually displays something. Like other plotting command, it should display nothing (if all the code or the minimal reproducible example is there), until you actually show the plot (withmatplotlib.pyplot.show()text4.dispersion_plot()opened a matplotlib window without a previousmatplotlib.pyplot.show()(tested on Win11 and Linux). Addingmatplotlib.pyplot.show()to the minimal example in fact solved the issue (didn't know I had to issue that line, too). Thank you for pointing me to this detail!fdist1, ... If you don't want to issue amatplotlib.pyplot.show()you can also pass the parametershow=Trueto plot methods (but then it is a blocking window: nothing else happens before you close it)show=True. 2. Store the return of theplotmethod in a variable:ax=fdisk1.plot...3. Callmatplotlib.pyplot.show(block=False)after the first plot. 4. Since any plot function, by default, plot on the last created plot (so dispersion plot), and since nltk wrapped matplotlib method do not accept aaxparameter, you can set the defaultax(where you plot) by callingmatplotlib.pyplot.sca(ax).show, either explicitly or not. (not saying that the book is right. Myself, I usually prefer plain python, even for interactive experimentation. Just it explains why they skip theshowpart, because the notebook does it implicitly)