Johnny
Johnny

Reputation: 1

How to choose and plot only specific arrays in python?

Here's an example of some code I'm using:

import numpy as np
import numpy.linalg as linalg

A = np.random.random((10,10))
eigenValues,eigenVectors = linalg.eig(A)

idx = eigenValues.argsort()   
eigenValues = eigenValues[idx]
eigenVectors = eigenVectors[:,idx]

What I'm trying to do is plot only the five smallest eigenvectors in a set of many more than five eigenvectors, and then plot them. So how could one choose the first five eigenvectors and then plot them in matplotlib?

Upvotes: 0

Views: 551

Answers (2)

nivniv
nivniv

Reputation: 3722

For future readers, here's a code sample that does exactly this:

from pylab import *
N = 10
k = 5
L = 1

x = linspace(-L, L, N)
H = random((N,N))
ls, vs = eig(H)

#find and plot k lowest eigenvalues and eigenvectors
min_values_indices = argsort(ls)[0:k]
for i in min_values_indices:
    plot(x, vs[:,i])

show()

btw - I would've changed the subject of this question to something more informative (such as "numpy - find lowest k eigenvalues and eigenvectors")

Upvotes: 0

NPE
NPE

Reputation: 500297

The following will select the first five eigenvectors (assuming you've already done the sorting as in your example):

eigenVectors[:,:5]

As to how best to plot a ten-dimensional vector, I am not sure.

Upvotes: 1

Related Questions