Why pyLDAvis graph does not display topic keywords on the bar chart?

1.5k views Asked by At

I am trying to visualise results of an LDA Model using PyLDAvis. I have managed to get the graphs to display in jupyter notebook, however, the labels of the keywords describing the topics (on the bar chart) are missing.

Below is an example of the code using dummy data.

import numpy as np
import pandas as pd
import gensim
import gensim.corpora as corpora
import matplotlib.pyplot as plt
import pyLDAvis
import pyLDAvis.gensim 

texts = [['today', 'cold', 'winter', 'lot', 'snow', 'everywhere'],
       ['winter', 'snow', 'snowman'],
       ['summer', 'day', 'sunny', 'lake'],
       ['sunny', 'garden', 'today'],
       ['winter', 'snowing', 'today'],
       ['picnic', 'garden', 'sunny', 'weekend']]

# Create Dictionary
dictionary = corpora.Dictionary(texts)

# Create Corpus
corpus = [dictionary.doc2bow(text) for text in texts]

# Train model
lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus,
                                           id2word=dictionary,
                                           num_topics=3, 
                                           random_state=100,
                                           update_every=1,
                                           chunksize=100,
                                           passes=10,
                                           alpha='auto',
                                           per_word_topics=True)

# Visualise topics
pyLDAvis.enable_notebook()
plot = pyLDAvis.gensim.prepare(lda_model, corpus, dictionary)
plot

Here is a screenshot of the plot that is displayed

Here is an example of what it should look like https://nbviewer.jupyter.org/github/bmabey/hacker_news_topic_modelling/blob/master/HN%20Topic%20Model%20Talk.ipynb

I have tried setting local parameter to True but it made no difference. pyLDAvis.enable_notebook(local = True)

I tried saving the output as an html but it produced an empty file. pyLDAvis.save_html(plot, 'lda.html')

I'm using python 3.7.1

Any suggestions?

2

There are 2 answers

2
reisen Inaba On BEST ANSWER

!pip install pyLDAvis==2.1.2

I got this problem as well and this helped. Older version of pyLDAvis does not work properly with Jupyter or Colab.

0
Yonghao Chen On

I met the same problem and I just use this:

pyLDAvis.save_html(plot, 'vis.html')

so the complete visuliazation would be saved in wd as a html file.