Take the 2-minute tour ×

Hi I need to graph the contents of a matrix where each row represents a different feature and each column is a different time point. In other words, I want to see the change in features over time and I have stacked each feature in the form of a matrix. C is the matrix

A=C.tolist() #convert matrix to list.
for i in xrange(len(A[0])):
for j in xrange(len(A[0])):

Is this right/is there a more efficient way of doing this? Thanks!

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You can extract column i of a matrix M with M[:,i] and the number of columns in M is given by M.shape[1].

import matplotlib.pyplot as plt

T = range(M.shape[0])

for i in range(M.shape[1]):
    plt.plot(T, M[:,i])


This assumes that the rows represent equally spaced timeslices.

share|improve this answer
Thank you so much! –  newb Aug 24 '12 at 1:23
Actually, you can do it a lot easier with plt.plot(M.T) - as in the answer below. –  aaren Aug 27 '12 at 10:12
add comment

From the docs:

matplotlib.pyplot.plot(*args, **kwargs):



If x and/or y is 2-dimensional, then the corresponding columns will be plotted.

So if A has the values in columns, it is as simple as:

pylab.plot(A, 'r*')  # making all red might be confusing, '*-' might be better

If your data is in rows, then plot the transpose of it:

pylab.plot(A.T, 'r*')
share|improve this answer
add comment

Your Answer


