Created
January 18, 2016 18:06
-
-
Save avaccari/7495c660858d35d8ad92 to your computer and use it in GitHub Desktop.
Create a matplotlib plot from within an ArcGIS python toolbox using tkinter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Tkinter as tk | |
import matplotlib | |
from numpy import arange, sin, pi | |
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg | |
from matplotlib.figure import Figure | |
class Toolbox(object): | |
def __init__(self): | |
self.label = "test plotting dev" | |
self.alias = "TestPlot_dev" | |
self.tools = [TestPlotting] | |
class TestPlotting(object): | |
"""Just an example tool to check how to plot in your own window""" | |
def __init__(self): | |
"""Define the tool properties""" | |
self.label = "Can we plot in ArcGIS?" | |
self.description = "Simple matplotlib plotting test." | |
self.canRunInBackground = True | |
return | |
def getParameterInfo(self): | |
"""Define parameter definitions""" | |
params = None | |
return params | |
def isLicensed(self): | |
"""Set whether tool is licensed to execute.""" | |
return True | |
def updateParameters(self, parameters): | |
"""Modify the values and properties of parameters before internal | |
validation is performed. This method is called whenever a parameter | |
has been changed.""" | |
return | |
def updateMessages(self, parameters): | |
"""Modify the messages created by internal validation for each tool | |
parameter. This method is called after internal validation.""" | |
return | |
def execute(self, parameters, messages): | |
"""Evaluate the top 0-100 percentile total variation""" | |
# Configure matplotlib to use tkinter | |
matplotlib.use("TkAgg") | |
# Prepare tkinter | |
self.root = tk.Tk() | |
self.root.title("Plot test") | |
self.root.protocol("WM_DELETE_WINDOW", self._quit) # Call root.quit when the window is closed | |
# Prepare the figure and the plot | |
f = Figure(figsize=(5, 4), dpi=100) | |
a = f.add_subplot(1, 1, 1) | |
t = arange(0.0, 3.0, 0.01) | |
s = sin(2 * pi * t) | |
a.plot(t, s) | |
# Prepare the canvas in matplotlib (fill the entire space) | |
canvas = FigureCanvasTkAgg(f, self.root) | |
canvas.show() | |
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) | |
# Call tkinter mainloop | |
self.root.mainloop() | |
return | |
def _quit(self): | |
"""Clearing up once the plot window is closed""" | |
self.root.quit() | |
self.root.destroy() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment