Python Forum
How do I open a file and the plot it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I open a file and the plot it?
#1
I want to create a GUI that allows me to read a text file into a variable by pressing a button. Then, I want to plot a histogram out of the data from the text file. The text file is just a 1D array of many values. Here is what I have so far:
 from tkinter import * from tkinter import ttk from tkinter import filedialog import matplotlib.pyplot as plt root = Tk() root.geometry('800x800') def openfile(): global peaks filename = filedialog.askopenfilename() peaks = open(filename).read() def plot(): fig2,ax2 = plt.subplots() ax2.hist(peaks,1000) button = ttk.Button(root, text="Open", command=openfile) button.grid(column=1, row=1) plot = ttk.Button(root,text='Plot',command = plot) plot.grid(column=2,row=1) root.mainloop()
From what I have at the moment, it seems like I can open a file, but I am not sure if it is being read into the global variable peaks. Then, when I press the Plot button, the code just stalls and then crashes. I need help with figuring out what I am doing wrong. Thank you.

Note: this is my first time dealing with Tkinter, so if there are better way to do this, I am open to suggestions.
Reply
#2
Try importing 'loadtext' from numpy so you can input your text as data (float) into the histogram function. You won't need to define 'peaks' anymore.

Hopefully that will do it for you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Open file, and insert space in string? Winfried 7 2,467 May-28-2025, 07:56 AM
Last Post: Winfried
  Trying to open depracated joblib file mckennamason 0 2,010 Sep-19-2024, 03:30 PM
Last Post: mckennamason
  Open/save file on Android frohr 0 2,085 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 16,026 Dec-14-2023, 08:03 AM
Last Post: shanoger
  How can i combine these two functions so i only open the file once? cubangt 4 2,635 Aug-14-2023, 05:04 PM
Last Post: snippsat
  I cannot able open a file in python ? ted 5 15,035 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 3,568 Dec-20-2022, 02:19 AM
Last Post: Skaperen
  Graphic line plot with matplotlib, text file in pytho khadija 2 3,478 Aug-15-2022, 12:00 PM
Last Post: khadija
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 2,621 May-06-2022, 01:44 PM
Last Post: Larz60+
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 12,479 Jan-07-2022, 04:55 PM
Last Post: bowlofred

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.