I have a text file with 3 columns of data I want to plot.
from numpy import *
import pylab
from mpl_toolkits.mplot3d import Axes3D
datalist = loadtxt("datagrid.txt")
x, t, u = datalist[:, 0, 0], datalist[0, :, 0], datalist[0, 0, :]
fig = pylab.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.plot(x, t, u)
pylab.show()
I am told too many indices. How can I unpack a text file with 3 columns if I can only use 2 indices?
As I understand correctly, the file "datagrid.txt"
contains something like
1 2 3
4 5 6
7 9 0
. . .
. . .
. . .
If so, then loadtxt
loads it as two-dimensional array. Hence, you need to change the line setting x
, t
, and u
into:
x, t, u = datalist[:,0], datalist[:,1], datalist[:,2]
or even simpler and more Pythonic:
x, t, u = datalist.transpose()
or as the third option, read your file as:
x, t, u = loadtxt("datagrid.txt", unpack=True)