Search code examples
pythonnlpjupyter-notebookdata-visualizationpyldavis

LDA visualisation in Jupyter notebook


When using the pyLDAvis package as follows, inside my jupyter notebook,

pyLDAvis.enable_notebook()

lda_tfidf = LatentDirichletAllocation(n_components=20, random_state=0)
lda_tfidf.fit(dtm_tfidf)
pyLDAvis.sklearn.prepare(lda_tf, dtm_tf, tf_vectorizer)

The resulting plot autosizes the width of my jupyter notebook, making all of the other cells overlap with the boarder - I have tried:

from IPython.core.display import display, HTML
display(HTML("<style>.container { width:95% !important; }</style>"))

with no luck, as well as

# Using gensim[![enter image description here][1]][1]
v = pyLDAvis.gensim.prepare(lda_model, doc_term_matrix, dictionary)
pyLDAvis.display(v)

spending a while searching the docs.... There dosen't seem to be anything about this is the documentation, has anyone dug into the code before and can point me in the right direction?

There does not seem to be any other posts of this, but have checked versions etc... with no luck

An image of the overlap is shown below:

:overlapimg


Solution

  • Have you tried using %matplotlib inline ? I have a similar code and the displays it's fine. Here is my example:

    %matplotlib inline
    vis = pyLDAvis.gensim.prepare(topic_model=lda_model, corpus=corpus, dictionary=dictionary_LDA)
    pyLDAvis.enable_notebook()
    pyLDAvis.display(vis)
    

    EDIT: I did had the same problem in my Jupyter Notebook (the overlap was for the output area) . I manage to have a better display by changing the style before calling pyLDAvis.display:

    from IPython.core.display import display, HTML
    display(HTML("<style>.container { max-width:100% !important; }</style>"))
    display(HTML("<style>.output_result { max-width:100% !important; }</style>"))
    display(HTML("<style>.output_area { max-width:100% !important; }</style>"))
    display(HTML("<style>.input_area { max-width:100% !important; }</style>"))
    

    Specially changing the output_area and input_area and setting the max-width (not the width)