Create a 2D plot pixel grid based on a pandas series of lists
<p>Suppose we have a pandas Series of lists where each list contains some characteristics described as strings like this:</p>

<pre><code>0  ["A", "C", "G", ...]
1  ["B", "C", "H", ...]
2  ["A", "X"]
...
N  ["J", "K", ...]
</code></pre>

<p>What would be the best/easiest way to plot a 2D pixel grid where the X axis is occurrence of the characteristic and the Y axis each sample in the series 0,1,2,..., N?</p>

<p>Edited on Sept 22 16:</p>

<p>It seems I haven't mentioned explicitly that the list of characteristics isn't necessarily of the same size for all observations. The observation 1 can have 4 characteristics, observation 2 can have no one, observation 3 can have 5 and so on. So, I can't transform them into a numpy array right away without preprocessing them in some way that the missing characteristics are filled in.</p>

<p>Using pandas for a 1D histrogram seems to be straightfoward as in <a href="http://stackoverflow.com/questions/28418988/how-to-make-a-histogram-from-a-list-of-strings-in-python">this</a> answer. You could use this idea and fill an array of N by 26 and then plot in 2D with </p>

<pre><code>import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import random
import string
from collections import Counter

#Generate list of letters and dataframe
N = 20
M = 1000
letterlist = []
for i in range(N):
    letterlist.append([random.choice(string.ascii_uppercase) for i in range(M)])
df = pd.DataFrame(letterlist)

#Fill an array of size N by 26
im = np.zeros([N,26])
for n in range(N):
    #Get histogram of letters for a line as Dict
    letter_counts = Counter(df.loc[n])
    #Add to array
    for k in letter_counts.keys():
        c = ord(k.lower()) - 97
        im[n,c] = letter_counts[k]

#Plot
plt.imshow(im, interpolation='none')
plt.colorbar()
plt.axis('tight')
plt.xticks(range(26), [i for i in string.ascii_uppercase])
plt.show()
</code></pre>

python pandas matplotlib plot series