I want to create a subplot inside the first subplot named "logo".
This time I generated my subplots with mosaic (ax_dict = fig.subplot_mosaic(layout))
How to generate a subplot inside the first subplot?
import numpy as np
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import ttk
from tkinter import *
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
root = tk.Tk()
root.title("Transactions")
root.geometry("1920x1080")
tabControl = ttk.Notebook(root)
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tabControl.add(tab1, text ='Home')
tabControl.add(tab2, text ='Scatter Plot')
tabControl.add(tab3, text ='Histogram')
#LABEL-FRAME
ontop = tk.Frame(root, background="#ccc5b9",height=33)
ontop.pack(fill=BOTH)
#BUTTON-FRAME
top = tk.Frame(root, background="#e9ecef")
top.pack(fill=X)
tabControl.pack(expand = FALSE, fill ="both")
#TABLE-FRAME
bottom = tk.Frame(root, background="#adb5bd")
bottom.pack(in_=tab1,fill=X)
s = ttk.Style()
s.theme_use('default')
s.configure('TNotebook.Tab', background="green3")
s.map("TNotebook", background= [("selected", "green3")])
layout = [["logo", "logo","logo"],["logo", "logo","logo"],["bar", "bar","bar"],["bar", "bar","bar"],["alpha","beta","edge"]]
fig = plt.figure(figsize = (5,9))
ax_dict = fig.subplot_mosaic(layout)
ax_dict["logo"].annotate(
xy = (3.3, 95),
text = "0-1000 €",
size = 10
)
ax_dict["bar"].annotate(
xy = (1.5,.5),
text = "bar",
ha = "left",
va = "center",
size = 10
)
ax_dict["edge"].annotate(
xy = (.5,.5),
text = "edge",
ha = "left",
va = "center",
size = 10
)
ax_dict["alpha"].annotate(
xy = (.5,.5),
text = "alpha",
ha = "center",
va = "center",
size = 10
)
ax_dict["beta"].annotate(
xy = (.5,.5),
text = "beta",
ha = "center",
va = "center",
size = 10
)
ax_dict["logo"].set_xlim([-10,1010])
ax_dict["bar"].set_xlim([900,100100])
ax_dict["alpha"].set_xlim([99000,1001000])#100T-1M
ax_dict["beta"].set_xlim([990000,10010000])#1M-10M
ax_dict["edge"].set_xlim([9900000,100100000])#10M-100M
ax_dict["logo"].set_xticks([-10,50,100,150,200,250,300,350,400,450,500,550,600,660,700,750,800,850,900,950,1010])
ax_dict["bar"].set_xticks([1000,5000, 10000,15000,20000,25000,30000,35000,40000,45000,50000,55000,60000,65000,70000,75000,80000,85000,90000,95000,100000])
ax_dict["alpha"].set_xticks([100000,200000,300000,400000,500000,600000,700000,800000,900000,1000000])
ax_dict["beta"].set_xticks([1000000,2000000,3000000,4000000,5000000,6000000,7000000,8000000,9000000,10000000])
ax_dict["edge"].set_xticks([10000000,20000000,30000000,40000000,50000000,60000000,70000000,80000000,90000000,100000000])
ax_dict["logo"].set_xticklabels(["0-10","50","100","150", "200","250","300","350","400","450","500","550","600","650","700","750","800","850","900","950","1000"],fontsize=6)
ax_dict["bar"].set_xticklabels(["1k","5k", "10k","15k","20k","25k","30k","35k","40k","45k","50k","55k","60k","65k","70k","75k","80k","85k","90k","95k","100k"],fontsize=8)
ax_dict["alpha"].set_xticklabels(["100k","200k","300k","400k","500k","600k","700k","800k","900k", "1M"],fontsize=8)
ax_dict["beta"].set_xticklabels(["1M","2M","3M","4M","5M","6M","7M","8M","9M","10M"],fontsize=8)
ax_dict["edge"].set_xticklabels(["10M","20M","30M","40M","50M","60M","70M","80M","90M","100M"],fontsize=8)
ax_dict["logo"].grid(visible = True, ls = ":", color = "gray")
ax_dict["edge"].grid(visible = True, ls = ":", color = "red")
ax_dict["bar"].grid(visible = True, ls = ":", color = "gray")
ax_dict["alpha"].grid(visible = True, ls = ":", color = "blue")
ax_dict["beta"].grid(visible = True, ls = ":", color = "green")
ax_dict["logo"].set_yticks([0,10,20,30,40,50,60,70,80,90,100])
ax_dict["bar"].set_yticks([0,10,20,30,40,50,60,70,80,90,100])
ax_dict["alpha"].set_yticks([0,1,2,3,4,5,6,7,8,9,10])
ax_dict["beta"].set_yticks([0,1,2,3,4,5,6,7,8,9,10])
ax_dict["edge"].set_yticks([0,1,2,3,4,5,6,7,8,9,10])
figure_canvas = FigureCanvasTkAgg(fig, tab2)
figure_canvas.draw()
figure_canvas.get_tk_widget().pack(expand=TRUE, fill=BOTH)
fig.set_tight_layout(True)
root.mainloop()
What did you try and what were you expecting?
I searched the web, but found nothing for this special case.
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
axins =inset_axes(ax_dict["logo"], width="70%", height=1.8, loc=1)
axins is then the axes inside the plot "logo", which was defined through
ax_dict = fig.subplot_mosaic(layout)
ax_dict["logo"].annotate(
xy = (3.3, 95),
text = "0-1000 €",
size = 10
)
Related
I noticed that when I reduce the code to the smallest possible, it works like a charm. However, when the code stays as it is, it won't change the variable (bore in my case) and I fail to see what I do wrong. Has anyone an idea what's going wrong?
import math
import matplotlib.animation as animation
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
from matplotlib import style
import numpy as np
import serial
import tkinter as tk
style.use('fivethirtyeight')
stroke=30
conrod=75
bore=40
color_bg = '#000040'
color_plot = '#606060'
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate = 500000,
timeout=None
)
ser.flushInput()
ser.flushOutput()
#xs = [1,2,3,4,5,6,7,8,9]
y1 = [4,5,6,7,6,5,4,5,6]
xs = np.linspace(0, stroke, 50)
#static elements of the plot
fig = plt.figure(figsize=(4,4))
fig.patch.set_facecolor(color_bg)
ax1 = fig.add_subplot(1,1,1)
ax1.set_facecolor(color_bg)
for axis in ['top', 'bottom', 'left', 'right']:
ax1.spines[axis].set_linewidth(2) # change width
ax1.spines[axis].set_color(color_plot) # change color
plt.grid(color=color_plot, linestyle='-', linewidth=1)
# set various colors
ax1.spines['bottom'].set_color('grey')
ax1.spines['top'].set_color('grey')
ax1.spines['right'].set_color('grey')
#ax1.spines['right'].set_linewidth(2)
ax1.spines['left'].set_color('grey')
#ax1.spines['left'].set_lw(2)
ax1.xaxis.label.set_color('white')
ax1.yaxis.label.set_color('white')
ax1.tick_params(colors='yellow', which='both')
#labels
def get_serial_data():
serialData = ser.readline()
serialData = serialData.decode()
floats=[float(value) for value in serialData.split(',')]
array_floats=np.array(np.round(floats,3))
return (array_floats)
def animate(i):
array_floats = get_serial_data()
#print(array_floats)
array_pressure=array_floats[:-2]
array_pressure_1=array_pressure[:-50]
array_pressure_b=array_pressure[50:]
array_pressure_2=array_pressure_b[::-1]
#just for testing
array_pressure_1 = array_pressure_1 + 5 * np.random.rand(50) - 2.5
array_pressure_2 = array_pressure_2 + 5 * np.random.rand(50) - 2.5
#calculation of physical units
average_1=np.average(array_pressure_1)
average_2=np.average(array_pressure_2)
medium_pressure=average_1-average_2
#extreme points
max_value = np.round(np.max(array_pressure_1), 2)
min_value = np.round(np.min(array_pressure_2), 2)
index_max = array_pressure_1.argmax()
index_min = array_pressure_2.argmin()
#arrays for extreme points
xmax = xs[index_max]
xmin = xs[index_min]
extremex = np.array([xmax, xmin])
extremey = np.array([max_value, min_value])
arduino_looptime=array_floats[100]
arduino_duration=array_floats[101]
rpm=int(1000*60/arduino_looptime)
P = round((pow((0.05*bore),2)*math.pi*0.001*stroke*rpm*medium_pressure/3057), 2)
#print(P)
arduino_duration=array_floats[101]
#print(arduino_duration)
#write values to widgets
label_power.config(text=str(P)+" kW")
label_rpm.config(text="at "+str(rpm)+" rpm")
label_pmax.config(text="& "+str(max_value)+" bar max")
label_pmin.config(text="& "+str(min_value)+" bar min")
random = np.random.rand(9)
ys = y1 * random
ax1.clear()
ax1.plot(xs, array_pressure_1, linewidth=1)
ax1.plot(xs, array_pressure_2, linewidth=1)
plt.axvline(x = xmax, color = 'white', linestyle=':', linewidth=1.5, label = 'axvline - full height')
plt.axvline(x = xmin, color = 'white', linestyle=':', linewidth=1.5, label = 'axvline - full height')
plt.axhline(y = max_value, color = 'white', linestyle=':', linewidth=1.5, label = 'axvline - full height')
plt.axhline(y = min_value, color = 'white', linestyle=':', linewidth=1.5, label = 'axvline - full height')
ax1.scatter(extremex, extremey)
plt.fill_between(xs, array_pressure_1, array_pressure_2, color = 'lightpink', alpha = 0.4)
plt.title("PV-Diagram", color='yellow', fontsize=36)
plt.xlabel("Piston Position in mm", color='yellow')
plt.ylabel("Pressure in Bar", color='yellow')
def main():
global label_power
global label_rpm
global label_pmax
global label_pmin
global button_bore
global button_stroke
global button_conrod
global entry_bore
global entry_stroke
global entry_conrod
window = tk.Tk()
window.title('Plotting in Tkinter')
window.geometry("1000x1000")
window.configure(bg=color_bg)
canvas = FigureCanvasTkAgg(fig, master = window)
canvas.draw()
canvas.get_tk_widget().place(relx=0.05, rely=0.05, anchor=tk.NW)
title = tk.Label(window, text="Engine Indicator", bg=color_bg, fg='yellow')
title.config(font=("Arial", 40))
title.place(relx=0.95, rely=0.125, anchor=tk.SE)
label_power = tk.Label(window, text="str(val)"+" kW", bg=color_bg, fg='yellow')
label_power.config(font=("Arial", 30))
label_power.place(relx=0.95, rely=0.2, anchor=tk.SE)
label_rpm = tk.Label(window, text="at "+"srt(val)"+" rpm", bg=color_bg, fg='yellow')
label_rpm.config(font=("Arial", 30))
label_rpm.place(relx=0.95, rely=0.275, anchor=tk.SE)
label_pmax = tk.Label(window, text="& "+"str(val)"+" bar", bg=color_bg, fg='yellow')
label_pmax.config(font=("Arial", 30))
label_pmax.place(relx=0.95, rely=0.35, anchor=tk.SE)
label_pmin = tk.Label(window, text="& "+"str(val)"+" bar", bg=color_bg, fg='yellow')
label_pmin.config(font=("Arial", 30))
label_pmin.place(relx=0.95, rely=0.425, anchor=tk.SE)
entry_bore = tk.Entry(window)
entry_bore.place(relx=0.85, rely=0.5, anchor=tk.SE)
button_bore = tk.Button(window, text="Bore", command=buttonbore())
button_bore.place(relx=0.95, rely=0.5, anchor=tk.SE)
entry_stroke = tk.Entry(window)
entry_stroke.place(relx=0.85, rely=0.55, anchor=tk.SE)
button_stroke = tk.Button(window, text="Stroke")
button_stroke.place(relx=0.95, rely=0.55, anchor=tk.SE)
entry_conrod = tk.Entry(window)
entry_conrod.place(relx=0.85, rely=0.6, anchor=tk.SE)
button_conrod = tk.Button(window, text="Conrod")
button_conrod.place(relx=0.95, rely=0.6, anchor=tk.SE)
print(bore)
#animate plot
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
def buttonbore():
bore = entry_bore.get()
return bore
'''
def button_stroke():
stroke = entry_stroke.get()
def button_conrod():
conrod = entry_conrod.get()
'''
main()
And this is the reduced code, which is as short as it can get. Remember I can't exclude serial input, because I don't know how else I create a zero-dimensional array of 103 numbers that is sent every second to the serial port.:
import tkinter as tk
import math
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2Tk)
import numpy as np
from matplotlib import pyplot as plt
import serial
from matplotlib import style
import matplotlib.animation as animation
import time
style.use('fivethirtyeight')
color_bg = '#000040'
color_plot = '#606060'
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate = 500000,
timeout=None
)
ser.flushInput()
ser.flushOutput()
#xs = [1,2,3,4,5,6,7,8,9]
bore = 40
stroke = 30
conrod = 70
y1 = [4,5,6,7,6,5,4,5,6]
xs = np.linspace(0, stroke, 50)
#static elements of the plot
fig = plt.figure(figsize=(4,4))
fig.patch.set_facecolor(color_bg)
ax1 = fig.add_subplot(1,1,1)
def get_serial_data():
serialData = ser.readline()
serialData = serialData.decode()
floats=[float(value) for value in serialData.split(',')]
array_floats=np.array(np.round(floats,3))
return (array_floats)
def animate(i):
array_floats = get_serial_data()
#print(array_floats)
array_pressure=array_floats[:-2]
array_pressure_1=array_pressure[:-50]
array_pressure_b=array_pressure[50:]
array_pressure_2=array_pressure_b[::-1]
#just for testing
array_pressure_1 = array_pressure_1 + 5 * np.random.rand(50) - 2.5
array_pressure_2 = array_pressure_2 + 5 * np.random.rand(50) - 2.5
random = np.random.rand(9)
ys = y1 * random
ax1.clear()
ax1.plot(xs, array_pressure_1, linewidth=1)
ax1.plot(xs, array_pressure_2, linewidth=1)
print(bore)
def main():
global button_bore
global entry_bore
window = tk.Tk()
window.title('Plotting in Tkinter')
window.geometry("1000x1000")
window.configure(bg=color_bg)
canvas = FigureCanvasTkAgg(fig, master = window)
canvas.draw()
canvas.get_tk_widget().place(relx=0.05, rely=0.05, anchor=tk.NW)
entry_bore = tk.Entry(window)
entry_bore.place(relx=0.85, rely=0.5, anchor=tk.SE)
button_bore = tk.Button(window, text="Bore", command=button_bore)
button_bore.place(relx=0.95, rely=0.5, anchor=tk.SE)
#print(bore)
#animate plot
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
def button_bore():
bore = entry_bore.get()
#print(bore)
return bore
main()
Trying to embed matplotlib in tkinter GUI, however I get the error:
TypeError: init() got multiple values for argument 'master'
Could you tell me please how to handle it?
Here's the code:
interface - tkinter GUI
visualizer - selv updating graph that uses function live_plotter from pylive_mod file
interface:
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 6 10:24:35 2020
#author: Dar0
"""
from tkinter import * #import tkinter module
from visualizer import main #import module 'visualizer' that shows the graph in real time
class Application(Frame):
''' Interface for visualizing graphs, indicators and text-box. '''
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
# Label of the 1st graph
Label(self,
text='Hook Load / Elevator Height / Depth vs Time'
).grid(row = 0, column = 0, sticky = W)
# Graph 1 - Hook Load / Elevator Height / Depth vs Time
# button that displays the plot
plot_button = Button(self,
master = root,
command = main,
height = 2,
width = 10,
text = "Plot").grid(row = 1, column = 0, sticky = W)
# place the button
# in main window
# Label of the 2nd graph
Label(self,
text = 'Hook Load / Elevator Height vs Time'
).grid(row = 2, column = 0, sticky = W)
# Graph 2 - Hook Load / Elevator Height vs Time
#Label of the 3rd graph
Label(self,
text = 'Hook Load vs Time'
).grid(row = 4, column = 0, sticky = W)
#Graph 3 - Hook Load vs Time
#Label of the 1st indicator
Label(self,
text = '1st performance indicator'
).grid(row = 0, column = 1, sticky = W)
#1st performance indicator
#Label of 2nd performance indicator
Label(self,
text = '2nd performance indicator'
).grid(row = 2, column = 1, sticky = W)
#2nd performance indicator
#Label of 3rd performance indicator
Label(self,
text = '3rd performance indicator'
).grid(row = 4, column = 1, sticky = W)
#Text-box showing comments based on received data
self.text_box = Text(self, width = 50, height = 10, wrap = WORD)
self.text_box.grid(row = 6, column = 0, columnspan = 1)
self.text_box.delete(0.0, END)
self.text_box.insert(0.0, 'My message will be here.')
#Main part
root = Tk()
root.title('WiTSML Visualizer by Dar0')
app = Application(root)
root.mainloop()
visualizer:
#WiTSML visualizer
#Created by Dariusz Krol
#import matplotlib
#matplotlib.use('TkAgg')
#from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
#from matplotlib.figure import Figure
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random
class Visualizer(object):
""" Includes all the methods needed to show streamed data. """
def __init__(self):
self.file_path = 'C:/Anaconda/_my_files/witsml_reader/modified_witsml.csv' #Defines which file is streamed
self.datetime_mod = []
self.bpos_mod = []
self.woh_mod = []
self.torq_mod = []
self.spp_mod = []
self.depth_mod = []
self.flow_in_mod = []
self.rpm_mod = []
def open_file(self):
self.df = pd.read_csv(self.file_path, low_memory = False, nrows = 300000) #Opens the STREAMED file (already modified so that data convert is not required)
self.df = self.df.drop(0)
self.df = pd.DataFrame(self.df)
return self.df
def convert_dataframe(self):
self.df = self.df.values.T.tolist() #Do transposition of the dataframe and convert to list
#Columns are as following:
# - DATETIME
# - BPOS
# - WOH
# - TORQ
# - SPP
# - DEPTH
# - FLOW_IN
# - RPM
self.datetime_value = self.df[0]
self.bpos_value = self.df[1]
self.woh_value = self.df[2]
self.torq_value = self.df[3]
self.spp_value = self.df[4]
self.depth_value = self.df[5]
self.flow_in_value = self.df[5]
self.rpm_value = self.df[7]
return self.datetime_value, self.bpos_value, self.woh_value, self.torq_value, self.spp_value, self.depth_value, self.flow_in_value, self.rpm_value
#print(self.bpos_value)
def deliver_values(self, no_dp, columns):
''' Method gets no_dp amount of data points from the original file. '''
self.no_dp = no_dp #defines how many data points will be presented in the graph
val_dict = {
'datetime': [self.datetime_value, self.datetime_mod],
'bpos': [self.bpos_value, self.bpos_mod],
'woh': [self.woh_value, self.woh_mod],
'torq': [self.torq_value, self.torq_mod],
'spp': [self.spp_value, self.spp_mod],
'depth': [self.depth_value, self.depth_mod],
'flow_in': [self.flow_in_value, self.flow_in_mod],
'rpm': [self.rpm_value, self.rpm_mod]
}
for item in columns:
if self.no_dp > len(val_dict[item][0]):
dp_range = len(val_dict[item][0])
else:
dp_range = self.no_dp
for i in range(dp_range):
val_dict[item][1].append(val_dict[item][0][i])
return self.datetime_mod, self.bpos_mod, self.woh_mod, self.torq_mod, self.spp_mod, self.depth_mod, self.flow_in_mod, self.rpm_mod
def show_graph2(self):
from pylive_mod import live_plotter
self.open_file()
self.convert_dataframe()
self.deliver_values(no_dp = 100000, columns = ['datetime', 'depth', 'bpos', 'woh'])
fst_p = 0
size = 300 # density of points in the graph (100 by default)
x_vec = self.datetime_mod[fst_p:size]
y_vec = self.depth_mod[fst_p:size]
y2_vec = self.bpos_mod[fst_p:size]
y3_vec = self.woh_mod[fst_p:size]
line1 = []
line2 = []
line3 = []
for i in range(self.no_dp):
#print(self.datetime_mod[i:6+i])
#print('Ostatni element y_vec: ', y_vec[-1])
#print(x_vec)
x_vec[-1] = self.datetime_mod[size+i]
y_vec[-1] = self.depth_mod[size+i]
y2_vec[-1] = self.bpos_mod[size+i]
y3_vec[-1] = self.woh_mod[size+i]
line1, line2, line3 = live_plotter(x_vec, y_vec, y2_vec, y3_vec, line1, line2, line3)
x_vec = np.append(x_vec[1:], 0.0)
y_vec = np.append(y_vec[1:], 0.0)
y2_vec = np.append(y2_vec[1:], 0.0)
y3_vec = np.append(y3_vec[1:], 0.0)
def main():
Graph = Visualizer()
Graph.open_file() #Opens the streamed file
Graph.convert_dataframe() #Converts dataframe to readable format
Graph.show_graph2()
#Show us the graph
#main()
pylive_mod (live_plotter):
def live_plotter(x_data, y1_data, y2_data, y3_data, line1, line2, line3, identifier='',pause_time=1):
if line1 == [] and line2 == [] and line3 == []:
# this is the call to matplotlib that allows dynamic plotting
plt.ion()
#fig = Figure(figsize = (5, 4), dpi = 100)
#host = fig.add_subplot()
fig, host = plt.subplots()
fig.set_figheight(7) #adjust figure's height
fig.set_figwidth(14) #adjust figure's width
fig.subplots_adjust(0.15)
#Line1
#line1 = host
ln1 = host
ln2 = host.twinx()
ln3 = host.twinx()
ln2.spines['right'].set_position(('axes', 1.))
ln3.spines['right'].set_position(('axes', 1.12))
make_patch_spines_invisible(ln2)
make_patch_spines_invisible(ln3)
ln2.spines['right'].set_visible(True)
ln3.spines['right'].set_visible(True)
ln1.set_xlabel('Date & Time') #main x axis
ln1.set_ylabel('Depth') #left y axis
ln2.set_ylabel('Elevator Height')
ln3.set_ylabel('Weight on Hook')
#
x_formatter = FixedFormatter([x_data])
x_locator = FixedLocator([x_data[5]])
ln1.xaxis.set_major_formatter(x_formatter)
ln1.xaxis.set_major_locator(x_locator)
#
ln1.locator_params(nbins = 5, axis = 'y')
ln1.tick_params(axis='x', rotation=90) #rotates x ticks 90 degrees down
ln2.axes.set_ylim(0, 30)
ln3.axes.set_ylim(200, 250)
line1, = ln1.plot(x_data, y1_data, color = 'black', linestyle = 'solid', alpha=0.8, label = 'Depth')
line2, = ln2.plot(x_data, y2_data, color = 'blue', linestyle = 'dashed', alpha=0.8, label = 'Elevator Height')
line3, = ln3.plot(x_data, y3_data, color = 'red', linestyle = 'solid', alpha=0.8, label = 'Weight on Hook')
# ----- embedding -----
canvas = FigureCanvasTkAgg(fig, master = root)
canvas.draw()
# placing the canvas on the Tkinter window
canvas.get_tk_widget().pack()
# creating the Matplotlib toolbar
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
# placing the toolbar on the Tkinter window
canvas.get_tk_widget().pack()
#----- -----
plt.title('WiTSML Visualizer')
fig.tight_layout() #the graphs is not clipped on sides
#Shows legend
lines = [line1, line2, line3]
host.legend(lines, [l.get_label() for l in lines], loc = 'lower left')
#Shows grid
plt.grid(True)
#Shows the whole graph
plt.show()
# after the figure, axis, and line are created, we only need to update the y-data
mod_x_data = convert_x_data(x_data, 10)
line1.axes.set_xticklabels(mod_x_data)
line1.set_ydata(y1_data)
line2.set_ydata(y2_data)
line3.set_ydata(y3_data)
#Debugging
#rint('plt.lim: ', ln2.axes.get_ylim())
# adjust limits if new data goes beyond bounds
# limit for line 1
if np.min(y1_data)<=line1.axes.get_ylim()[0] or np.max(y1_data)>=line1.axes.get_ylim()[1]:
plt.ylim(0, 10)
line1.axes.set_ylim([np.min(y1_data)-np.std(y1_data),np.max(y1_data)+np.std(y1_data)])
# limit for line 2
if np.min(y2_data)<=line2.axes.get_ylim()[0] or np.max(y2_data)>=line2.axes.get_ylim()[1]:
plt.ylim([np.min(y2_data)-np.std(y2_data),np.max(y2_data)+np.std(y2_data)])
#plt.ylim(0, 25)
# limit for line 3
if np.min(y3_data)<=line3.axes.get_ylim()[0] or np.max(y3_data)>=line3.axes.get_ylim()[1]:
plt.ylim([np.min(y3_data)-np.std(y3_data),np.max(y3_data)+np.std(y3_data)])
#plt.ylim(0, 25)
# Adds lines to the legend
#host.legend(lines, [l.get_label() for l in lines])
# this pauses the data so the figure/axis can catch up - the amount of pause can be altered above
plt.pause(pause_time)
# return line so we can update it again in the next iteration
return line1, line2, line3
I have build simple tkinter GUI.
Now, I am trying to visualise 3 different graphs (by calling the same function with different variables) and place them in 3 different rows of the GUI.
When I do that I get 2 problems:
Every time I run the script (interface.py) I get 2 windows - both GUI and external graph's window. How to get rid of the second one?
I am not able to visualize all the 3 graphs. The script stops after showing the first one. I believe this is because of that the first graph works in a loop (iterates through plenty of data points). Is there any work around it?
Interface:
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 6 10:24:35 2020
#author: Dar0
"""
from tkinter import * #import tkinter module
from visualizer import main #import module 'visualizer' that shows the graph in real time
class Application(Frame):
''' Interface for visualizing graphs, indicators and text-box. '''
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
# Label of the 1st graph
Label(self,
text='Hook Load / Elevator Height / Depth vs Time'
).grid(row = 0, column = 0, sticky = W)
# Graph 1 - Hook Load / Elevator Height / Depth vs Time
# button that displays the plot
#plot_button = Button(self,2
# command = main,
# height = 2,
# width = 10,
# text = "Plot"
# ).grid(row = 1, column = 0, sticky = W)
self.graph_1 = main(root, 1, 0)
# place the button
# in main window
# Label of the 2nd graph
Label(self,
text = 'Hook Load / Elevator Height vs Time'
).grid(row = 3, column = 0, sticky = W)
# Graph 2 - Hook Load / Elevator Height vs Time
self.graph_2 = main(root, 4, 0)
#Label of the 3rd graph
Label(self,
text = 'Hook Load vs Time'
).grid(row = 6, column = 0, sticky = W)
#Graph 3 - Hook Load vs Time
#Label of the 1st indicator
Label(self,
text = '1st performance indicator'
).grid(row = 0, column = 1, sticky = W)
#1st performance indicator
#Label of 2nd performance indicator
Label(self,
text = '2nd performance indicator'
).grid(row = 3, column = 1, sticky = W)
#2nd performance indicator
#Label of 3rd performance indicator
Label(self,
text = '3rd performance indicator'
).grid(row = 6, column = 1, sticky = W)
#Text-box showing comments based on received data
self.text_box = Text(self, width = 50, height = 10, wrap = WORD)
self.text_box.grid(row = 9, column = 0, columnspan = 1)
self.text_box.delete(0.0, END)
self.text_box.insert(0.0, 'My message will be here.')
#Main part
root = Tk()
root.title('WiTSML Visualizer by Dar0')
app = Application(root)
root.mainloop()
Visualizer:
#WiTSML visualizer
#Created by Dariusz Krol
#import matplotlib
#matplotlib.use('TkAgg')
#from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
#from matplotlib.figure import Figure
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random
class Visualizer(object):
""" Includes all the methods needed to show streamed data. """
def __init__(self):
self.file_path = 'C:/Anaconda/_my_files/witsml_reader/modified_witsml.csv' #Defines which file is streamed
self.datetime_mod = []
self.bpos_mod = []
self.woh_mod = []
self.torq_mod = []
self.spp_mod = []
self.depth_mod = []
self.flow_in_mod = []
self.rpm_mod = []
def open_file(self):
self.df = pd.read_csv(self.file_path, low_memory = False, nrows = 300000) #Opens the STREAMED file (already modified so that data convert is not required)
self.df = self.df.drop(0)
self.df = pd.DataFrame(self.df)
return self.df
def convert_dataframe(self):
self.df = self.df.values.T.tolist() #Do transposition of the dataframe and convert to list
#Columns are as following:
# - DATETIME
# - BPOS
# - WOH
# - TORQ
# - SPP
# - DEPTH
# - FLOW_IN
# - RPM
self.datetime_value = self.df[0]
self.bpos_value = self.df[1]
self.woh_value = self.df[2]
self.torq_value = self.df[3]
self.spp_value = self.df[4]
self.depth_value = self.df[5]
self.flow_in_value = self.df[5]
self.rpm_value = self.df[7]
return self.datetime_value, self.bpos_value, self.woh_value, self.torq_value, self.spp_value, self.depth_value, self.flow_in_value, self.rpm_value
#print(self.bpos_value)
def deliver_values(self, no_dp, columns):
''' Method gets no_dp amount of data points from the original file. '''
self.no_dp = no_dp #defines how many data points will be presented in the graph
val_dict = {
'datetime': [self.datetime_value, self.datetime_mod],
'bpos': [self.bpos_value, self.bpos_mod],
'woh': [self.woh_value, self.woh_mod],
'torq': [self.torq_value, self.torq_mod],
'spp': [self.spp_value, self.spp_mod],
'depth': [self.depth_value, self.depth_mod],
'flow_in': [self.flow_in_value, self.flow_in_mod],
'rpm': [self.rpm_value, self.rpm_mod]
}
for item in columns:
if self.no_dp > len(val_dict[item][0]):
dp_range = len(val_dict[item][0])
else:
dp_range = self.no_dp
for i in range(dp_range):
val_dict[item][1].append(val_dict[item][0][i])
return self.datetime_mod, self.bpos_mod, self.woh_mod, self.torq_mod, self.spp_mod, self.depth_mod, self.flow_in_mod, self.rpm_mod
def show_graph2(self, tr_val, row, column):
from pylive_mod import live_plotter, live_plotter2
self.open_file()
self.convert_dataframe()
self.deliver_values(no_dp = 100000, columns = ['datetime', 'depth', 'bpos', 'woh'])
fst_p = 0
size = 300 # density of points in the graph (100 by default)
x_vec = self.datetime_mod[fst_p:size]
y_vec = self.depth_mod[fst_p:size]
y2_vec = self.bpos_mod[fst_p:size]
y3_vec = self.woh_mod[fst_p:size]
line1 = []
line2 = []
line3 = []
for i in range(self.no_dp):
#print(self.datetime_mod[i:6+i])
#print('Ostatni element y_vec: ', y_vec[-1])
#print(x_vec)
x_vec[-1] = self.datetime_mod[size+i]
y_vec[-1] = self.depth_mod[size+i]
y2_vec[-1] = self.bpos_mod[size+i]
y3_vec[-1] = self.woh_mod[size+i]
line1, line2, line3 = live_plotter2(tr_val, row, column, x_vec, y_vec, y2_vec, y3_vec, line1, line2, line3)
x_vec = np.append(x_vec[1:], 0.0)
y_vec = np.append(y_vec[1:], 0.0)
y2_vec = np.append(y2_vec[1:], 0.0)
y3_vec = np.append(y3_vec[1:], 0.0)
def main(tr_val, row, column):
Graph = Visualizer()
Graph.open_file() #Opens the streamed file
Graph.convert_dataframe() #Converts dataframe to readable format
Graph.show_graph2(tr_val, row, column)
#Show us the graph
#main()
Function that creates the graph:
def live_plotter2(tr_val, row, column, x_data, y1_data, y2_data, y3_data, line1, line2, line3, identifier='',pause_time=1):
if line1 == [] and line2 == [] and line3 == []:
# this is the call to matplotlib that allows dynamic plotting
plt.ion()
fig = plt.figure(figsize = (5, 4), dpi = 100)
fig.subplots_adjust(0.15)
# -------------------- FIRST GRAPH --------------------
host = fig.add_subplot()
ln1 = host
ln2 = host.twinx()
ln3 = host.twinx()
ln2.spines['right'].set_position(('axes', 1.))
ln3.spines['right'].set_position(('axes', 1.12))
make_patch_spines_invisible(ln2)
make_patch_spines_invisible(ln3)
ln2.spines['right'].set_visible(True)
ln3.spines['right'].set_visible(True)
ln1.set_xlabel('Date & Time') #main x axis
ln1.set_ylabel('Depth') #left y axis
ln2.set_ylabel('Elevator Height')
ln3.set_ylabel('Weight on Hook')
#
x_formatter = FixedFormatter([x_data])
x_locator = FixedLocator([x_data[5]])
#ln1.xaxis.set_major_formatter(x_formatter)
ln1.xaxis.set_major_locator(x_locator)
#
ln1.locator_params(nbins = 5, axis = 'y')
ln1.tick_params(axis='x', rotation=90) #rotates x ticks 90 degrees down
ln2.axes.set_ylim(0, 30)
ln3.axes.set_ylim(200, 250)
line1, = ln1.plot(x_data, y1_data, color = 'black', linestyle = 'solid', alpha=0.8, label = 'Depth')
line2, = ln2.plot(x_data, y2_data, color = 'blue', linestyle = 'dashed', alpha=0.8, label = 'Elevator Height')
line3, = ln3.plot(x_data, y3_data, color = 'red', linestyle = 'solid', alpha=0.8, label = 'Weight on Hook')
fig.tight_layout() #the graphs is not clipped on sides
plt.title('WiTSML Visualizer')
plt.grid(True)
#Shows legend
lines = [line1, line2, line3]
host.legend(lines, [l.get_label() for l in lines], loc = 'lower left')
#Shows the whole graph
#plt.show()
#-------------------- Embedding --------------------
canvas = FigureCanvasTkAgg(fig, master=tr_val)
canvas.draw()
canvas.get_tk_widget().grid(row=row, column=column, ipadx=40, ipady=20)
# navigation toolbar
toolbarFrame = tk.Frame(master=tr_val)
toolbarFrame.grid(row=row,column=column)
toolbar = NavigationToolbar2Tk(canvas, toolbarFrame)
# after the figure, axis, and line are created, we only need to update the y-data
mod_x_data = convert_x_data(x_data, 20)
line1.axes.set_xticklabels(mod_x_data)
line1.set_ydata(y1_data)
line2.set_ydata(y2_data)
line3.set_ydata(y3_data)
#Debugging
#rint('plt.lim: ', ln2.axes.get_ylim())
# adjust limits if new data goes beyond bounds
# limit for line 1
if np.min(y1_data)<=line1.axes.get_ylim()[0] or np.max(y1_data)>=line1.axes.get_ylim()[1]:
plt.ylim(0, 10)
line1.axes.set_ylim([np.min(y1_data)-np.std(y1_data),np.max(y1_data)+np.std(y1_data)])
# limit for line 2
if np.min(y2_data)<=line2.axes.get_ylim()[0] or np.max(y2_data)>=line2.axes.get_ylim()[1]:
plt.ylim([np.min(y2_data)-np.std(y2_data),np.max(y2_data)+np.std(y2_data)])
#plt.ylim(0, 25)
# limit for line 3
if np.min(y3_data)<=line3.axes.get_ylim()[0] or np.max(y3_data)>=line3.axes.get_ylim()[1]:
plt.ylim([np.min(y3_data)-np.std(y3_data),np.max(y3_data)+np.std(y3_data)])
#plt.ylim(0, 25)
# Adds lines to the legend
#host.legend(lines, [l.get_label() for l in lines])
# this pauses the data so the figure/axis can catch up - the amount of pause can be altered above
plt.pause(pause_time)
# return line so we can update it again in the next iteration
return line1, line2, line3
The key is to not use pyplot when you want to plot within tkinter as shown in the official example. Use matplotlib.figure.Figure instead (see this for added info).
Below is a minimum sample that plots 3 independent graphs along a Text widget which I see in your code:
import pandas as pd
import numpy as np
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
class Graph(tk.Frame):
def __init__(self, master=None, title="", *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.fig = Figure(figsize=(4, 3))
ax = self.fig.add_subplot(111)
df = pd.DataFrame({"values": np.random.randint(0, 50, 10)}) #dummy data
df.plot(ax=ax)
self.canvas = FigureCanvasTkAgg(self.fig, master=self)
self.canvas.draw()
tk.Label(self, text=f"Graph {title}").grid(row=0)
self.canvas.get_tk_widget().grid(row=1, sticky="nesw")
toolbar_frame = tk.Frame(self)
toolbar_frame.grid(row=2, sticky="ew")
NavigationToolbar2Tk(self.canvas, toolbar_frame)
root = tk.Tk()
for num, i in enumerate(list("ABC")):
Graph(root, title=i, width=200).grid(row=num//2, column=num%2)
text_box = tk.Text(root, width=50, height=10, wrap=tk.WORD)
text_box.grid(row=1, column=1, sticky="nesw")
text_box.delete(0.0, "end")
text_box.insert(0.0, 'My message will be here.')
root.mainloop()
Result:
I have been working on a gui to display a graph of values by two indices. That works fine as a standalone py file. I have made a gui that will successfully plot and clear a plot on a frame on canvas. I cannot figure out how to merge the two. The issue is in my plotData function (I have added and commented out a version of it that works very well with a simpler graph). I know my problem is in these lines below because I am using both plt and plot1, but now I don't know anymore what either of them do.
#fig1=plt.figure(1)
if len(blob0)>0:
plt.plot(blob0_et, blob0_acc, "s", color="blue", markersize=10, label = '0')
if len(blob1)>0:
plt.plot(blob1_et, blob1_acc, "s", color="red", markersize=10, label = '1')
if len(blob2)>0:
plt.plot(blob2_et, blob2_acc, "s", color="green", markersize=10, label = '2')
plot1 = fig.add_subplot(111) # adding the subplot
plot1.plot(y) # plotting the graph
plot1.set_title ("Circle Calibration, Accumulator vs Edge Threshold", fontsize=12)
plot1.set_ylabel("Accumulator", fontsize=14)
plot1.set_xlabel("Edge Threshold", fontsize=14)
Full Code
import tkinter.filedialog
import os
import tkinter as tk
from tkinter import ttk
import matplotlib
matplotlib.use("TkAgg") # this is the backend of matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import easygui
from matplotlib.legend_handler import HandlerLine2D
import simple_data
import ETvACCUM3
root = tk.Tk()
root.title("Tab Widget")
root.geometry("500x350")
tabControl = ttk.Notebook(root)
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tab4 = ttk.Frame(tabControl)
tabControl.add(tab1, text ='Tab 1')
tabControl.add(tab2, text ='Tab 2')
tabControl.add(tab3, text ='Tab 3')
tabControl.add(tab4, text ='Tab 4')
tk.Grid.rowconfigure(root, 0, weight=1)
tk.Grid.columnconfigure(root, 0, weight=1)
tabControl.grid(column=0, row=0, sticky=tk.E+tk.W+tk.N+tk.S)
#=====================================================================
# TAB 2
#=====================================================================
#=====================================================================
# my_frame_1 and its contents
#=====================================================================
# creating a frame (my_frame_1)
my_frame_1 = tk.Frame(tab2, bd=2, relief=tk.GROOVE)
my_frame_1.pack(side=tk.LEFT, anchor=tk.N, fill=tk.BOTH, expand=True)
#---------------------------------------------------------------------
# the figure that will contain the plot
fig = Figure(figsize = (5, 5), dpi = 100)
#=====================================================================
# frame2 and widgets it contains.
#=====================================================================
def plotData():
fig.clear()
file = easygui.fileopenbox(msg=None, title=None, default="/Users/.../Desktop/tk_gui_grid/", filetypes = None, multiple = False)
print('\n', "This is the selected file:", file, '\n')
# load data as a pandas dataframe
df = pd.read_csv(file, sep='\t', lineterminator='\n')
# make a smaller array by using the loc
df = df.loc[:,['Accum', 'EdgeThr','NumberOfBlobs']]
blob0 = []
blob1 = []
blob2 = []
blob0 = df[df['NumberOfBlobs'] == 0][['Accum', 'EdgeThr']]
blob1 = df[df['NumberOfBlobs'] == 1][['Accum', 'EdgeThr']]
blob2 = df[df['NumberOfBlobs'] == 2][['Accum', 'EdgeThr']]
blob0 = blob0.values.tolist()
blob1 = blob1.values.tolist()
blob2 = blob2.values.tolist()
print('blob2: ',blob2, '\n'*3)
fontTitle = {'family': 'arial',
'color': 'darkred',
'weight': 'normal',
'size': 16,
}
fontAxisLabels = {'family': 'helvetica',
'color': 'darkblue',
'weight': 'normal',
'size': 16,
}
if len(blob0)>0:
blob0_acc, blob0_et = map(list, zip(*blob0))
if len(blob1)>0:
blob1_acc, blob1_et = map(list, zip(*blob1))
if len(blob2)>0:
blob2_acc, blob2_et = map(list, zip(*blob2))
#fig1=plt.figure(1)
if len(blob0)>0:
plt.plot(blob0_et, blob0_acc, "s", color="blue", markersize=10, label = '0')
if len(blob1)>0:
plt.plot(blob1_et, blob1_acc, "s", color="red", markersize=10, label = '1')
if len(blob2)>0:
plt.plot(blob2_et, blob2_acc, "s", color="green", markersize=10, label = '2')
plot1 = fig.add_subplot(111) # adding the subplot
plot1.plot(y) # plotting the graph
plot1.set_title ("Circle Calibration, Acc vs ET", fontsize=12)
plot1.set_ylabel("Acc", fontsize=14)
plot1.set_xlabel("ET", fontsize=14)
toolbar = NavigationToolbar2Tk(my_canvas, my_frame_1)
toolbar.update()
my_canvas.get_tk_widget().pack(side = tkinter.TOP, fill = tkinter.BOTH, expand = 1)
my_canvas.draw_idle()
plt.axis([0,250,0,50])
plt.xlabel('Edge Threshold', fontdict = fontAxisLabels, fontsize = 12)
plt.ylabel('Accumulator', fontdict = fontAxisLabels, fontsize = 12)
plt.title('Accum vs Edge threshold', fontname='arial', color=('black'),fontdict = fontTitle,fontsize = 10)
plt.legend(loc = "upper right")
def getDataFile():
file = easygui.fileopenbox(msg=None, title=None, default="/Users/.../Desktop/tk_gui_grid/", filetypes = None, multiple = False)
print('\n', "This is the selected file:", file, '\n')
# load data as a pandas dataframe
df = pd.read_csv(file, sep='\t', lineterminator='\n')
print(df)
return df
"""
# this function successfully placed a simple plot onto the canvas when the button was pressed
def plotData():
fig.clear()
y = simple_data.some_yVals() #imported from a function in another module
plot1 = fig.add_subplot(111) # adding the subplot
plot1.plot(y) # plotting the graph
plot1.set_title ("Circle Calibration, Accumulator vs Edge Threshold", fontsize=12)
plot1.set_ylabel("Accumulator", fontsize=14)
plot1.set_xlabel("Edge Threshold", fontsize=14)
toolbar = NavigationToolbar2Tk(my_canvas, my_frame_1)
toolbar.update()
my_canvas.get_tk_widget().pack(side = tkinter.TOP, fill = tkinter.BOTH, expand = 1)
my_canvas.draw_idle()
"""
def clearPlot():
fig.clear()
my_canvas.draw_idle()
my_canvas = FigureCanvasTkAgg(fig, master = my_frame_1) # creating the Tkinter canvas containing the Matplotlib figure
my_canvas.get_tk_widget().pack() # placing the canvas on the Tkinter window
my_canvas.draw()
#create another frame(my_frame_2)
my_frame_2 = tk.Frame(tab2, bd=2, relief=tk.GROOVE)
my_frame_2.pack(side=tk.RIGHT)
#---------------------------------------------------------------------
button1 = tk.Button(my_frame_2, text = "Browse \nfiles", command = getDataFile, relief = tk.GROOVE, bg = "red", padx =20,pady =20 )
button1.pack(side="top", fill="x")
button2 = tk.Button(my_frame_2, text = "Plot \nData", command = plotData, relief = tk.GROOVE, bg = "red", padx =20,pady =20 )
button2.pack(side="top", fill="x" )
button3 = tk.Button(my_frame_2, text = "Clear \nPlot", command = clearPlot, relief = tk.GROOVE, bg = "red", padx =20,pady =20 )
button3.pack(side="top", fill="x")
button4 = tk.Button(my_frame_2, text = "Doggy", relief = tk.GROOVE, bg = "red", padx =20,pady =20 )
button4.pack(side="top", fill="x")
root.mainloop()
print('\n'*4)
*** Desired output ***
Any knowledge that helps me understand how to get the plots onto the canvas and to understand the difference between plt.plot and plot1.plot
You should read about the difference between the matplotlib object-oriented interface and the pyplot interface.
In your case you have mixed the two:
pyplot interface
when you call plt.plot, matplotlib will create an Axes instance if one does not already exist, or if one does, it will plot on the current Axes.
Object-oriented interface:
You create an Axes instance called plot1 using plot1 = plt.subplots(111), and then call the function plot from that instance: plot1.plot(). Anything you call from an Axes instance will be displayed on that Axes.
This issue likely comes because an Axes instance is created from your first plt.plot, but then you call plt.subplots and possibly create a different Axes instance which is used for everything afterwards.
In my opinion, it is usually better to use the object-oriented approach, since that way you always know where things you plot are going to end up. Its almost always a bad idea to mix the two approaches, as things end up getting confused somewhere.
Note that in the documentation and in many examples you will see around the web, the Axes instance is often called ax or ax1, rather than plot1 which you have here. Both will work just fine, but that might help to keep in mind when looking at examples elsewhere.
Its hard to tell exactly, and you don't say quite what your desired outcome is, but I think you probably want something like this. Create the plot1 Axes instance before you plot blob0, blob1 or blob2, then call plot1.plot for those three plotting functions too. That should ensure it all turns up on the same Axes.
plot1 = fig.add_subplot(111) # adding the subplot
if len(blob0)>0:
plot1.plot(blob0_et, blob0_acc, "s", color="blue", markersize=10, label = '0')
if len(blob1)>0:
plot1.plot(blob1_et, blob1_acc, "s", color="red", markersize=10, label = '1')
if len(blob2)>0:
plot1.plot(blob2_et, blob2_acc, "s", color="green", markersize=10, label = '2')
plot1.plot(y) # plotting the graph
plot1.set_title ("Circle Calibration, Acc vs ET", fontsize=12)
plot1.set_ylabel("Acc", fontsize=14)
plot1.set_xlabel("ET", fontsize=14)
Its probably not required, but if you are changing to the object-oriented interface, you might as well go the whole way and change it everywhere. For example, these lines:
plt.axis([0,250,0,50])
plt.xlabel('Edge Threshold', fontdict = fontAxisLabels, fontsize = 12)
plt.ylabel('Accumulator', fontdict = fontAxisLabels, fontsize = 12)
plt.title('Accum vs Edge threshold', fontname='arial', color=('black'),fontdict = fontTitle,fontsize = 10)
plt.legend(loc = "upper right")
should become:
plot1.axis([0,250,0,50])
plot1.set_xlabel('Edge Threshold', fontdict = fontAxisLabels, fontsize = 12)
plot1.set_ylabel('Accumulator', fontdict = fontAxisLabels, fontsize = 12)
plot1.set_title('Accum vs Edge threshold', fontname='arial', color=('black'),fontdict = fontTitle,fontsize = 10)
plot1.legend(loc = "upper right")
I am writing a python application using tkinter that plots weather data on a map. I am currently testing out my button that is supposed to plot the data on the map I created using basemap. However, when I run my program, my button does nothing. I defined my function that plots the data and called the function in the command attribute of my button. Anyone know what the problem could be?
import tkinter as tk
from tkinter import ttk
import tkinter.font as tkFont
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import requests
lon = [-76.5152, -76.7311, -76.3055, -76.7277, -75.4714, -75.1652, -77.0011, -75.6624, -78.3947, -77.8600, -78.7583, -79.9959, -80.0851]
lat = [40.3295, 40.1998, 40.0379, 39.9626, 40.6023, 39.9526, 41.2412, 41.4090, 40.5187, 40.7934, 41.1210, 40.4406, 42.1292]
def current_weather():
key = '4a7a419e4e16e2629a4cedc37cbf7e50'
url = 'https://api.openweathermap.org/data/2.5/weather'
global observation
observation = []
for i in range(0,len(lon)):
params = {'lat': lat[i], 'lon': lon[i], 'appid': key, 'units': 'imperial'}
response = requests.get(url, params = params)
weather = response.json()
observation.append(round(weather['main']['temp']))
current_weather()
root = tk.Tk()
#place map on canvas
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot(111)
map = Basemap(llcrnrlon=-80.5, llcrnrlat=39.8, urcrnrlon=-74.7, urcrnrlat=42.3,resolution='i',
lat_0 = 40., lon_0 = -80, ax = ax)
map.drawmapboundary(fill_color='#A6CAE0')
map.drawcounties(zorder = 20)
map.drawstates()
map.fillcontinents(color='#e6b800',lake_color='#A6CAE0')
def plot_data():
for i in range(0, len(lon)):
x,y = map(lon[i], lat[i])
ax.text(x, y, observation[i], fontsize = 7)
#create widgets
canvas = FigureCanvasTkAgg(fig, master = root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
radiovalue = tk.IntVar()
temperature = tk.Radiobutton(root, text = "Plot current temperatures", variable = radiovalue, value = 1)
dewpoint = tk.Radiobutton(root, text = "Plot current dewpoints", variable = radiovalue, value = 2)
wind = tk.Radiobutton(root, text = "Plot current winds", variable = radiovalue, value = 3)
button = tk.Button(root, text = "Plot", command = plot_data)
#organize widgets
temperature.place(relx = 0.025, rely = 0.8)
dewpoint.place(relx = 0.385, rely = 0.8)
wind.place(relx = 0.675, rely = 0.8)
button.place(relx = 0.35, rely = 0.1)
root.mainloop()