- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试以 matplotlib 子图的形式显示跨多个数据集的 LDA 文本分析的主题提取结果。
这是我现在的位置:
我认为我的问题是我对 matplotlib 不熟悉。我已经提前完成了所有的数字处理,以便我可以专注于如何绘制数据:
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>
我已经为此好几天了。我觉得我已经很接近了,但是这种结果真的让我很困惑,有人有解决方案或能够指出我正确的方向吗?
最佳答案
据我从您的问题中了解到,您的问题是为子图获取正确的索引。
就您而言,您有一个数组 range(len(tf_list))
来索引数据,一些数据(例如 top_words_master[i]
)要绘制,并且具有 10 个子图的图(行数 = 2,列数 = 5)。例如,如果您要绘制数据的第 7 个项目 (i=6),则 ax
的索引将为 axes[1,1]
。
为了获得子图轴的正确索引,您可以使用 numpy.unravel_index 。当然,您不应该压平
您的轴
。
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()
关于python - matplotlib子图网格: where to insert row/column arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70830018/
这正确地呈现了标题,因为我可以看到一个列带有“Product ID”标题的表:。我正试图重构它,这样我就可以重用它,因为它们在整个应用程序中几乎是相同的:。在这种情况下,头不会呈现...检查页面元素中
我什至不知道如何表达这一点,但在 Python 中有没有一种方法可以引用等号之前的文本,而无需实际再次编写? ** 编辑 - 我在 Jupyter 中使用 python3 我似乎用了半辈子的时间来写作
假设我有一个包含以下列的字典 dict_ = [ {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', '
假设我有一个包含以下列的字典 dict_ = [ {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', '
假设我的数据库中有一张地铁 map ,其中每条线路的每个站点都是一行。如果我想知道我的线路在哪里互连: mysql> SELECT LineA.stop_id FROM LineA, LineB WH
代码 select c1,c2,c3,c4,c5,c6 from table where c5 in ('a', 'b') 从这里开始,我想将 c5 列拆分为两列,然后根据它们对 c
我正在尝试搜索表格中的两列,即标题和描述。但我想先搜索标题,然后再搜索描述。所以匹配标题的所有行排在第一位,所有匹配描述的行排在第二位 我可以使用单个 SQL 查询来实现吗? 最佳答案 您还可以使用
下面有以下 Python 数据框。 “标志”字段是我想要用代码创建的所需列。 我想要执行以下操作: 如果“分配类型”是预测的并且“Activities_Counter”大于 10,我想创建一个名为“F
我有两列,area 和 block,其中 area 是一个 int 类型,block 是一个 varchar。 现在我正在写两个查询: select * from table where area a
使用 Slick 2,我试图生成一个带有元组 IN 子句的查询: select * from my_table where (a, b) IN ((1, 87)); 给定: val seq: Seq[
我正在尝试从数据透视表中获取一组值,其中 A 列等于值数组,例如 ID 12 的 attribute_value_id 等于 3 和 9。这可以做到吗?我已经走了这么远... ID | post_id
我找不到这样做的有效方法。我在 Python 中有以下 DataFrame,列从 A 到 Z A B C ... Z 0 2.0 8.0 1.0 ... 5.0 1
我的数据框中有以下格式的数据: >>> df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD')) >>> df
我有多个与我公司销售的产品相关的表被新产品取代,随着时间的推移,这导致了多个表的出现。 我一遍又一遍地使用以下查询,直到最终表中只剩下 2 个产品... CREATE TABLE mar15a
我有如下 2 个表:- 表A ------------------------------- | product_id | price | --------------------
我有一个名为 tbl_mainsheet7 的表,创建方式如下: pk_mainsheet client_id project_id mainsheet_id project_cat EA_
我得到了以下 HTML 结构: ... ... 我的 CSS: #main-container { width:80%; margin:20px auto;
对于我的以下要求,我无法获得解决方案。 如果 data.table(如下)在 Col1 和 Col3 中有匹配的值。替换 Col2 值(旧的 New-Val)。 Col1 Col2 Col3
我正在通过连接几个表来构建一个 View ,以通过 Entity Framework 提取数据。由于此 View 没有唯一列,EntityFramework 无法检索正确的结果集(即第一列重复)。 为
好的,我已经尝试了太久了,是时候寻求帮助了。我有一个看起来有点像这样的数据框: person fruit quantity all_fruits 0 p1 grapes 2
我是一名优秀的程序员,十分优秀!