How to Create a Function Plotter with Tkinter and Matplotlib in Python: A GUI Tutorial

Posted by

Python GUI Tutorial: Create a Function Plotter with Tkinter and Matplotlib

Python GUI Tutorial: Create a Function Plotter with Tkinter and Matplotlib

In this tutorial, we will learn how to create a simple function plotter using Python’s Tkinter library for building graphical user interfaces and Matplotlib for plotting graphs. This project will allow us to enter a mathematical function and visualize its graph on the GUI.

Setting up the environment

Before we start building our function plotter, make sure you have Python installed on your system. You can download and install it from the official website. Additionally, we will need to install the following libraries using pip:


pip install tkinter matplotlib

Creating the GUI

Let’s start by importing the required libraries and setting up the main window for our application. We will also add input fields for the user to enter the function and the range of x values for the plot. Here’s the code to create the basic GUI layout:


import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

# Create the main window
root = tk.Tk()
root.title("Function Plotter")

# Add input fields
function_label = tk.Label(root, text="Enter the function:")
function_label.pack()

function_entry = tk.Entry(root)
function_entry.pack()

range_label = tk.Label(root, text="Enter the range for x values (e.g. -10,10):")
range_label.pack()

range_entry = tk.Entry(root)
range_entry.pack()

# Add a button to plot the function
plot_button = tk.Button(root, text="Plot", command=plot_function)
plot_button.pack()

# Add a canvas to display the plot
canvas = tk.Canvas(root)
canvas.pack()

root.mainloop()

Plotting the function

Now that we have created the GUI, let’s write the function to plot the user-entered function. We will use the Matplotlib library to create the graph and display it on the canvas. Here’s the code for the plot_function function:


import matplotlib.pyplot as plt
import numpy as np

def plot_function():
function = function_entry.get()
x_range = range_entry.get()

# Parse the range input
x_min, x_max = map(int, x_range.split(','))

# Generate x values
x = np.linspace(x_min, x_max, 100)

# Generate y values based on the function
y = eval(function)

# Clear the canvas
canvas.get_tk_widget().delete("all")

# Plot the function
fig = plt.Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot(111)
ax.plot(x, y)

# Display the plot on the canvas
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack()

# Run the main loop
root.mainloop()

Conclusion

Congratulations! You have successfully created a function plotter using Python’s Tkinter and Matplotlib. You can further customize the GUI and add more features to enhance the user experience. Feel free to experiment with different types of functions and explore the various options available in Matplotlib for creating beautiful and interactive plots.

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@janethelmy3919
11 months ago

Perfect 👌👌👌