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.

Community
  • 1
  • 1
praxmon
  • 5,009
  • 22
  • 74
  • 121
  • See https://stackoverflow.com/questions/16849483/which-is-the-recommended-way-to-plot-matplotlib-or-pylab/16849816#16849816 – tacaswell Mar 27 '14 at 19:59

1 Answers1

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()
Naib
  • 999
  • 7
  • 20
  • This is similar to the link that I attached in the question, and that is precisely what I wanted to bypass. – praxmon Mar 27 '14 at 12:50
  • 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 – Naib Mar 27 '14 at 12:59
  • 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. – praxmon Mar 28 '14 at 03:35
  • 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. – Naib Mar 28 '14 at 09:29
  • 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) – praxmon Mar 28 '14 at 10:30
  • because pylab imports it at its namespace. Matplotlib ships with lots of modules, one is matplotlib.py, the main package. There are others which are either imported as an when or in the instance of pyplot.py, a module which is a convenience wrapper for quick plots. I use pyplot alot to quickly plot data BUT todo anything more useful than just plot(x,y) other aspects of Matplotlib are needed – Naib Mar 28 '14 at 10:54
  • So I need to import matplotlib and then start with the example? – praxmon Mar 28 '14 at 11:19
  • please just try the example – Naib Mar 28 '14 at 11:23
  • Okay okay I will if you say so. :D – praxmon Mar 28 '14 at 11:24