| 1 |
|
| 2 |
import matplotlib |
| 3 |
matplotlib.use('TkAgg') |
| 4 |
|
| 5 |
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg |
| 6 |
try: |
| 7 |
from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk as NavigationToolbar2TkAgg |
| 8 |
except ImportError: |
| 9 |
from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg |
| 10 |
from matplotlib.backends.backend_agg import FigureCanvasAgg |
| 11 |
from matplotlib.figure import Figure |
| 12 |
from matplotlib.lines import Line2D |
| 13 |
from matplotlib.axes import Axes |
| 14 |
from matplotlib.ticker import AutoLocator, FixedLocator |
| 15 |
from auto import Points |
| 16 |
if not Points.numpyimported: |
| 17 |
Points.importnumpy() |
| 18 |
transpose = Points.N.transpose |
| 19 |
|
| 20 |
try: |
| 21 |
from mpl_toolkits.mplot3d import Axes3D |
| 22 |
from mpl_toolkits.mplot3d.proj3d import proj_transform, inv_transform |
| 23 |
except ImportError: |
| 24 |
try: |
| 25 |
from matplotlib.axes3d import Axes3D, Axes3DI |
| 26 |
from matplotlib.proj3d import proj_transform, inv_transform |
| 27 |
|
| 28 |
def autoscale_view(self, scalex=True, scaley=True): |
| 29 |
self.set_top_view() |
| 30 |
Axes3DI.autoscale_view = autoscale_view |
| 31 |
Axes3DI.set_xlim3d = Axes3DI.set_w_xlim |
| 32 |
Axes3DI.set_ylim3d = Axes3DI.set_w_ylim |
| 33 |
Axes3DI.set_zlim3d = Axes3DI.set_w_zlim |
| 34 |
Axes3DI.get_xlim3d = Axes3DI.get_w_xlim |
| 35 |
Axes3DI.get_ylim3d = Axes3DI.get_w_ylim |
| 36 |
Axes3DI.get_zlim3d = Axes3DI.get_w_zlim |
| 37 |
except (ImportError, NotImplementedError, NameError): |
| 38 |
Axes3D = None |
| 39 |
|
| 40 |
try: |
| 41 |
import Tkinter |
| 42 |
import tkFileDialog |
| 43 |
except ImportError: |
| 44 |
import tkinter as Tkinter |
| 45 |
from tkinter import filedialog as tkFileDialog |
| 46 |
from auto.graphics import Pmw |
| 47 |
from auto import AUTOutil |
| 48 |
import math |
| 49 |
import string |
| 50 |
from auto.graphics import grapher |
| 51 |
|
| 52 |
GrapherError="GrapherError" |
| 53 |
|
| 54 |
class FigureCanvasTkAggRedraw(FigureCanvasTkAgg): |
| 55 |
def __init__(self,grapher,parent): |
| 56 |
if parent is None: |
| 57 |
parent = Tkinter.Tk() |
| 58 |
parent.wm_title("PyPLAUT") |
| 59 |
FigureCanvasTkAgg.__init__(self,grapher.ax.get_figure(),master=parent) |
| 60 |
|
| 61 |
grapher.toolbar = NavigationToolbar2TkAgg( self, parent ) |
| 62 |
grapher.toolbar.zoom() |
| 63 |
|
| 64 |
self.grapher = grapher |
| 65 |
|
| 66 |
def redraw(self): |
| 67 |
|
| 68 |
self.grapher.plotlabels() |
| 69 |
FigureCanvasTkAgg.draw(self) |
| 70 |
|
| 71 |
def draw(self): |
| 72 |
ax = self.grapher.ax |
| 73 |
d = {} |
| 74 |
if ax is self.grapher.ax3d: |
| 75 |
[d["minx"],d["maxx"]] = ax.get_xlim3d() |
| 76 |
[d["miny"],d["maxy"]] = ax.get_ylim3d() |
| 77 |
[d["minz"],d["maxz"]] = ax.get_zlim3d() |
| 78 |
d["azimuth"] = ax.azim |
| 79 |
d["elevation"] = ax.elev |
| 80 |
d["cur_lims"] = Axes.get_xlim(ax), Axes.get_ylim(ax) |
| 81 |
else: |
| 82 |
[d["minx"],d["maxx"]] = ax.get_xlim() |
| 83 |
[d["miny"],d["maxy"]] = ax.get_ylim() |
| 84 |
for k in list(d): |
| 85 |
|
| 86 |
if k == "cur_lims": |
| 87 |
if map(list, d[k]) == map(list, self.grapher._cur_lims): |
| 88 |
del d[k] |
| 89 |
elif d[k] == self.grapher.cget(k): |
| 90 |
del d[k] |
| 91 |
if d != {}: |
| 92 |
if "cur_lims" in d: |
| 93 |
del d["cur_lims"] |
| 94 |
if d != {}: |
| 95 |
self.grapher._configNoDraw(**d) |
| 96 |
self.redraw() |
| 97 |
return |
| 98 |
FigureCanvasTkAgg.draw(self) |
| 99 |
|
| 100 |
def show(self): |
| 101 |
fig = self.grapher.ax.get_figure() |
| 102 |
dpi = fig.get_dpi() |
| 103 |
self.grapher._configNoDraw( |
| 104 |
realwidth=fig.get_figwidth()*dpi, |
| 105 |
realheight=fig.get_figheight()*dpi) |
| 106 |
self.redraw() |
| 107 |
|
| 108 |
class BasicGrapher(grapher.BasicGrapher): |
| 109 |
"""Documentation string for Basic Grapher |
| 110 |
|
| 111 |
A simple graphing widget |
| 112 |
By Randy P. and Bart O.""" |
| 113 |
def __init__(self,parent=None,**kw): |
| 114 |
self.ax = Figure(figsize=(4.3,3.0)).gca() |
| 115 |
self.ax2d = self.ax |
| 116 |
self.ax3d = None |
| 117 |
if kw.get("hide"): |
| 118 |
self.canvas = FigureCanvasAgg(self.ax.get_figure()) |
| 119 |
else: |
| 120 |
self.canvas = FigureCanvasTkAggRedraw(self,parent) |
| 121 |
tk_widget = self.canvas.get_tk_widget() |
| 122 |
self.quit = tk_widget.quit |
| 123 |
self.bind = tk_widget.bind |
| 124 |
self.unbind = tk_widget.unbind |
| 125 |
self.winfo_rootx = tk_widget.winfo_rootx |
| 126 |
self.winfo_rooty = tk_widget.winfo_rooty |
| 127 |
self.ax.set_autoscale_on(0) |
| 128 |
self.postscript = self.canvas.print_figure |
| 129 |
self.savefig = self.ax.get_figure().savefig |
| 130 |
|
| 131 |
self.redrawlabels = 0 |
| 132 |
|
| 133 |
callback = self.__optionCallback |
| 134 |
grapher.BasicGrapher.__init__(self,parent,callback,**kw) |
| 135 |
optionDefaults={} |
| 136 |
optionDefaults["xticks"] = (None,callback) |
| 137 |
optionDefaults["yticks"] = (None,callback) |
| 138 |
optionDefaults["zticks"] = (None,callback) |
| 139 |
optionDefaults["background"] = ("white",callback) |
| 140 |
optionAliases = {} |
| 141 |
optionAliases["bg"] = "background" |
| 142 |
|
| 143 |
self.addOptions(**optionDefaults) |
| 144 |
self.addAliases(**optionAliases) |
| 145 |
|
| 146 |
for key in ["grid","decorations","xlabel","ylabel","zlabel", |
| 147 |
"xlabel_fontsize", "ylabel_fontsize", "zlabel_fontsize", |
| 148 |
"minx","maxx","miny","maxy","minz","maxz", "width", |
| 149 |
"height", "left_margin", "right_margin", "top_margin", |
| 150 |
"bottom_margin"]: |
| 151 |
self.__optionCallback(key,self.cget(key),[]) |
| 152 |
matplotlib.rcParams["axes.edgecolor"]=self.cget("foreground") |
| 153 |
|
| 154 |
def pack(self,**kw): |
| 155 |
self.canvas.get_tk_widget().pack(kw) |
| 156 |
|
| 157 |
def update(self): |
| 158 |
if isinstance(self.canvas,FigureCanvasTkAggRedraw): |
| 159 |
self.canvas.get_tk_widget().update() |
| 160 |
FigureCanvasTkAgg.draw(self.canvas) |
| 161 |
else: |
| 162 |
self.canvas.draw() |
| 163 |
|
| 164 |
def __optionCallback(self,key,value,options): |
| 165 |
if key in ["minx","maxx","miny","maxy","minz","maxz", |
| 166 |
"realwidth","realheight","azimuth","elevation", |
| 167 |
"left_margin","right_margin","top_margin","bottom_margin"]: |
| 168 |
self.redrawlabels = 1 |
| 169 |
if key[:3] in ["min", "max"]: |
| 170 |
minc = self.cget("min"+key[3]) |
| 171 |
maxc = self.cget("max"+key[3]) |
| 172 |
if minc < maxc: |
| 173 |
func = None |
| 174 |
if self.ax is self.ax3d: |
| 175 |
func = getattr(self.ax, "set_"+key[3]+"lim3d") |
| 176 |
self._cur_lims = ( |
| 177 |
Axes.get_xlim(self.ax), Axes.get_ylim(self.ax)) |
| 178 |
elif key[3] != 'z': |
| 179 |
func = getattr(self.ax, "set_"+key[3]+"lim") |
| 180 |
if func is not None: |
| 181 |
func(minc,maxc) |
| 182 |
tickskey = key[3]+"ticks" |
| 183 |
ticksval = self.cget(tickskey) |
| 184 |
if ticksval is not None: |
| 185 |
self.__optionCallback(tickskey,ticksval,options) |
| 186 |
elif key == "realwidth": |
| 187 |
lm = float(self.cget("left_margin")) |
| 188 |
rm = float(self.cget("right_margin")) |
| 189 |
self.ax.get_figure().subplots_adjust(left=lm/value, |
| 190 |
right=1-rm/value) |
| 191 |
elif key == "realheight": |
| 192 |
tm = float(self.cget("top_margin")) |
| 193 |
bm = float(self.cget("bottom_margin")) |
| 194 |
top = 1-tm/value |
| 195 |
bottom = bm/value |
| 196 |
if top > bottom: |
| 197 |
self.ax.get_figure().subplots_adjust(top=top, bottom=bottom) |
| 198 |
elif key == "left_margin": |
| 199 |
fig = self.ax.get_figure() |
| 200 |
width = fig.get_figwidth()*fig.get_dpi() |
| 201 |
fig.subplots_adjust(left=value/width) |
| 202 |
elif key == "right_margin": |
| 203 |
fig = self.ax.get_figure() |
| 204 |
width = fig.get_figwidth()*fig.get_dpi() |
| 205 |
fig.subplots_adjust(right=1-value/width) |
| 206 |
elif key == "top_margin": |
| 207 |
fig = self.ax.get_figure() |
| 208 |
height = fig.get_figheight()*fig.get_dpi() |
| 209 |
fig.subplots_adjust(top=1-value/height) |
| 210 |
elif key == "bottom_margin": |
| 211 |
fig = self.ax.get_figure() |
| 212 |
height = fig.get_figheight()*fig.get_dpi() |
| 213 |
fig.subplots_adjust(bottom=value/height) |
| 214 |
elif self.ax is self.ax3d: |
| 215 |
elev = self.cget("elevation") |
| 216 |
azim = self.cget("azimuth") |
| 217 |
if elev is not None or azim is not None: |
| 218 |
self.ax.view_init(elev, azim) |
| 219 |
elif key == "grid": |
| 220 |
if value in ["yes", True]: |
| 221 |
self.ax.grid(color = self.cget("foreground")) |
| 222 |
else: |
| 223 |
self.ax.grid(False) |
| 224 |
elif key in ["width", "height"]: |
| 225 |
if isinstance(self.canvas,FigureCanvasTkAggRedraw): |
| 226 |
self.canvas.get_tk_widget()[key] = value |
| 227 |
else: |
| 228 |
fig = self.ax.get_figure() |
| 229 |
if key == "width": |
| 230 |
fig.set_figwidth(float(value)/fig.get_dpi()) |
| 231 |
self._configNoDraw(realwidth=value) |
| 232 |
else: |
| 233 |
fig.set_figheight(float(value)/fig.get_dpi()) |
| 234 |
self._configNoDraw(realheight=value) |
| 235 |
elif key == "top_title": |
| 236 |
fontsize = self.cget("top_title_fontsize") |
| 237 |
if fontsize is None: |
| 238 |
self.ax.set_title(value) |
| 239 |
else: |
| 240 |
self.ax.set_title(value,fontsize=fontsize) |
| 241 |
elif key == "top_title_fontsize": |
| 242 |
title = self.cget("top_title") |
| 243 |
if title is not None: |
| 244 |
self.ax.set_title(title,fontsize=value) |
| 245 |
elif key in ["background", "bg"]: |
| 246 |
if hasattr(self.ax, 'set_facecolor'): |
| 247 |
|
| 248 |
self.ax.set_facecolor(value) |
| 249 |
else: |
| 250 |
self.ax.set_axis_bgcolor(value) |
| 251 |
elif key in ["foreground", "fg"]: |
| 252 |
matplotlib.rcParams["axes.edgecolor"]=self.cget("foreground") |
| 253 |
self.redrawlabels = 1 |
| 254 |
if self.cget("grid") in ["yes", True]: |
| 255 |
self.ax.grid(color=value) |
| 256 |
elif key == "color_list": |
| 257 |
color_list = value.split() |
| 258 |
i = 0 |
| 259 |
for d in self.data: |
| 260 |
if d["newsect"] is None or d["newsect"]: |
| 261 |
i = i+1 |
| 262 |
if d["color"] is None: |
| 263 |
color = i |
| 264 |
else: |
| 265 |
color = d["color"] |
| 266 |
d["mpline"].set_color(color_list[color%len(color_list)]) |
| 267 |
elif key == "decorations": |
| 268 |
if value: |
| 269 |
self.ax.set_axis_on() |
| 270 |
else: |
| 271 |
self.ax.set_axis_off() |
| 272 |
elif key == "use_symbols": |
| 273 |
self.plotsymbols() |
| 274 |
elif key == "use_labels": |
| 275 |
self.plotlabels() |
| 276 |
elif key in ["xlabel","ylabel","zlabel"]: |
| 277 |
if value is None: |
| 278 |
value = "" |
| 279 |
fontsize = self.cget(key+"_fontsize") |
| 280 |
if hasattr(self.ax,"set_"+key): |
| 281 |
func = getattr(self.ax,"set_"+key) |
| 282 |
if fontsize is None: |
| 283 |
func(value) |
| 284 |
else: |
| 285 |
func(value,fontsize=fontsize) |
| 286 |
elif key in ["xlabel_fontsize","ylabel_fontsize","zlabel_fontsize"]: |
| 287 |
label = self.cget(key[:6]) |
| 288 |
if hasattr(self.ax,"set_"+key[:6]): |
| 289 |
func = getattr(self.ax,"set_"+key[:6]) |
| 290 |
if value is None: |
| 291 |
func(label) |
| 292 |
else: |
| 293 |
func(label,fontsize=value) |
| 294 |
elif key in ["xticks","yticks","zticks"]: |
| 295 |
if value is None: |
| 296 |
if self.ax is self.ax3d: |
| 297 |
axis = getattr(self.ax, "w_"+key[0]+"axis") |
| 298 |
axis.set_major_locator(AutoLocator()) |
| 299 |
elif key == "xticks": |
| 300 |
self.ax.set_xscale('linear') |
| 301 |
else: |
| 302 |
self.ax.set_yscale('linear') |
| 303 |
else: |
| 304 |
min = self.cget("min"+key[0]) |
| 305 |
max = self.cget("max"+key[0]) |
| 306 |
ticks = [min + ((max - min) * i) / float(value-1) |
| 307 |
for i in range(value)] |
| 308 |
if self.ax is self.ax3d: |
| 309 |
axis = getattr(self.ax, "w_"+key[0]+"axis") |
| 310 |
axis.set_major_locator( FixedLocator(ticks) ) |
| 311 |
elif key == "xticks": |
| 312 |
self.ax.set_xticks(ticks) |
| 313 |
elif key == "yticks": |
| 314 |
self.ax.set_yticks(ticks) |
| 315 |
|
| 316 |
def _delAllData(self): |
| 317 |
for d in self.data: |
| 318 |
if "mpline" in d: |
| 319 |
self.ax.lines.remove(d["mpline"]) |
| 320 |
self.data=[] |
| 321 |
|
| 322 |
|
| 323 |
try: |
| 324 |
zcolumn = self.cget(self.cget("type")+"_z") |
| 325 |
except Tkinter.TclError: |
| 326 |
return |
| 327 |
oldax = self.ax |
| 328 |
if zcolumn is None or Axes3D is None: |
| 329 |
if zcolumn is not None: |
| 330 |
self._configNoDraw({self.cget("type")+"_z":None}) |
| 331 |
print("\nmatplotlib 0.98.x does not support 3D plots.") |
| 332 |
print("Plotting only the first two coordinates.") |
| 333 |
if self.ax is self.ax3d: |
| 334 |
|
| 335 |
new_zoom_mode = self.zoom_mode |
| 336 |
self.ax = self.ax2d |
| 337 |
else: |
| 338 |
if self.ax3d is None: |
| 339 |
self.ax3d = Axes3D(self.ax.get_figure()) |
| 340 |
try: |
| 341 |
self.ax3d.set_autoscale_on(0) |
| 342 |
except TypeError: |
| 343 |
self.ax2d.set_autoscale_on(0) |
| 344 |
self.ax3d.set_autoscalez_on(0) |
| 345 |
if self.ax is self.ax2d: |
| 346 |
|
| 347 |
if isinstance(self.canvas,FigureCanvasTkAggRedraw): |
| 348 |
self.zoom_mode = self.toolbar.mode |
| 349 |
new_zoom_mode = '' |
| 350 |
self.ax = self.ax3d |
| 351 |
if self.ax is not oldax: |
| 352 |
if (isinstance(self.canvas,FigureCanvasTkAggRedraw) and |
| 353 |
new_zoom_mode != self.toolbar.mode): |
| 354 |
if "rect" in new_zoom_mode: |
| 355 |
self.toolbar.zoom() |
| 356 |
elif "pan" in new_zoom_mode: |
| 357 |
self.toolbar.pan() |
| 358 |
elif "rect" in self.toolbar.mode: |
| 359 |
self.toolbar.zoom() |
| 360 |
elif "pan" in self.toolbar.mode: |
| 361 |
self.toolbar.pan() |
| 362 |
|
| 363 |
for key in ("grid","decorations","xlabel","ylabel","zlabel", |
| 364 |
"xticks","yticks","zticks", |
| 365 |
"minx","maxx","miny","maxy","minz","maxz", |
| 366 |
"azimuth", "elevation", |
| 367 |
"top_title", "background"): |
| 368 |
self.__optionCallback(key,self.cget(key),[]) |
| 369 |
|
| 370 |
def _delData(self,index): |
| 371 |
if "mpline" in data[index]: |
| 372 |
self.ax.lines.remove(data[index]["mpline"]) |
| 373 |
del self.data[index] |
| 374 |
|
| 375 |
def clear(self): |
| 376 |
if len(self.ax.get_figure().axes) > 0: |
| 377 |
self.ax.get_figure().delaxes(self.ax.get_figure().axes[0]) |
| 378 |
|
| 379 |
def draw(self): |
| 380 |
if self.redrawlabels: |
| 381 |
self.plotlabels() |
| 382 |
if len(self.ax.get_figure().axes) == 0: |
| 383 |
self.ax.get_figure().add_axes(self.ax) |
| 384 |
if isinstance(self.canvas,FigureCanvasTkAggRedraw): |
| 385 |
FigureCanvasTkAgg.draw(self.canvas) |
| 386 |
else: |
| 387 |
self.canvas.draw() |
| 388 |
|
| 389 |
def plot(self): |
| 390 |
color_list = self.cget("color_list").split() |
| 391 |
|
| 392 |
|
| 393 |
line_width=self.cget("line_width") |
| 394 |
dashes=list(map(float,self.cget("dashes"))) |
| 395 |
i=-1 |
| 396 |
for d in self.data: |
| 397 |
if d["newsect"] is None or d["newsect"]: |
| 398 |
i = i+1 |
| 399 |
curve="curve:%d"%(i,) |
| 400 |
if self.ax is self.ax2d: |
| 401 |
v = [d["x"],d["y"]] |
| 402 |
else: |
| 403 |
v = [d["x"],d["y"],d["z"]] |
| 404 |
if d["color"] is None: |
| 405 |
color = i |
| 406 |
else: |
| 407 |
color = d["color"] |
| 408 |
kw = {'color':color_list[color%len(color_list)]} |
| 409 |
if len(v[0]) == 1: |
| 410 |
|
| 411 |
if self.cget("type") == "solution": |
| 412 |
marker = 'o' |
| 413 |
else: |
| 414 |
marker = ',' |
| 415 |
v.append(marker) |
| 416 |
|
| 417 |
else: |
| 418 |
stable = d["stable"] |
| 419 |
|
| 420 |
kw['lw'] = line_width |
| 421 |
if stable is not None and not stable: |
| 422 |
kw.update({'ls':'--','dashes':dashes}) |
| 423 |
if self.ax is self.ax2d: |
| 424 |
self.ax.plot(*v,**kw) |
| 425 |
else: |
| 426 |
self.ax.plot3D(*v,**kw) |
| 427 |
d["mpline"] = self.ax.lines[-1] |
| 428 |
if len(self.ax.get_figure().axes) == 0: |
| 429 |
self.ax.get_figure().add_axes(self.ax) |
| 430 |
|
| 431 |
def __setitem__(self,key,value): |
| 432 |
self.configure(**{key: value}) |
| 433 |
|
| 434 |
def __getitem__(self,key): |
| 435 |
return self.cget(key) |
| 436 |
|
| 437 |
class LabeledGrapher(BasicGrapher,grapher.LabeledGrapher): |
| 438 |
def __init__(self,parent=None,**kw): |
| 439 |
self.labels=[] |
| 440 |
BasicGrapher.__init__(self,parent,**kw) |
| 441 |
|
| 442 |
def _delData(self,i): |
| 443 |
del self.labels[i] |
| 444 |
BasicGrapher._delData(self,i) |
| 445 |
|
| 446 |
def _delAllData(self): |
| 447 |
for l in self.labels: |
| 448 |
for label in l: |
| 449 |
if "mpline" in label: |
| 450 |
self.ax.lines.remove(label["mpline"]) |
| 451 |
if "mptext" in label: |
| 452 |
self.ax.texts.remove(label["mptext"]) |
| 453 |
if "mpsymline" in label: |
| 454 |
self.ax.lines.remove(label["mpsymline"]) |
| 455 |
if "mpsymtext" in label: |
| 456 |
self.ax.texts.remove(label["mpsymtext"]) |
| 457 |
self.labels=[] |
| 458 |
BasicGrapher._delAllData(self) |
| 459 |
|
| 460 |
def _addData(self,data,newsect=None,color=None,stable=None): |
| 461 |
self.labels.append([]) |
| 462 |
BasicGrapher._addData(self,data,newsect,color,stable) |
| 463 |
|
| 464 |
def plotlabels(self): |
| 465 |
if self.cget("realwidth") == 1 or self.cget("realheight") == 1: |
| 466 |
return |
| 467 |
self.redrawlabels = 0 |
| 468 |
|
| 469 |
for labels in self.labels: |
| 470 |
for label in labels: |
| 471 |
if "mpline" in label: |
| 472 |
self.ax.lines.remove(label["mpline"]) |
| 473 |
del label["mpline"] |
| 474 |
if "mptext" in label: |
| 475 |
self.ax.texts.remove(label["mptext"]) |
| 476 |
del label["mptext"] |
| 477 |
|
| 478 |
if not self.cget("use_labels"): |
| 479 |
return |
| 480 |
|
| 481 |
if hasattr(self.ax.transData,'transform'): |
| 482 |
trans = self.ax.transData.transform |
| 483 |
if self.ax is self.ax2d: |
| 484 |
def transseq(x,y): |
| 485 |
return trans(transpose([x,y])) |
| 486 |
else: |
| 487 |
|
| 488 |
|
| 489 |
self.ax.M = self.ax.get_proj() |
| 490 |
def transseq(x,y,z): |
| 491 |
return trans(transpose(proj_transform(x,y,z,self.ax.M)[:2])) |
| 492 |
inv_trans = self.ax.transData.inverted().transform |
| 493 |
else: |
| 494 |
trans = self.ax.transData.xy_tup |
| 495 |
if self.ax is self.ax2d: |
| 496 |
def transseq(x,y): |
| 497 |
return transpose(self.ax.transData.numerix_x_y(x,y)) |
| 498 |
else: |
| 499 |
self.ax.M = self.ax.get_proj() |
| 500 |
def transseq(x,y,z): |
| 501 |
return transpose(self.ax.transData.numerix_x_y( |
| 502 |
*(proj_transform(x,y,z,self.ax.M)[:2]))) |
| 503 |
inv_trans = self.ax.transData.inverse_xy_tup |
| 504 |
if self.cget("smart_label"): |
| 505 |
mp = self.inarrs() |
| 506 |
sp1 = self.cget("left_margin") |
| 507 |
sp2 = 5 |
| 508 |
sp3 = self.cget("bottom_margin") |
| 509 |
sp4 = 5 |
| 510 |
for d in self.data: |
| 511 |
if self.ax is self.ax2d: |
| 512 |
seq = transseq(d["x"],d["y"]) |
| 513 |
else: |
| 514 |
seq = transseq(d["x"],d["y"],d["z"]) |
| 515 |
seq[:,0] = (seq[:,0] - sp1) / sp2 |
| 516 |
seq[:,1] = (seq[:,1] - sp3) / sp4 |
| 517 |
self.map_curve(mp,seq) |
| 518 |
|
| 519 |
for i, labels in enumerate(self.labels): |
| 520 |
for label in labels: |
| 521 |
if len(label["text"]) == 0: |
| 522 |
continue |
| 523 |
[x,y] = label["xy"][:2] |
| 524 |
if self.ax is self.ax3d: |
| 525 |
|
| 526 |
|
| 527 |
z = label["xy"][2] |
| 528 |
[x,y,z] = proj_transform(x, y, z, self.ax.M) |
| 529 |
lim = Axes.get_xlim(self.ax) |
| 530 |
if x < lim[0] or x > lim[1]: |
| 531 |
continue |
| 532 |
lim = Axes.get_ylim(self.ax) |
| 533 |
if y < lim[0] or y > lim[1]: |
| 534 |
continue |
| 535 |
data = trans((x,y)) |
| 536 |
if data is not None: |
| 537 |
[x,y] = data |
| 538 |
if self.cget("smart_label"): |
| 539 |
[xoffd1,yoffd1,xoffd2,yoffd2, |
| 540 |
xofft,yofft,pos] = self.findsp(x,y,mp) |
| 541 |
else: |
| 542 |
[xoffd1,yoffd1,xoffd2,yoffd2, |
| 543 |
xofft,yofft,pos] = self.dumblabel(i,label["j"],x,y) |
| 544 |
[ha,va] = self.getpos(pos) |
| 545 |
[xd1,yd1] = inv_trans((x+xoffd1,y+yoffd1)) |
| 546 |
[xd2,yd2] = inv_trans((x+xoffd2,y+yoffd2)) |
| 547 |
[xt,yt] = inv_trans((x+xofft,y+yofft)) |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
line = Line2D([xd1,xd2],[yd1,yd2],linewidth=0.5, |
| 553 |
color=self.cget("foreground")) |
| 554 |
self.ax.add_line(line) |
| 555 |
self.ax.annotate(label["text"],(xt,yt),ha=ha,va=va, |
| 556 |
color=self.cget("foreground"),clip_on=True) |
| 557 |
label["mpline"] = self.ax.lines[-1] |
| 558 |
label["mptext"] = self.ax.texts[-1] |
| 559 |
|
| 560 |
def getpos(self,pos): |
| 561 |
has = [ "left", "center", "right", "right", "right", "center", |
| 562 |
"left", "left", "left" ] |
| 563 |
vas = [ "bottom","bottom","bottom","center", "top", "top", |
| 564 |
"top", "center", "bottom" ] |
| 565 |
ha = has[pos] |
| 566 |
va = vas[pos] |
| 567 |
return [ha,va] |
| 568 |
|
| 569 |
def plot(self): |
| 570 |
self.plotlabels() |
| 571 |
BasicGrapher.plot(self) |
| 572 |
self.plotsymbols() |
| 573 |
|
| 574 |
def plotsymbols(self): |
| 575 |
for labels in self.labels: |
| 576 |
for label in labels: |
| 577 |
v = label["xy"] |
| 578 |
if Axes3D is None: |
| 579 |
v = v[:2] |
| 580 |
l = label["symbol"] |
| 581 |
if l is None: |
| 582 |
continue |
| 583 |
if "mpsymline" in label: |
| 584 |
self.ax.lines.remove(label["mpsymline"]) |
| 585 |
del label["mpsymline"] |
| 586 |
if "mpsymtext" in label: |
| 587 |
self.ax.texts.remove(label["mpsymtext"]) |
| 588 |
del label["mpsymtext"] |
| 589 |
if not self.cget("use_symbols"): |
| 590 |
continue |
| 591 |
c=self.cget("symbol_color") |
| 592 |
if len(l) <= 3: |
| 593 |
|
| 594 |
kw = {'ha':"center",'va':"center",'color':c,'clip_on':True} |
| 595 |
if self.ax is self.ax2d: |
| 596 |
self.ax.text(*(v+[l]),**kw) |
| 597 |
else: |
| 598 |
t = self.ax.text3D(*(v+[l])) |
| 599 |
t.set_horizontalalignment(kw['ha']) |
| 600 |
t.set_verticalalignment(kw['va']) |
| 601 |
t.set_color(kw['color']) |
| 602 |
t.set_clip_on(True) |
| 603 |
label["mpsymtext"] = self.ax.texts[-1] |
| 604 |
continue |
| 605 |
v = [[coord] for coord in v] |
| 606 |
markerdict = {"fillcircle" : "o", "circle": "o", |
| 607 |
"square": "s", "crossquare": "x", |
| 608 |
"fillsquare": "s", "diamond": "D", |
| 609 |
"filldiamond": "D", "triangle": "^", |
| 610 |
"doubletriangle": "^"} |
| 611 |
ms = None |
| 612 |
for i, ch in enumerate(l): |
| 613 |
if ch in string.digits: |
| 614 |
ms = int(l[i:]) |
| 615 |
l = l[:i] |
| 616 |
break |
| 617 |
if l not in markerdict: |
| 618 |
continue |
| 619 |
v.append(markerdict[l]) |
| 620 |
kw = {'color': c} |
| 621 |
if l in ["circle", "square", "diamond", "triangle"]: |
| 622 |
if hasattr(self.ax, 'get_facecolor'): |
| 623 |
|
| 624 |
kw['mfc'] = self.ax.get_facecolor() |
| 625 |
else: |
| 626 |
kw['mfc'] = self.ax.get_axis_bgcolor() |
| 627 |
if l in ["diamond","filldiamond","triangle","doubletriangle"]: |
| 628 |
kw['ms'] = 8 |
| 629 |
if ms is not None: |
| 630 |
kw['ms'] = ms |
| 631 |
if self.ax is self.ax2d: |
| 632 |
self.ax.plot(*v,**kw) |
| 633 |
else: |
| 634 |
self.ax.plot3D(*v,**kw) |
| 635 |
label["mpsymline"] = self.ax.lines[-1] |
| 636 |
|
| 637 |
|
| 638 |
class InteractiveGrapher(LabeledGrapher,grapher.InteractiveGrapher): |
| 639 |
def __init__(self,parent=None,**kw): |
| 640 |
LabeledGrapher.__init__(self,parent,**kw) |
| 641 |
|
| 642 |
class GUIGrapher(InteractiveGrapher,grapher.GUIGrapher): |
| 643 |
def __init__(self,parent=None,**kw): |
| 644 |
InteractiveGrapher.__init__(self,parent,**kw) |
| 645 |
|
| 646 |
if kw.get("hide"): |
| 647 |
return |
| 648 |
self.menu=Tkinter.Menu() |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
self.menu.add_command(label="Unzoom",command=self.unzoom) |
| 654 |
self.menu.add_command(label="Save",command=self.generatePostscript) |
| 655 |
self.menu.add_command(label="Configure...",command=self.__interactiveConfigureDialog) |
| 656 |
|
| 657 |
def __interactiveConfigureDialog(self): |
| 658 |
widget=self.canvas.get_tk_widget() |
| 659 |
diag = Pmw.Dialog(widget,buttons=("Ok","Cancel")) |
| 660 |
options = sorted([self.configure(key)[0] for key in self.configure() |
| 661 |
if self._isInternalOption(key)]) |
| 662 |
|
| 663 |
self.optionList = Pmw.ScrolledListBox(diag.interior(), |
| 664 |
items=options, |
| 665 |
dblclickcommand=self.__updateInteractiveConfigureDialog) |
| 666 |
self.optionList.pack(side="left") |
| 667 |
|
| 668 |
frame = Tkinter.Frame(diag.interior()) |
| 669 |
frame.pack(side="right") |
| 670 |
self.optionLabel = Pmw.EntryField(frame, |
| 671 |
labelpos="w", |
| 672 |
label_text="Option Name", |
| 673 |
entry_state=Tkinter.DISABLED) |
| 674 |
self.optionLabel.pack(side="top") |
| 675 |
|
| 676 |
self.valueLabel = Pmw.EntryField(frame, |
| 677 |
labelpos="w", |
| 678 |
label_text="Old Value", |
| 679 |
entry_state=Tkinter.DISABLED) |
| 680 |
self.valueLabel.pack(side="top") |
| 681 |
|
| 682 |
self.valueEntry = Pmw.EntryField(frame, |
| 683 |
labelpos="w", |
| 684 |
label_text="New Value", |
| 685 |
command=self.__modifyOption) |
| 686 |
self.valueEntry.pack(side="top") |
| 687 |
|
| 688 |
def generatePostscript(self,filename=None,pscolormode=None): |
| 689 |
if pscolormode is None: |
| 690 |
pscolormode=self.cget("ps_colormode") |
| 691 |
if filename is None: |
| 692 |
filename = tkFileDialog.asksaveasfilename(defaultextension=".eps",title="Save the figure") |
| 693 |
self.update() |
| 694 |
|
| 695 |
self.postscript(filename) |
| 696 |
|
| 697 |
def test(): |
| 698 |
grapher.test(GUIGrapher()) |
| 699 |
|
| 700 |
if __name__=='__main__': |
| 701 |
test() |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|