I was able to draw the graph using the code below, but I couldn't find a way to change the range of the x and y axes after that, so please let me know.
import numpy as np
import matplotlib.pyplot asplt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
from tkinter import filedialog
root=tk.Tk()
config=plt.figure()
ax=fig.add_subplot()
def file_select():
idir=r'C:\Users'
file_path1=tk.filedialog.askopenfilename (initialdir=idir)
input_box.insert(tk.END, file_path1)
data=np.loadtxt(file_path1, comments="*")
ax.plot(data[:,0], np.log10(data[:,1]*data[:,2]), label="Si", linewidth=0.4)
canvas=FigureCanvasTkAgg(fig,master=root)
canvas.draw()
canvas.get_tk_widget().pack()
plt.title("sample")# Graph Title
plt.xlabel(r"x")# Name of the x-axis
plt.ylabel("y")# y-axis name
plt.xlim(20,70)# x-axis range
plt.ylim(0,25)# y-axis range
plt.legend(bbox_to_anchor=(0,0.85), loc='upper left', borderaxespad=1,fontsize=12)
input_box=tk.Entry(width=40)
input_box.place(x=10,y=100)
input_box.pack()
# Button 1
btn1 = tk.Button(root, text="reference", command=file_select)
btn1.pack()
# Button 2
btn2=tk.Button(root, text="End", command=lambda:root.quit())
btn2.pack()
root.withdraw()
root.update()
root.deiconify()
root.mainloop()
Thank you metroplis and Kunif.I'll think about it myself from here.
python matplotlib tkinter
I tried using matplotlib.widgets.RangeSlide instead of buttons.
import numpy as np
import matplotlib.pyplot asplt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.widgets import RangeSlider
import tkinter as tk
from tkinter import filedialog
from functools import partial
defplot_data(ax):
# select data file
idir=r'C:\Users'
file_path1=tk.filedialog.askopenfilename (initialdir=idir)
input_box.insert(tk.END, file_path1)
# import data and lot
data=np.loadtxt(file_path1, comments='*')
ax.plot(
data[:,0], np.log10 (data[:,1]*data[:,2]),
label='Si', linewidth=0.4)
canvas=FigureCanvasTkAgg(fig,master=root)
canvas.draw()
# add range slider to x-axis
slx = plt.axes ([0.2, 0.07, 0.7, 0.03])
x_slider=RangeSlider(
ax=slx, label=', valmin=20, valmax=70,
validate=(20,70), valstep=1)
# add range slider to y-axis
sly = plt.axes ([0.08, 0.2, 0.025, 0.68])
y_slider = RangeSlider(
ax=sly, label=', valmin=0, valmax=25,
validate=(0,25), valstep=1, orientation='vertical')
# callback function when moving slider
default(val):
ax.set_xlim (*(x_slider.val))
ax.set_ylim(*(y_slider.val))
canvas.draw()
x_slider.on_changed (update)
y_slider.on_changed (update)
canvas.get_tk_widget().pack()
if__name__=='__main__':
root=tk.Tk()
config,ax=plt.subplots()
plt.subplots_adjust (left=0.2, bottom=0.2)
plt.title('sample')# Graph Title
plt.xlabel('x')# Name of the x-axis
Name of the plt.ylabel('y')#y axis
plt.xlim(20,70)# x-axis range
plt.ylim(0,25)# y-axis range
plt.legend(
bbox_to_anchor=(0,0.85), loc='upper left',
borderaxespad=1, fontsize=12)
input_box=tk.Entry(width=40)
input_box.place(x=10,y=100)
input_box.pack()
# Button 1
btn1 = tk.Button(
root, text='reference', command=partial(plot_data,ax))
btn1.pack()
# Button 2
btn2 = tk.Button(
root, text='End', command=lambda:root.quit())
btn2.pack()
root.withdraw()
root.update()
root.deiconify()
root.mainloop()
580 PHP ssh2_scp_send fails to send files as intended
602 GDB gets version error when attempting to debug with the Presense SDK (IDE)
903 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
614 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.