Issue
I have a python code in which I attempt to read a csv file using askopenfilename to grab the file name and then use pandas to pull the data. During testing the code without the addition of askopenfilename it was able to plot the data however, it is now unable to display the plot at all. Any idea as to what has happened to cause this error?
import matplotlib.pyplot as plt
import numpy as np
import csv
import pandas as pd
from tkinter.filedialog import askopenfilename
file_name = askopenfilename()
df = pd.read_csv(file_name)
print(df)
scan = df.scan_area # this is where the AttributeError is flagged
X = df.x_coor
Y = df.y_coor
Z = df.thickness
previous_scan_unit = df.units[1]
previous_scan_name = df.scan_area[1]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(X, Y, Z, cmap='RdYlGn')#, edgecolors='grey', alpha=0.5)
ax.scatter(X, Y, Z, c='black')
ax.set_title('Data Collected from Scan ' + previous_scan_name)
ax.set_xlabel('Transducer Displacement (' + previous_scan_unit +')')
ax.set_ylabel('Machine Displacement (' + previous_scan_unit +')')
ax.set_zlabel('Material Thickness (' + previous_scan_unit +')')
plt.show()
As marked with a comment in the above code there is an error on scan = df.scan_area however, the print(df) above displays the data correctly seen below along with the error (DEPRECATION WARNING is due to use of an OSX version of Tk):
DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
scan area voltage gate start gate width x coor y coor voltage.1 thickness units
0 test 1 1 0.5 0.25 0.0 0.0 0.5 0.25 mm
1 test 1 1 0.5 0.25 0.5 0.0 0.6 0.25 mm
2 test 1 1 0.5 0.25 1.0 0.0 0.7 0.25 mm
3 test 1 1 0.5 0.25 1.5 0.0 0.5 0.25 mm
4 test 1 1 0.5 0.25 0.0 0.5 0.5 0.25 mm
Traceback (most recent call last):
File "/Users/mac/Desktop/Capstone/testplotter.py", line 13, in <module>
scan = df.scan_area # df is the variable name and .scan_area is the column header
File "/Users/mac/Desktop/Capstone/venv_capstone/lib/python3.8/site-packages/pandas/core/generic.py", line 5462, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'scan_area'
Solution
The column is called "scan area" but you are trying to access "scan_area". You should instead access the column with scan = df["scan area"]
. You will need to do something similar with "x coor" and "y coor".
Answered By - Dillon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.