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!
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()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With