Reputation: 471
I have some data in a matrix format like this:
data = [['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
['', '0', '', -1, '', -2, '', -3, '', -4, '', -5, '', -6, '', -7],
['', '', 1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -7, -8],
['', -1, -2, 1, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5],
['', '', -2, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -3, -6],
['', -2, -3, 0, -1, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -3],
['', '', -3, -1, 1, -1, 1, -2, -2, -3, -3, -4, -2, -5, -5, -4],
['', -3, -4, -1, -2, 1, 0, 1, 0, 0, -1, -1, -2, -2, -3, -3],
['', '', -4, -2, 0, 0, 2, 0, 0, -1, -1, -2, 0, -3, -3, -4],
['', -4, -5, -2, -3, 0, -1, 2, 1, 1, 0, 0, -1, 0, -1, -1],
['', '', -5, -3, -3, -1, -1, 1, 3, 0, 2, -1, -1, -1, -1, -2],
['', -5, -6, -3, -4, -1, -2, 1, 0, 3, 2, 2, 1, 1, 0, 0],
['', '', -6, -4, -4, -2, -2, 0, 0, 2, 2, 1, 1, 0, 2, -1],
['', -6, -7, -4, -5, -2, -3, 0, -1, 2, 1, 2, 1, 1, 0, 2]]
And the headers like this:
X = "AGGTTGC"
Y = "ACGGTC"
I would like to create a table that looks like this in python:
I was trying to do it with pandas dataframe and I discovered multiindexing, but I did not get the result I wanted. Also I tried pyplot, with not much success. Any suggestions?
Upvotes: 1
Views: 654
Reputation: 471
I finally managed to reproduce the image with only matplotlib.pyplot:
import numpy as np
import matplotlib.pyplot as plt
def plot_calculations(X, Y, data):
X = "-" + X
Y = "-" + Y
column_labels = list(X)
row_labels = list(Y)
plt.rcParams["figure.figsize"] = (7, 7) # resize
fig, ax = plt.subplots()
plt.tight_layout()
plt.margins(0, 0)
for i in np.arange(0, len(X), 0.5):
if (i % 1 == 0):
width = 2
else:
width = 0.5
plt.plot([i, i], [0, len(Y)], linewidth=width, color="k")
for i in np.arange(0, len(Y), 0.5):
if (i % 1 == 0):
width = 2
else:
width = 0.5
plt.plot([0, len(X)],[i, i], linewidth=width, color="k")
for i in range(0, (len(X))*2):
for j in range(0, (len(Y))*2):
if (i%2 == 1 and j%2 == 1):
fw ='bold'
fs = 12
else:
fw = 'normal'
fs = 10
plt.text(i/2+1/4, j/2+1/4, data[j][i], fontsize=fs, horizontalalignment='center',verticalalignment='center', fontweight=fw)
ax.set_xticks(np.arange(len(X))+0.5)
ax.set_yticks(np.arange(len(Y))+0.5)
ax.invert_yaxis()
ax.xaxis.tick_top()
ax.set_xticklabels(column_labels)
ax.set_yticklabels(row_labels)
plt.show()
X = "AGGTTGC"
Y = "ACGGTC"
data = [['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
['', '0', '', -1, '', -2, '', -3, '', -4, '', -5, '', -6, '', -7],
['', '', 1, -2, -2, 0, -3, -1, -4, -2, -5, -3, -6, -4, -7, -5],
['', -1, -2, 1, -3, 0, -4, -1, -5, -2, -6, -3, -7, -4, -8, -5],
['', '', -2, -3, 0, -1, -1, -1, -2, -2, -3, -3, -4, -4, -3, -5],
['', -2, 0, 0, -1, 0, -2, -1, -3, -2, -4, -3, -5, -4, -6, -3],
['', '', -3, -4, 1, -2, 1, 0, -2, 0, -3, -1, -2, -2, -5, -3],
['', -3, -1, -1, -1, 1, -2, 1, -3, 0, -4, -1, -5, -2, -4, -3],
['', '', -4, -5, 0, -3, 2, -1, 0, 1, -1, 0, 0, -1, -3, -1],
['', -4, -2, -2, 0, 0, 0, 2, -1, 1, -2, 0, -3, 0, -4, -1],
['', '', -5, -6, -3, -4, -1, -2, 3, 0, 2, 2, -1, 1, -1, 0],
['', -5, -3, -3, -1, -1, 1, 1, 0, 3, -1, 2, -1, 1, -2, 0],
['', '', -6, -7, -4, -5, -2, -3, 0, -1, 2, 1, 1, 1, 2, 0],
['', -6, -4, -4, -2, -2, 0, 0, 2, 2, 1, 2, 0, 1, -1, 2]]
plot_calculations(X, Y, data)
Here is the result:
Upvotes: 2