I've got multiple pages in a notebook and have multiple graphs on each.
Is there a way to use one navigation toolbar for all graphs on a notebook page? Or how can I put each navigation toolbar directly underneath the graph it controls?
I've tried adding padx and pady which I thought would force it under the graph, but it keeps putting both toolbars underneath the Quit button.
I'm pretty new to Python and am really struggling to find an answer to this question!
import numpy as np
import matplotlib.pyplot as plt
import tkinter as app
from tkinter import ttk
from matplotlib.collections import LineCollection
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
x = np.linspace(1, 10, 10)
y = np.square(x)
z = x
#Make the root widget
root = app.Tk()
#Make the notebook
nb = ttk.Notebook(root)
nb.pack()
root.title("Test")
#plot first graph
f1 = app.Frame(nb)
nb.add(f1, text="Test")
app.Label(f1, text="Test window 1").pack(padx=5, pady=5)
fig1 = Figure(figsize=(5, 4), dpi=100)
fig1.add_subplot(111).plot(x, y)
canvas1 = FigureCanvasTkAgg(fig1, master=f1)
canvas1.draw()
canvas1.get_tk_widget().pack(side=app.TOP, fill=app.BOTH, expand=1)
#add toolbar
toolbar1 = NavigationToolbar2Tk(canvas1, f1)
toolbar1.update()
canvas1.get_tk_widget().pack(side=app.TOP, fill=app.BOTH, expand=1)
#plot second graph
fig2 = Figure(figsize=(5, 4), dpi=100)
fig2.add_subplot(111).plot(x, z)
canvas2 = FigureCanvasTkAgg(fig2, master=f1)
canvas2.draw()
canvas2.get_tk_widget().pack(side=app.TOP, fill=app.BOTH, expand=1, padx=5, pady=5)
#add toolbar
toolbar2 = NavigationToolbar2Tk(canvas2, f1)
toolbar2.update()
canvas2.get_tk_widget().pack(side=app.TOP, fill=app.BOTH, expand=1)
app.Button(f1, text='Exit', command=root.destroy).pack(padx=50, pady=50)
nb.select(f1)
nb.enable_traversal()
#Enter the mainloop
root.mainloop()
Related
I would like to change the color of the toolbar when making a matplotlib figure in tkinter. I have managed to find and change the color of two parts. There is one remaining.
My code comes directly from https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_tk_sgskip.html?highlight=embedding%20tk with three additional lines to change colors.
import tkinter
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import numpy as np
root = tkinter.Tk()
root.wm_title("Embedding in Tk")
fig = Figure(figsize=(5, 4), dpi=100)
t = np.arange(0, 3, .01)
fig.add_subplot().plot(t, 2 * np.sin(2 * np.pi * t))
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
color = "#d469a3"
toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False)
toolbar.config(background=color)
toolbar._message_label.config(background=color)
toolbar.update()
button = tkinter.Button(master=root, text="Quit", command=root.quit)
button.pack(side=tkinter.BOTTOM)
toolbar.pack(side=tkinter.BOTTOM)
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
tkinter.mainloop()
This gives me the window:
What is the small, grey rectangle I have pointed out? How do I change its color?
It is an empty label. You can get a reference to it via winfo_children:
print (toolbar.winfo_children()[-2])
# .!navigationtoolbar2tk.!label
And to change its color:
toolbar.winfo_children()[-2].config(background=color)
I do not understand how correctly add matplotlib into panedwindow of tkinter.
I want to have on one side list box and matplotlib interactive plot, on the other side
I need to resize listbox and plot by dragging boundary between them
this is what is desirable
I tried this but I cannot add correctly matplot lib to paned window
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 matplotlib.pyplot as plt
import numpy as np
root = Tk()
m = PanedWindow(root)
m.pack(fill=BOTH, expand=1)
fig = Figure(figsize=(5, 4), dpi=100)
t = np.arange(0, 3, .01)
fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))
canvas = FigureCanvasTkAgg(fig, master = root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=RIGHT, fill=BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=RIGHT, fill=BOTH, expand=1)
m.add(toolbar)
m.add(canvas)
lstbox2 = Listbox(selectmode=MULTIPLE, width=20, height=10)
m.add(lstbox2)
root.mainloop()
Put your canvas and toolbar in a Frame instead, and add that Frame to your paned window:
root = Tk()
...
canvas_frame = Frame(root)
canvas = FigureCanvasTkAgg(fig, master = canvas_frame)
canvas.draw()
canvas.get_tk_widget().pack()
toolbar = NavigationToolbar2Tk(canvas, canvas_frame)
toolbar.update()
toolbar.pack_configure(expand=True)
m.add(canvas_frame)
lstbox2 = Listbox(selectmode=MULTIPLE, width=20, height=10)
m.add(lstbox2)
root.mainloop()
I have a plot in a tkinter application and I am trying to change the background color of the plot using tkinter colors. I have found How to change plot background color? however, I got this error:
ValueError: Invalid RGBA argument: 'AntiqueWhite2'
...indicating I may only be able to use their subset of colors? Does anyone know how to use the tkinter colors here?
Here is what I am currently using (without color change):
self.fig = Figure(figsize=(5, 4), dpi=100)
self.canvas = FigureCanvasTkAgg(self.fig, master=self.master) # A tk.DrawingArea
self.canvas.draw()
self.ax = self.fig.add_subplot(111)
import tkinter
import matplotlib.pyplot as plt
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.geometry("770x633+745+171")
Frame1 = tkinter.Frame(root)
Frame1.place(relx=0.1, rely=0.1, relheight=0.3, relwidth=0.5)
root.wm_title("Embedding in Tk")
t = np.arange(0, 3, .01)
fig = Figure(figsize=(5, 3), dpi=80,facecolor = 'k')
axes1 =fig.add_subplot(111)
# axes1.axis('tight')
axes1.autoscale(enable=True, axis='y', tight=True)
axes1.plot(t, 2 * np.sin(2 * np.pi * t))
# axes1.set_axis_bgcolor('k')
axes1.set_facecolor('k')
axes1.grid(color = 'w')
for label in axes1.xaxis.get_ticklabels():
# label is a Text instance
label.set_color('w')
for label in axes1.yaxis.get_ticklabels():
# label is a Text instance
label.set_color('w')
# label.set_rotation(45)
# label.set_fontsize(1)
for line in axes1.yaxis.get_ticklines():
# line is a Line2D instance
line.set_color('w')
for line in axes1.xaxis.get_ticklines():
# line is a Line2D instance
line.set_color('w')
# line.set_markersize(25)
# line.set_markeredgewidth(3)
for line in axes1.xaxis.get_gridlines():
line.set_color('w')
for line in axes1.yaxis.get_gridlines():
line.set_color('w')
line.set_markeredgewidth(8)
axes1.yaxis.grid(color='w',linewidth=1)
# axes1.set_xmargin(0.9)
axes1.set_xlabel("Time(ns)")
axes1.xaxis.label.set_color('w')
axes1.set_ylabel("Amplitude(mV)")
axes1.yaxis.label.set_color('w')
axes1.xaxis.grid(color='w',linewidth=1)
axes1.spines['bottom'].set_color('white')
axes1.spines['top'].set_color('white')
axes1.spines['left'].set_color('white')
axes1.spines['right'].set_color('white')
canvas = FigureCanvasTkAgg(fig, master=Frame1) # A tk.DrawingArea.
canvas.get_tk_widget().configure(bg='black')
canvas.get_tk_widget().grid(row=1,column=0)
canvas.get_tk_widget().grid(row=1,column=0)
# canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
canvas.draw()
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)
fig.tight_layout()
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()
# plt.tight_layout()
This is an example for change the background as black and change the label, grid, tick labels, ... color.
I'm trying to set up some program that includes a matplotlib graph and tkinter buttons and whatnot below it. However, the matplotlib graph occupies the entire window overlaying the tkinter buttons and stuff.
I'd tried using pack, but it doesn't let me put stuff side by side, so I'd like the tkinter widgets to be arranged with .grid or coordinates.
from tkinter import *
from tkinter import StringVar
import tkinter
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
def eggs():
print("eggs")
root = Tk()
root.geometry("600x600")
root.title("eggs")
fig = Figure(figsize=(10, 6), dpi=100)
x = [1,2,3,4]
y = [1,2,3,4]
AS = [10/2**0]
fig.add_subplot(111).plot(x,y)
#fig.add_subplot(111).plot(AS)
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)
testbutton = Button(root, text = "test button", command = eggs)
testbutton.place(x=100, y=550)
root.mainloop()
Wanting the top part of the window to be occupied by the graph and the buttons and other additional stuff below it.
You can use one Frame to keep graph and its toolbar vertically, and another Frame to keep buttons horizontally. And then you can use pack() to put one Frame at top top and other at the bottom.
The only problem makes figsize=(10, 6) which needs more space than "600x600"
BTW: you can use Button(toolbar, ...) to add button to NavigationToolbar2Tk - see "my tool".
]1
import tkinter as tk
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
# --- functions ---
def eggs():
print("eggs")
# --- main ---
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
AS = [10/2**0]
# ---
root = tk.Tk()
root.geometry("600x600")
root.title("eggs")
# ---
frame_top = tk.Frame(root)
frame_top.pack(fill='both', expand=True)
fig = Figure(dpi=100) # figsize=(10, 6),
fig.add_subplot(111).plot(x,y)
#fig.add_subplot(111).plot(AS)
canvas = FigureCanvasTkAgg(fig, master=frame_top) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(fill='both', expand=True)
toolbar = NavigationToolbar2Tk(canvas, frame_top)
toolbar.update()
tool = tk.Button(toolbar, text="my tool")
tool.pack(side='left')#, fill='x', expand=True)
# ---
frame_bottom = tk.Frame(root)
frame_bottom.pack(fill='x')
button1 = tk.Button(frame_bottom, text="button1")
button1.pack(side='left', fill='x', expand=True)
button2 = tk.Button(frame_bottom, text="button2")
button2.pack(side='left', fill='x', expand=True)
button3 = tk.Button(frame_bottom, text="button3")
button3.pack(side='left', fill='x', expand=True)
# ---
root.mainloop()
I have little experience how to combine matplotlib with tkinter or pyqt.
I need a horizontal scroll bar on the matplotlib chart.
I want to zoom the graph horizontally and scroll the graph from the beginning
to the end of the data that is loaded from the file.
After reviewing the examples, I added a scroll bar.
But she's completely unresponsive to the chart.)
And the process doesn't end when I close the window(how to remove not understood, an example taken from here enter link)
import tkinter
from tkinter import *
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
root = tkinter.Tk()
x = pd.read_csv('file.txt',
index_col='DATE',
parse_dates=True,
infer_datetime_format=True)
z = x.iloc[:, 3].values
N = len(z)
ind = np.arange(N)
fig, ax = plt.subplots()
ax.plot(ind, z)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.show()
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)
scrollbar = tkinter.Scrollbar(master=root, orient=HORIZONTAL)
scrollbar.pack(side=tkinter.BOTTOM, fill=X)
tkinter.mainloop()
After adding a row
scrollbar["command"] = canvas.get_tk_widget().xview
As pointed out- MaxiMouse.
The graph scrolls, but not along the entire length.
I need the scroll to work on all the data after zooming in. I displayed this at the end of the video.
Put this before the tkinter.mainloop() call:
scrollbar["command"] = canvas.get_tk_widget().xview
canvas.get_tk_widget()["xscrollcommand"] = scrollbar.set