You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用def main时Matplotlib与pyLDAvis图表无法显示的问题排查

Matplotlib图表无法显示的问题排查与修复

核心问题

你的代码里绘图逻辑完全没被执行——所有和绘图相关的代码都写在了model_coherence_generator函数的return语句之后。函数一旦执行到return就会直接退出,后面的代码永远不会运行,这就是图表出不来的根本原因。

另外还要注意:model_coherence_generator里用到了LdaMulticore,但原代码没看到对应的导入语句,需要补充导入。

修复后的完整代码

import pandas as pd
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import gensim
from gensim import corpora, models
from gensim.models.coherencemodel import CoherenceModel
import logging
import matplotlib.pyplot as plt
from tqdm import tqdm

def main():
    df = pd.read_csv('ClearFunc_textana.csv', parse_dates=['date'])
    df_sorted = df.sort_values(by='like_count', ascending=False)
    df_top_10000 = df_sorted.iloc[:10000]

    stop_words = set(stopwords.words('english'))
    lemmatizer = WordNetLemmatizer()

    logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
    docs = df_top_10000['processed_content'].apply(lambda x: [lemmatizer.lemmatize(word) for word in nltk.word_tokenize(x.lower()) if
                   word.isalpha() and word not in stop_words])

    dictionary = corpora.Dictionary(docs)
    corpus = [dictionary.doc2bow(doc) for doc in docs]

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

    for idx, topic in lda_model.print_topics(-1):
        print('Topic: {} \nWords: {}'.format(idx, topic))
        print('\n')

    cm = CoherenceModel(model=lda_model, corpus=corpus, coherence='c_v', texts=docs)
    coherence = cm.get_coherence()
    print('Coherence score:', coherence)
    print('\nPerplexity: ', lda_model.log_perplexity(corpus))

    def model_coherence_generator(corpus, dictionary, data, start_topic, end_topic, step, passes, workers):
        models = []
        coherence_scores = []
        for num in tqdm(range(start_topic, end_topic + 1, step)):
            ldamodel = models.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=num, passes=passes, workers=workers)
            cv_coherence_model = CoherenceModel(model=ldamodel, corpus=corpus, coherence='c_v', texts=data)
            score = cv_coherence_model.get_coherence()

            coherence_scores.append(score)
            models.append(ldamodel)
        return models, coherence_scores

    # 把调用函数和绘图代码移到main函数内、model_coherence_generator定义之外
    lda_models, coherence_scores = model_coherence_generator(corpus, dictionary, docs, start_topic=2, end_topic=40, step=2, passes=10, workers=10)

    x_ax = range(2, 41, 2)
    y_ax = coherence_scores
    plt.figure(figsize=(12, 6))
    plt.plot(x_ax, y_ax, c='r')
    plt.axhline(y=0.354, c='k', linestyle='--', linewidth=2)
    plt.rcParams['figure.facecolor'] = 'white'
    plt.title('Number of Topics vs Coherence Score')
    plt.xlabel('Number of Topics')
    plt.ylabel('Coherence Score')
    plt.savefig("figure.png")
    plt.show()

if __name__ == '__main__':
    main()

修复说明

  • 将调用model_coherence_generator函数和绘图的代码从函数内部移出,放到main函数里、该函数定义的下方,确保这部分代码能被执行到。
  • 补充了必要的导入语句,避免运行时报错。
  • 调整了LdaMulticore的调用方式,用models.LdaMulticore确保能正确引用到该类。

修复后运行代码,plt.show()会正常弹出图表窗口,同时plt.savefig()会把图表保存为本地的figure.png文件。

内容的提问来源于stack exchange,提问作者Fang Mei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 13:17:55