Reputation: 3343
I have an application that should display the residual error after calibrating some data. The main window is a matplotlib canvas which displays the raw and calibrated data (as defined below):
# Begins the actual Application
class App():
def __init__(self,master):
self.fig = plt.Figure()
self.canvas = FigureCanvasTkAgg(self.fig, master = master)
self.toolbar = NavigationToolbar2TkAgg(self.canvas, root)
self.canvas.get_tk_widget().pack()
self.canvas.draw()
The residual errors I wish to be displayed in a new window but so far I have only succeeded in the new plot being attached to the main window (see the image below this) at the bottom and I am not sure why or how to proceed from here.
My current code for the error plot is as follows (this function is a part of the Class App()
:
####################
# WORK IN PROGRESS #
####################
def displayError(self,errors):
""" This function displays a graph to show the error
after calibration.
"""
self.fig = plt.Figure()
self.canvas = FigureCanvasTkAgg(self.fig, master = root)
self.canvas.show()
self.canvas.get_tk_widget().pack()
x_array = []
y_array = []
labels = []
for counter,i in enumerate(errors):
x_array.append(counter)
labels.append(i[0])
y_array.append(i[2])
self.axes = self.fig.add_subplot(111)
self.axes.scatter(x_array,y_array)
self.axes.tick_params(axis='x',which='both',bottom='off',top='off',labelbottom='off')
self.axes.set_ylabel('Mass error (ppm)')
self.axes.hlines(0,x_array[1]-0.5,x_array[-1]+0.5)
self.axes.set_xlim(x_array[1]-0.5,x_array[-1]+0.5)
for counter,i in enumerate(errors):
self.axes.annotate(i[1],(x_array[counter]+0.1,y_array[counter]+0.1))
self.axes.vlines(x_array[counter],0,y_array[counter],linestyles='dashed')
#self.canvas.draw()
####################
# WORK IN PROGRESS #
####################
Upvotes: 0
Views: 3268
Reputation: 1539
I think this is because your FigureCanvasTkAgg
s both have the same master
. Can you spawn a new tk.Toplevel
for the second plot? i.e. it would be something like this:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import tkinter as tk
root = tk.Tk()
x = np.arange(0, 10, 1)
y = np.arange(0, 10, 1)
fig1 = plt.Figure()
canvas1 = FigureCanvasTkAgg(fig1, master=root)
canvas1.show()
canvas1.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1.0)
ax = fig1.add_subplot(111)
ax.plot(x,y)
canvas1.draw()
root2 = tk.Toplevel()
fig2 = plt.Figure()
canvas2 = FigureCanvasTkAgg(fig2, master=root2)
canvas2.show()
canvas2.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1.0)
ax = fig2.add_subplot(111)
ax.plot(x,2*y)
canvas2.draw()
so the second window is a 'popup'.
Upvotes: 4