1

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?

6
  • 2
    Are you running this in a notebook, or some IDE acting like one? Because for me (assuming, without contrary indication, some plain python) the real mystery is "how come that 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 (with matplotlib.pyplot.show() Commented Aug 2 at 11:41
  • 1
    @chrslg: I've simply used Python Shell (inside PyCharm, but this shouldn't make a difference, I assume). Minimal example specified. The command text4.dispersion_plot() opened a matplotlib window without a previousmatplotlib.pyplot.show() (tested on Win11 and Linux). Adding matplotlib.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! Commented Aug 2 at 11:43
  • You don't necessarily have to. But it depends on what you are trying to do (that the reason I didn't answer: not sure if you were trying to have some blocking plots, one after the other, to have 2 of them, to update the first plot with your second fdist1, ... If you don't want to issue a matplotlib.pyplot.show() you can also pass the parameter show=True to plot methods (but then it is a blocking window: nothing else happens before you close it) Commented Aug 2 at 11:48
  • Note that if you want to replot things on your first plot after you have plotted the second one, you then need to: 1. not use show=True. 2. Store the return of the plot method in a variable: ax=fdisk1.plot... 3. Call matplotlib.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 a ax parameter, you can set the default ax (where you plot) by calling matplotlib.pyplot.sca(ax). Commented Aug 2 at 11:52
  • 1
    The book is probably expecting you to work in a notebook (jupyter and the like). Most of them display figures directly (when returned), without the need to call 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 the show part, because the notebook does it implicitly) Commented Aug 2 at 12:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.