Matlibplot graphs in Python & Tkinter

Today we will learn how to use the python plugin matlibplot in a tkinter window, to create beautiful, stand-alone graphs. First, as always, we need our imports:

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *

Basically, we only need matlibplot, tkinter as numpy, as we are going to graph a numpy array. As we learned in the first Tkinter Tutorial, we need to start our script with opening a root, and end it with running the mainloop() function. At start we generate our numpy array and set a Figure size (figsize), meaning the size of our tkinter window.

root = Tk()
p = np.array([32.574, 33.586, 32.582, 32.368, 32.45, 32.575, 31.746, 32.593, 32.597, 32.58, 32.594, 32.887, 32.938, 32.594, 32.425, 32.607, 32.601, 32.41, 32.402, 32.417, 32.604, 32.602, 32.602, 32.422, 32.609, 32.604, 32.582, 32.594, 32.593, 32.592, 32.796, 32.594, 32.624, 31.596, 32.602, 32.704, 32.606, 32.507, 32.396, 31.909, 32.598, 32.608, 32.588, 32.605, 32.589, 32.589, 32.016, 32.128, 32.598])
fig = Figure(figsize=(10,5))
a = fig.add_subplot(111)

In line 4 we added a subplot, meaning you can add multiple plots to a figure, with an identification code (111), meaning it’s a 1×1 grid and a is the first subplot.

Next up we can then start to plot our array, found under p, and set a title for our graph.

a.plot(p, c = "blue") #c = color
a.set_title("Wordpress Test Values", fontsize = 16)

In the last step we need to setup our canvas, being a seperate frame in our root main window, we will get to that in a later part of tkinter. 🙂

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack()
canvas.draw()
root.mainloop(
2017-06-15_09h51_33
Result of plotting the test values

Complete script:

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *

root = Tk()
p = np.array([32.574, 33.586, 32.582, 32.368, 32.45, 32.575, 31.746, 32.593, 32.597, 32.58, 32.594, 32.887, 32.938, 32.594, 32.425, 32.607, 32.601, 32.41, 32.402, 32.417, 32.604, 32.602, 32.602, 32.422, 32.609, 32.604, 32.582, 32.594, 32.593, 32.592, 32.796, 32.594, 32.624, 31.596, 32.602, 32.704, 32.606, 32.507, 32.396, 31.909, 32.598, 32.608, 32.588, 32.605, 32.589, 32.589, 32.016, 32.128, 32.598])
fig = Figure(figsize=(10,5))
a = fig.add_subplot(111)

a.plot(p, c = "blue") #c = color
a.set_title("Wordpress Test Values", fontsize = 16)

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack()
canvas.draw()
root.mainloop()

 

 

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s