简体   繁体   中英

How to overlay multiple plots on the same chart tkinter

I am new to tkinter and python and trying to write a simple program that lets me overlay line charts on the same plot. The code I have written (some taken from the 'matplotlib' site) keeps placing all the charts underneath each other. Is there a simpler way to do this. I cant seem to take the canvas or widgets outside the 'for' loop without it messing something up,my code and test 'csv' files are below.Any help or guidance please as to what I need to change?

from tkinter import ttk
import tkinter
from tkinter import *
from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure

import numpy as np


root = tkinter.Tk()
root.wm_title("Embedding in Tk")
current_list = ['Blue', 'Green', 'Yellow']
for item in current_list:
    x, y = np.loadtxt(item + '_' + 'Test.csv', skiprows=1, usecols=[2, 3],
                      unpack=True, delimiter=',')

    fig = Figure(figsize=(2, 3), dpi=100)
    fig.add_subplot(111).plot(x, y)

    canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
    canvas.draw()
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

    toolbar = NavigationToolbar2Tk(canvas, root)
    toolbar.update()
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)


def on_key_press(event):
    print("you pressed {}".format(event.key))
    key_press_handler(event, canvas, toolbar)

    canvas.mpl_connect("key_press_event", on_key_press)


def _quit():
    root.quit()     # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent
                    # Fatal Python Error: PyEval_RestoreThread: NULL tstate


button = tkinter.Button(master=root, text="Quit", command=_quit)
button.pack(side=tkinter.BOTTOM)

tkinter.mainloop()
# If you put root.destroy() here, it will cause an error if the window is
# closed with the window manager.

csv test files - Blue_Test.csv

Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8
1,100,19,100,Blue,2000,1.00E-19,1.00E-09
2,110,20,101,Blue,3000,5.00E-19,1.00E+00
3,120,21,102,Blue,4000,9.00E-19,2.00E+00
4,150,24,105,Blue,7000,2.10E-18,5.00E+00
5,160,25,106,Blue,8000,2.50E-18,6.00E+00
## csv test files - Green_Test.csv
Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8
1,100,19,2000,Green,2000,1.00E-19,1.00E-09
2,110,20,3001,Green,3000,5.00E-19,1.00E+00
3,120,21,4002,Green,4000,9.00E-19,2.00E+00
4,150,24,5005,Green,7000,2.10E-18,5.00E+00
5,160,25,6006,Green,8000,2.50E-18,6.00E+00
## csv test files - Yellow_Test.csv
Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8
1,100,19,11000,Yellow,2000,1.00E-19,1.00E-09
2,110,20,12001,Yellow,3000,5.00E-19,1.00E+00
3,120,21,13002,Yellow,4000,9.00E-19,2.00E+00
4,150,24,14005,Yellow,7000,2.10E-18,5.00E+00
5,160,25,15006,Yellow,8000,2.50000,5.000000

The problem lies in the for item in current_list: loop, in each iteration of the loop the code creates a new subplot and draws it on the canvas. Instead, you should create one subplot and plot each dataset on it. Then once the loop has completed, draw the populated subplot on the canvas. https://pydatascience.org/2017/11/24/plot-multiple-lines-in-one-chart-with-different-style-python-matplotlib/

import tkinter

import numpy as np
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure

root = tkinter.Tk()
root.wm_title("Embedding in Tk")
current_list = ['Blue', 'Green', 'Yellow']

fig = Figure(figsize=(2, 3), dpi=100)
ax = fig.add_subplot(111)

for item in current_list:
    x, y = np.loadtxt(item + '_' + 'Test.csv', skiprows=1, usecols=[2, 3],
                      unpack=True, delimiter=',')
    ax.plot(x, y, label=item)

fig.legend()

canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)


def on_key_press(event):
    print("you pressed {}".format(event.key))
    key_press_handler(event, canvas, toolbar)

    canvas.mpl_connect("key_press_event", on_key_press)


def _quit():
    root.quit()  # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent
    # Fatal Python Error: PyEval_RestoreThread: NULL tstate


button = tkinter.Button(master=root, text="Quit", command=_quit)
button.pack(side=tkinter.BOTTOM)

tkinter.mainloop()
# If you put root.destroy() here, it will cause an error if the window is
# closed with the window manager.

tkinter 输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM