BaRud
BaRud

Reputation: 3218

matplotlib 2d numpy array

I have created a 2d numpy array as:

for line in finp:
    tdos = []
    for _ in range(250):
        sdata = finp.readline()
        tdos.append(sdata.split())
    break

tdos = np.array(tdos)

Which results in:

[['-3.463' '0.0000E+00' '0.0000E+00' '0.0000E+00' '0.0000E+00']
 ['-3.406' '0.0000E+00' '0.0000E+00' '0.0000E+00' '0.0000E+00']
 ['-3.349' '-0.2076E-29' '-0.3384E-30' '-0.1181E-30' '-0.1926E-31']
 ..., 
 ['10.594' '0.2089E+02' '0.3886E+02' '0.9742E+03' '0.9664E+03']
 ['10.651' '0.1943E+02' '0.3915E+02' '0.9753E+03' '0.9687E+03']
 ['10.708' '0.2133E+02' '0.3670E+02' '0.9765E+03' '0.9708E+03']]

Now, I need to plot $0:$1 and $0:-$2 using matplotlib, so that the in x axis, I will have:

tdata[i][0] (i.e. -3.463, -3.406,-3.349, ..., 10.708)

,and in the yaxis, I will have:

tdata[i][1] (i.e. 0.0000E+00,0.0000E+00,-0.2076E-29,...,0.2133E+02)

How I can define xaxis and yaxis from the numpy array?

Upvotes: 0

Views: 2000

Answers (1)

armatita
armatita

Reputation: 13465

Just try the following recipe and see if it is what you want (two image plot methods followed by the same methods but with cropped image):

import matplotlib.pyplot as plt
import numpy as np

X, Y = np.meshgrid(range(100), range(100))
Z = X**2+Y**2

plt.imshow(Z,origin='lower',interpolation='nearest')
plt.show()

plt.pcolormesh(X,Y,Z)
plt.show()

plt.imshow(Z[20:40,30:70],origin='lower',interpolation='nearest')
plt.show()

plt.pcolormesh(X[20:40,30:70],Y[20:40,30:70],Z[20:40,30:70])
plt.show()

, results in:

imshow

pcolormesh

cropped imshow

cropped pcolormesh

Upvotes: 1

Related Questions