0

I am running the following script. The script has to generate a circle.

import pyplot as plt

circle1=plt.Circle((0.5,0.5),.2,color='g', fill=False)
fig=plt.gcf()
fig.gca().add_artist(circle1)
plt.show()

The output is as follows:

enter image description here

I want the output on a Tkinter canvas. How do I go about this?

I read this question and the code given there wants me to import matplotlib but I do not need to import it.

1

1 Answer 1

2

As per the matplotlib examples:

http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html

#!/usr/bin/env python

import matplotlib
matplotlib.use('TkAgg')

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler


from matplotlib.figure import Figure

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

root = Tk.Tk()
root.wm_title("Embedding in TK")


f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)

a.plot(t,s)


# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

toolbar = NavigationToolbar2TkAgg( canvas, root )
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

def on_key_event(event):
    print('you pressed %s'%event.key)
    key_press_handler(event, canvas, toolbar)

canvas.mpl_connect('key_press_event', on_key_event)

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

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

Tk.mainloop()
Sign up to request clarification or add additional context in comments.

9 Comments

This is similar to the link that I attached in the question, and that is precisely what I wanted to bypass.
why don't you want to import matplotlib? essentially you are via import pylab anyway. Its needed for all the canvas hooks into the different toolkits
I am just importing pyplot and it is working. Since I am running it on windows I guess the installation has made it easier...? I don't have to import matplotlib to make pyplot work.
it would do since importing pyplot imports alot to allow it to work, one of the 1st things the pyplot.py file imports is... matplotlib.
See I have installed matplotlib and pyplot. Whenever it needs to import pyplot and pyplot searches for matplotlib it finds it and then I am able to plot the graph without even importing matplotlib (I am serious, I don't know how it finds matplotlib and works without me properly importing it)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.