Igor Smorąg
Igor Smorąg

Reputation: 62

How can I draw a grid onto a tkinter canvas

I am trying to draw a grid on:

from matplotlib.figure import 

figure = Figure(figsize=(10, 10))
a = figure.add_subplot(111)
a.plot(x, y, '-r')
canvas = FigureCanvasTkAgg(figure, master=window)
canvas.get_tk_widget().pack()
NavigationToolbar2Tk(canvas, plotWindow)
canvas.draw()

plotWindow is just a tkinter.Frame object

Thanks in advance:)

Upvotes: 0

Views: 1624

Answers (2)

Henry Yik
Henry Yik

Reputation: 22493

IIUC, your figsize is too large and it pushes off your NavigationToolbar. Try the below with minimum changes to your code:

from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2Tk
import tkinter as tk

root = tk.Tk()
window = tk.Canvas(root)
window.pack()
plotWindow = tk.Frame(root)
plotWindow.pack()

figure = Figure(figsize=(5, 5))
a = figure.add_subplot(111)
x=[-100,100]
y=[0,4]
a.plot(x, y, '-r')
a.grid()
canvas = FigureCanvasTkAgg(figure, master=window)
canvas.get_tk_widget().pack()
NavigationToolbar2Tk(canvas, plotWindow)
canvas.draw()

root.mainloop()

enter image description here

Upvotes: 1

Bill Bell
Bill Bell

Reputation: 21643

Since I don't have all your code this is what I'd suggest.

After a = figure.add_subplot(111) toss in

ax = f.gca()
ax.set_xticks(numpy.arange(0,1,0.5))

Then after canvas = put in

a.grid()
canvas.show()

Omit canvas.draw()?

Upvotes: 1

Related Questions