【问题标题】:Visualize data from dictionary of list using scatter plot使用散点图可视化列表字典中的数据
【发布时间】:2021-01-27 19:11:12
【问题描述】:

我有一本列表字典:

topic_words_dict = {0: [[-0.669712, 0.6868, 0.9821409999999999, 0.287708],[-0.925967, 0.6138399999999999, 1.247525, 0.740929]], 1: [[-0.862131, 0.890915, 1.07759, 0.295002],[-0.437658, 0.279271, 0.627497, 0.322339]], 2: [[-0.671647, 0.670583, 0.937155, 0.334581], [-0.675347, 0.466983, 0.8505440000000001, 0.5795710000000001]], 3: [[-0.8414590000000001, 0.797826, 1.124295, 0.40925300000000003], [-0.567535, 0.40820300000000004, 0.811368, 0.429982]], 4: [[-0.8560549999999999, 1.0617020000000001, 1.579302, 0.282398], [-0.576105, 0.5029239999999999, 0.9392, 0.400042]], 5: [[-0.858527, 0.924175, 1.333083, 0.336538], [-0.562329, 0.37295500000000004, 0.9964350000000001, 0.439751]] }

其中键 0 到 5 表示 6 个主题,值表示词的嵌入。根据“topic_words_dict”字典,每个主题都包含两个词的嵌入,例如:

0: [[-0.669712, 0.6868, 0.9821409999999999, 0.287708],[-0.925967, 0.6138399999999999, 1.247525, 0.740929]],

这里的主题“0”包含两个词嵌入 [-0.669712, 0.6868, 0.9821409999999999, 0.287708] 和 [-0.925967, 0.6138399999999999, 1.247525, 0.740929]
在 Python 3.x 中
如何使用散点图将其可视化,该散点图将在其主题下显示一组单词(点),其中每个主题将表示为标签。 如下:

plt.scatter(值,标签=键) plt.legend()

我没有找到一些我可以轻松理解的清晰文档。请帮忙。感谢您宝贵的时间。

【问题讨论】:

  • 也许for key in topic_words_dict: plt.scatter(topic_words_dict[key][0], topic_words_dict[key][1], label=key)
  • 能否请您给我文档链接,因为问题是在 python 3.x 中迭代密钥

标签: python python-3.x dictionary matplotlib plot


【解决方案1】:

在 Python 3.x 中,循环遍历字典时,可以使用items() 方法同时检索其键和对应的值。

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()

topic_words_dict = {0: [[-0.669712, 0.6868, 0.9821409999999999, 0.287708],[-0.925967, 0.6138399999999999, 1.247525, 0.740929]],
 1: [[-0.862131, 0.890915, 1.07759, 0.295002],[-0.437658, 0.279271, 0.627497, 0.322339]],
 2: [[-0.671647, 0.670583, 0.937155, 0.334581], [-0.675347, 0.466983, 0.8505440000000001, 0.5795710000000001]],
 3: [[-0.8414590000000001, 0.797826, 1.124295, 0.40925300000000003], [-0.567535, 0.40820300000000004, 0.811368, 0.429982]],
 4: [[-0.8560549999999999, 1.0617020000000001, 1.579302, 0.282398], [-0.576105, 0.5029239999999999, 0.9392, 0.400042]],
 5: [[-0.858527, 0.924175, 1.333083, 0.336538], [-0.562329, 0.37295500000000004, 0.9964350000000001, 0.439751]]
 }

for key, value in topic_words_dict.items():
    ax.scatter(value[0],value[1],label=key)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.legend()

【讨论】:

  • 非常感谢
  • 很高兴为您提供帮助,如果此答案有助于解决您的问题,您想检查绿色标记。这将有助于将注意力集中在未回答的问题上。
  • 我还有一个问题请{0:[[-30.669712,0.6868,0.98214099999999999999999999,1.247525,0.7409999999999999999999999 ],61383999999999999,10.247529] ,..... , [-0.925967, 0.6138399999999999, 1.247525, 0.740929] ] 如果我们在每个键下有很多列表说 200 在这种情况下如何写这一行 ax.scatter(value[0],value[1],标签=键)。我的意思是 200 个列表的值 [0]、值 [1]
  • 是 value[0] 和 value[1] 分别不是你的 x 和 y 轴。如果不是,那么您的 x 轴值是多少?如果要绘制多个列表,可以在第一个 for 循环之后添加一个内部循环。
猜你喜欢
  • 2021-09-03
  • 1970-01-01
  • 2013-11-01
  • 2019-11-30
  • 1970-01-01
  • 2018-02-08
  • 2020-06-24
  • 2021-09-10
  • 2019-08-29
相关资源
最近更新 更多