Since last month NLTK dispersion_plot seems to have y (vertical) axis in reversed order on my machine. This is likely something about my versions of software (I am on a school virtual machine).
versions: nltk 3.8.1 matplotlib 3.7.2 Python 3.9.13
code:
from nltk.draw.dispersion import dispersion_plot
words=['aa','aa','aa','bbb','cccc','aa','bbb','aa','aa','aa','cccc','cccc','cccc','cccc']
targets=['aa','bbb', 'f', 'cccc']
dispersion_plot(words, targets)
expected: aaa is present at the beginning, and cccc at the end. actual: it's backwards! also notice f should be completely absent - instead bbb is absent.
conclusion: Y axis is backwards.


I found source code for nltk.draw.dispersion and it seems there is mistake.
It calculates
word2yusingreversed(words)but later it uses
ax.set_yticks()usingwordsbut it should usereversed(words)(or it should calculate
word2ywithout usingreversed()).I added
# <--- HEREin code above to show these places.It may need to report it as a issue.
At this moment you can get
axand useset_ytickswithreversedto correct it.In your code it will be
targetsinstead ofwordsFull working code
EDIT: I seems this problem was reported few months ago and they add
reversed()in code on GitHub - and probably it will work in next versiondispersion plot not working properly · Issue #3133 · nltk/nltk
dispersion plot not working properly by Apros7 · Pull Request #3134 · nltk/nltk