I am trying to reproduce a figure from a paper in matplotlib, shown below. Basically, each cell has a percentage in it, and the higher the percentage, the darker the background of the cell:
The code below produces something similar, but each cell is a square pixel and I would like for them to be flatter rectangles rather than squares, as in the above image. How can I achieve this with matplotlib?
import numpy as np
import matplotlib.pyplot as plt
import itertools
table = np.random.uniform(low=0.0, high=1.0, size=(10,5))
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
plt.figure()
plt.imshow(table, interpolation='nearest', cmap=plt.cm.Greys, vmin=0, vmax=1)
plt.yticks(np.arange(10), class_names)
for i,j in itertools.product(range(table.shape[0]), range(table.shape[1])):
plt.text(j, i, format(table[i,j], '.2f'),
horizontalalignment="center",
color="white" if table[i,j] > 0.5 else "black")
plt.show()
Here's what this code above produces:
I suspect that changing the aspect
and the extent
for imshow
may help me here. I don't fully understand how this works, but here's what I've tried:
plt.imshow(table, interpolation='nearest', cmap=plt.cm.Greys, vmin=0, vmax=1, aspect='equal', extent=[0,14,10,0])
I realise that I also need to add the borders between the cells, remove the tick marks, and change the values to percentages rather than decimals, and I am confident that I will be able to do this by myself, but if you want to help me out with that too then please feel free!