Python Tkinter Line Chart

How to Create a Line Chart In Python Tkinter

How to Create a Line Chart In Python Tkinter


In this Python tutorial we will create a line chart using the Tkinter library for the graphical user interface. 
We will make a GUI application that displays a line chart with labeled axes and data points. 
It uses the tkinter library to draw the chart, which includes grid lines, labels, and a title. 

What We Are Gonna Use In This Project:

- Python Programming Language.
- Tkinter for GUI.
- VS Code Editor.




Project Source Code:


import tkinter as tk

class LineChart(tk.Canvas):
def __init__(self, master=None, **kwargs):
super().__init__(master, **kwargs)
self.padding = 30
self.label_padding = 40
self.axis_label_padding = 20
self.max_vertical_labels = 5
self.max_horizontal_labels = 5

self.vertical_labels = ["0", "25", "50", "75", "100"]
self.horizontal_labels = ["Label 1", "Label 2", "Label 3", "Label 4",
        "Label 5"]

self.draw_chart()


def draw_chart(self):
# Calculate canvas dimensions and starting points
width = self.winfo_reqwidth()
height = self.winfo_reqheight()
x_start = self.padding + self.label_padding + self.axis_label_padding
y_start = height - self.padding - self.label_padding
x_end = width - self.padding
y_end = self.padding + self.label_padding

# Calculate step size for vertical and horizontal labels
y_step = (y_start - y_end) / (self.max_vertical_labels - 1)
x_step = (x_end - x_start) / (self.max_horizontal_labels - 1)

# Draw vertical labels
for i in range(self.max_vertical_labels):
y = y_start - i * y_step
self.draw_vertical_label(self.vertical_labels[i], self.padding / 2, y)

# Draw horizontal labels
for i in range(self.max_horizontal_labels):
x = x_start + i * x_step
self.draw_horizontal_label(self.horizontal_labels[i], x,
            height - (self.padding / 2))

# Sample data
data = [50, 90, 30, 80, 10]
max_value = max(data)

# Draw the rectangle representing the chart area
self.create_rectangle(x_start, y_end, x_end, y_start, fill="white",
        outline="black")

# Draw horizontal grid lines
y_grid_count = len(self.horizontal_labels)
for i in range(y_grid_count):
y = y_start - i * y_step
self.create_line(x_start, y, x_end, y, fill="black")
# Draw vertical grid lines
x_grid_count = len(self.vertical_labels)
for i in range(x_grid_count):
x = x_start + i * x_step
self.create_line(x, y_end, x, y_start, fill="black")

# Draw data points and connecting lines
for i in range(len(data)):
# Calculate the x and y coordinates for the current data point
x = x_start + i * x_step
y = y_start - (data[i] / 100) * (y_start - y_end)
# Draw a circular data point at the calculated coordinates
self.create_oval(x-10, y-10, x+10, y+10, fill="red")

# Draw a connecting line between the current data point
            # and the previous one
if i > 0:
# Calculate the x and y coordinates for the previous data point
prev_x = x_start + (i - 1) * x_step
prev_y = y_start - (data[i - 1] / 100) * (y_start - y_end)
# Draw a line connecting the previous and current data points
self.create_line(prev_x, prev_y, x, y, width=2, fill="blue")


chart_title = "Chart Title"
self.create_text(width / 2, self.padding, text=chart_title,
            font=("Arial", 20, "bold"), fill="yellow")




def draw_vertical_label(self, label, x, y):
self.create_text(x + 30, y, text=label, fill="#ecf0f1",
        font=("Arial", 10, "bold"))
def draw_horizontal_label(self, label, x, y):
self.create_text(x, y, text=label, fill="#ecf0f1", font=("Arial", 10, "bold"))



class LineChartApp(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.line_chart = LineChart(self, width=800, height=600, background="#2c3e50")
self.line_chart.pack()


if __name__ == "__main__":
root = tk.Tk()
root.title("Line Chart")
app = LineChartApp(root)
root.mainloop()



The Final Result:

Python Tkinter Line Chart











Share this

Related Posts

Latest
Previous
Next Post »