Could someone please tell me what is wrong with my code? I'm importing a csv using a file selector. I'd then like to plot the results but for some reason my code does not recognise the my dataframe.
import tkinter as tk
from tkinter.filedialog import askopenfilename
import matplotlib.pyplot as plt
import pandas as pd
def import_csv_data():
global v
csv_file_path = askopenfilename()
print(csv_file_path)
v.set(csv_file_path)
df = pd.read_csv(csv_file_path)
return df
def plotting():
fig1 = plt.figure('Flowrates')
plt.title('Pressure vs. Flow')
plt.xlabel('Pressure (bar)')
plt.ylabel('Flow rate (L/min)')
plt.axhline(y=300,color='r')
plt.plot(df.iloc[:,0],df.iloc[:,1],label='ø2,0mm')
root = tk.Tk()
tk.Label(root, text='File Path').grid(row=0, column=0)
v = tk.StringVar()
entry = tk.Entry(root, textvariable=v).grid(row=0, column=1)
tk.Button(root, text='Browse Data Set',command=import_csv_data).grid(row=1, column=0)
tk.Button(root, text='Close',command=root.destroy).grid(row=1, column=1)
tk.Button(root, text='Plot',command=plotting).grid(row=1,column=2)
root.mainloop()
my dataset is like so:
Wall pressure,flow rate (L.min-1) ›2mm,flow rate (L.min-1) ›2.1mm,flow rate (L.min-1) ›2.2mm,flow rate (L.min-1) ›2.3mm,flow rate (L.min-1) ›2.4mm,flow rate (L.min-1) ›2.5mm,flow rate (L.min-1) ›2.6mm
9,333,362,380,420,439,480,509
8.5,317,340,360,397,410,446,482
8,301,322,340,375,389,422,456
7.5,284,304,319,350,365,404,427
7,267,284,297,328,341,376,405
6.5,251,267,277,308,319,350,375
6,235,250,258,288,298,330,350
5.5,219,230,239,267,277,303,324
5,202,212,217,246,255,281,299
4.5,185,194,201,225,233,257,274
Any advice on how to get the 'plotting' function to work is appreciated.
Thanks
import_csv_datafunction returns the dataframe, but the return is not stored in any variable. So yourplottingfunction does not know the variabledf. You can just make yourdfglobal. This answer might help you as well: link