簡體   English   中英

python - matplot lib 子圖網格:在哪里插入行/列 arguments

[英]python - matplot lib sub-plot grid: where to insert row/column arguments

我正在嘗試以 matplotlib 子圖的形式顯示跨多個數據集的 LDA 文本分析的主題提取結果。

這是我所在的位置:

我認為我的問題是我不熟悉 matplotlib。 我已經提前完成了所有的數字運算,以便我可以專注於如何 plot 數據:

top_words_master = []
top_weights_master = []
for i in range(len(tf_list)):
    tf = tf_vectorizer.fit_transform(tf_list[i])
    lda.fit(tf)
    n_top_words = 20
    tf_feature_names = tf_vectorizer.get_feature_names_out()
    top_features_ind = lda.components_[0].argsort()[: -n_top_words - 1 : -1]
    top_features = [tf_feature_names[i] for i in top_features_ind]
    weights = lda.components_[0][top_features_ind]
    top_words_master.append(top_features)
    top_weights_master.append(weights)

這給了我我的話和我的權重(x 軸值)來制作我的行/條形圖的子圖矩陣。

我嘗試通過 matplot lib 構建它:

fig, axes = plt.subplots(2, 5, figsize=(30, 15), sharex=True)
plt.subplots_adjust(hspace=0.5)
fig.suptitle("Topics in LDA Model", fontsize=18, y=0.95)
axes = axes.flatten()
for i in range(len(tf_list)):

    ax = axes[i]
    ax.barh(top_words_master[i], top_weights_master[i], height=0.7)
    ax.set_title(topic_map[f"Topic {i +1}"], fontdict={"fontsize": 30})
    ax.invert_yaxis()
    ax.tick_params(axis="both", which="major", labelsize=20)
    for j in "top right left".split():
        ax.spines[j].set_visible(False)
    fig.suptitle("Topics in LDA Model", fontsize=40)

    plt.subplots_adjust(top=0.90, bottom=0.05, wspace=0.90, hspace=0.3)
    plt.show()

但是,它只顯示了一個,第一個。 對於剩下的 6 個數據集,它剛剛打印:

<Figure size 432x288 with 0 Axes> <Figure size 432x288 with 0 Axes> <Figure size 432x288 with 0 Axes> <Figure size 432x288 with 0 Axes> <Figure size 432x288 with 0 Axes>

問題

我已經在這幾天了。 我覺得我很接近了,但是這種結果真的讓我很困惑,有人有解決方案或能夠指出我正確的方向嗎?

您應該首先創建圖形:

def top_word_comparison(axes, model, feature_names, n_top_words):
    for topic_idx, topic in enumerate(model.components_):
        top_features_ind = topic.argsort()[: -n_top_words - 1 : -1]
        top_features = [feature_names[i] for i in top_features_ind]
        weights = topic[top_features_ind]

        ax = axes[topic_idx]
        ax.barh(top_features, weights, height=0.7)
        ax.set_title(topic_map[f"Topic {topic_idx +1}"], fontdict={"fontsize": 30})
        ax.invert_yaxis()
        ax.tick_params(axis="both", which="major", labelsize=20)
        for i in "top right left".split():
            ax.spines[i].set_visible(False)

tf_list = [cm_array, xb_array]
fig, axes = plt.subplots(len(tf_list), 5, figsize=(30, 15), sharex=True)
fig.suptitle("Topics in LDA model", fontsize=40)

for i in range(enumerate(tf_list)):
    tf = tf_vectorizer.fit_transform(tf_list[i])
    n_components = 1
    lda.fit(tf)
    n_top_words = 20
    tf_feature_names = tf_vectorizer.get_feature_names_out()
    top_word_comparison(axes[i], lda, tf_feature_names, n_top_words)

plt.subplots_adjust(top=0.90, bottom=0.05, wspace=0.90, hspace=0.3)
plt.show()

據我從您的問題中了解到,您的問題是為您的子圖獲取正確的索引。

在您的情況下,您有一個數組range(len(tf_list))來索引您的數據,一些數據(例如top_words_master[i] )到 plot,以及一個包含 10 個子圖的圖形(行 = 2,列 = 5)。 例如,如果您想 plot 數據的第 7 項 (i=6),則ax的索引將是axes[1,1]

為了獲得子圖軸的正確索引,您可以使用numpy.unravel_index 而且,當然,你不應該把你的axes flatten

import matplotlib.pyplot as plt
import numpy as np


# dummy function
my_func = lambda x: np.random.random(x)
x_max = 100

# fig properties
rows = 2
cols = 5
fig, axes = plt.subplots(rows,cols,figsize=(30, 15), sharex=True)

for i in range(rows*cols):
    ax_i = np.unravel_index(i,(rows,cols))
    
    axes[ax_i[0],ax_i[1]].barh(np.arange(x_max),my_func(x_max), height=0.7)

plt.show()

結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM