0

I have a similarity matrix sim_matrix. I also have a names list which for each col/row. What's the easiest way to visualize this matrix (i.e., low values e.g. are light red and high values dark red), have the cols and rows annotated with the names?

I am currently doing this with xlsx_writer but I am sure it's easier to just use 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()
Marcel Braasch
  • 1,083
  • 1
  • 10
  • 19

1 Answers1

1

Since you are already using XlsxWriter you could apply a 2 or 3 "color scale" conditional format. The colors used in the scale can be changed:

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

Output:

enter image description here

jmcnamara
  • 38,196
  • 6
  • 90
  • 108