Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting 3 columns from .dat file

I have a .dat file with 3 columns that I would like to plot. How can I plot them using matplotlib/ python? I am new to python, the .dat file was created using Fortran 90. A portion of the filename.dat file is below

0.0 0.1 0.85
1.0 0.3 0.62
2.0 0.5 0.27
3.0 0.7 0.34
4.0 0.9 0.19

My python code (not correct) that plots the data in 3D is below.

import numpy as np
import matplotlib.pyplot as plot
from mpl_toolkits.mplot3d import Axes3D

3Dplot.plot(*np.loadtxt("filename.dat",unpack=True), linewidth=2.0) #invalid syntax, why?
3Dplot.show()

What is the equivalent to the GNUplot command

splot 'filename.dat' using 1:2:3

in python? That is what I am trying to do.

However when I run the .py, I get "invalid syntax" error message which I am not sure why, but it is the line that starts with 3D. How can I plot 3d data? I am able to do this if I have a .dat file with only two columns, but when I go to 3 dimensions I do not know what to do. Thanks!

like image 295
Jeff Faraci Avatar asked Dec 12 '25 03:12

Jeff Faraci


1 Answers

You are using the Axes3D object incorrectly, that's why you see a SyntaxError.

Try this:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)

# Unpack file data.
dat_file = np.loadtxt("filename.dat", unpack=True)

# Plot data.
ax.scatter(*dat_file, linewidth=2.0)
plt.show()

enter image description here

like image 132
Gabriel Avatar answered Dec 13 '25 23:12

Gabriel