Python Forum

Full Version: semantics of comma inside array brackets
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi,

I am trying to understand a piece of code, namely
import matplotlib.pyplot as plt
import numpy as np
expdat = np.genfromtxt(fname='expDat.txt', delimiter=',', 
                       dtype=np.int, skip_header=1)[:,1:]
np.set_printoptions(edgeitems=5)
N = 5
plt.figure(figsize=(14,12))
for i in range(N):
    for j in range(N):
        plt.subplot(N, N, i*N+j+1)
        plt.scatter(expdat[:,i], expdat[:,j])    
        if j==0:
            plt.ylabel('$x_{}$'.format(i+1))
        if i==N-1:
            plt.xlabel('$x_{}$'.format(j+1))
plt.suptitle('pairwise scatter plots of the first {} variables (processes)'.format(N))
plt.show()
I want to know what the notation
expdat[:,i]
does.
I tried it on my own by running
import matplotlib.pyplot as plt
N = 2
dat = [[1,2],[3,4]]
for i in range(N):
    for j in range(N):
        plt.subplot(N,N,i*N+j+1)
        plt.scatter(dat[:,i],dat[:,j])
plt.show()
However, it throws
Error:
TypeError: list indices must be integers or slices, not tuple
.
Is this a special operation on an object generated from
np.genfromtxt()
?
Thanks!
The syntax inside brackets differ for python lists and numpy arrays. You could probably use
dat = np.array([[1,2],[3,4]])
Thanks for your answer. It seems like
dat[:,i]
prints the i. index of every row, so the i. column of every row.
dat[i:,j]
seems to print the first j elements of row i. But this is kind of conflicting, since
dat[:,i]
prints the i. index of every row
dat[i:,j]
should print one element only, the one at row i column j. Vice versa,
dat[:,i]
should print the first i elements of any row. Can you tell me the general rule? I know this question is pretty basic.

Edit: found a helpful article, thanks :)