【问题标题】:Easiest way to visualize similarity matrix可视化相似度矩阵的最简单方法
【发布时间】:2021-01-11 21:51:40
【问题描述】:

我有一个相似矩阵sim_matrix。我还有一个names 列表,每个列/行。可视化此矩阵的最简单方法是什么(即,低值,例如浅红色和高值深红色),用名称注释列和行?

我目前正在使用 xlsx_writer 执行此操作,但我确信使用 matplotlib 会更容易。

import xlsxwriter
workbook = xlsxwriter.Workbook('arrays.xlsx')
worksheet = workbook.add_worksheet()
f = lambda x, y: [f"{x} {no+1}" for no in range(y)] + [f"{x} mean"]
names = f("doc", len(urls))[:len(urls)] + f("wiki", len(wiki)) +  f("para", len(paragraphs)) + f("topic", len(topics))
# Write name
for i, name in enumerate(names):
    cell = i + 1
    worksheet.write(cell, 0, name)
    worksheet.write(0, cell, name)
# Write similarity values
for i, row in enumerate(sim_matrix):
    for j, element in enumerate(row):
        if j < i:
            # Here I would have to differentiate and color accordingly. But that's really annoying
            cell_format = workbook.add_format().set_bg_color("blue")
            worksheet.write(i + 1, j + 1, element, cell_format)
workbook.close()

【问题讨论】:

标签: python xlsxwriter


【解决方案1】:

由于您已经在使用 XlsxWriter,您可以应用 2 或 3“色标”conditional format。刻度中使用的颜色可以更改:

worksheet.conditional_format('B3:K12', {'type': '2_color_scale'})

输出

【讨论】:

  • 谢谢,这样就可以了,我会更改问题中的标签。
猜你喜欢
  • 2016-06-15
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多