/usr/share/pyshared/mpl_toolkits/axes_grid1/parasite_axes.py is in python-matplotlib 1.1.1~rc1+git20120423-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | import warnings
import matplotlib
rcParams = matplotlib.rcParams
import matplotlib.artist as martist
import matplotlib.transforms as mtransforms
import matplotlib.collections as mcoll
import matplotlib.legend as mlegend
from matplotlib.axes import subplot_class_factory
from mpl_axes import Axes
from matplotlib.transforms import Bbox
import numpy as np
import matplotlib.cbook as cbook
is_string_like = cbook.is_string_like
class ParasiteAxesBase:
def get_images_artists(self):
artists = set([a for a in self.get_children() if a.get_visible()])
images = set([a for a in self.images if a.get_visible()])
return list(images), list(artists - images)
def __init__(self, parent_axes, **kargs):
self._parent_axes = parent_axes
kargs.update(dict(frameon=False))
self._get_base_axes_attr("__init__")(self, parent_axes.figure,
parent_axes._position, **kargs)
def cla(self):
self._get_base_axes_attr("cla")(self)
martist.setp(self.get_children(), visible=False)
self._get_lines = self._parent_axes._get_lines
# In mpl's Axes, zorders of x- and y-axis are originally set
# within Axes.draw().
if self._axisbelow:
self.xaxis.set_zorder(0.5)
self.yaxis.set_zorder(0.5)
else:
self.xaxis.set_zorder(2.5)
self.yaxis.set_zorder(2.5)
_parasite_axes_classes = {}
def parasite_axes_class_factory(axes_class=None):
if axes_class is None:
axes_class = Axes
new_class = _parasite_axes_classes.get(axes_class)
if new_class is None:
import new
def _get_base_axes_attr(self, attrname):
return getattr(axes_class, attrname)
new_class = new.classobj("%sParasite" % (axes_class.__name__),
(ParasiteAxesBase, axes_class),
{'_get_base_axes_attr': _get_base_axes_attr})
_parasite_axes_classes[axes_class] = new_class
return new_class
ParasiteAxes = parasite_axes_class_factory()
# #class ParasiteAxes(ParasiteAxesBase, Axes):
# @classmethod
# def _get_base_axes_attr(cls, attrname):
# return getattr(Axes, attrname)
class ParasiteAxesAuxTransBase:
def __init__(self, parent_axes, aux_transform, viewlim_mode=None,
**kwargs):
self.transAux = aux_transform
self.set_viewlim_mode(viewlim_mode)
self._parasite_axes_class.__init__(self, parent_axes, **kwargs)
def _set_lim_and_transforms(self):
self.transAxes = self._parent_axes.transAxes
self.transData = \
self.transAux + \
self._parent_axes.transData
self._xaxis_transform = mtransforms.blended_transform_factory(
self.transData, self.transAxes)
self._yaxis_transform = mtransforms.blended_transform_factory(
self.transAxes, self.transData)
def set_viewlim_mode(self, mode):
if mode not in [None, "equal", "transform"]:
raise ValueError("Unknown mode : %s" % (mode,))
else:
self._viewlim_mode = mode
def get_viewlim_mode(self):
return self._viewlim_mode
def update_viewlim(self):
viewlim = self._parent_axes.viewLim.frozen()
mode = self.get_viewlim_mode()
if mode is None:
pass
elif mode == "equal":
self.axes.viewLim.set(viewlim)
elif mode == "transform":
self.axes.viewLim.set(viewlim.transformed(self.transAux.inverted()))
else:
raise ValueError("Unknown mode : %s" % (self._viewlim_mode,))
def _pcolor(self, method_name, *XYC, **kwargs):
if len(XYC) == 1:
C = XYC[0]
ny, nx = C.shape
gx = np.arange(-0.5, nx, 1.)
gy = np.arange(-0.5, ny, 1.)
X, Y = np.meshgrid(gx, gy)
else:
X, Y, C = XYC
pcolor_routine = self._get_base_axes_attr(method_name)
if kwargs.has_key("transform"):
mesh = pcolor_routine(self, X, Y, C, **kwargs)
else:
orig_shape = X.shape
xy = np.vstack([X.flat, Y.flat])
xyt=xy.transpose()
wxy = self.transAux.transform(xyt)
gx, gy = wxy[:,0].reshape(orig_shape), wxy[:,1].reshape(orig_shape)
mesh = pcolor_routine(self, gx, gy, C, **kwargs)
mesh.set_transform(self._parent_axes.transData)
return mesh
def pcolormesh(self, *XYC, **kwargs):
return self._pcolor("pcolormesh", *XYC, **kwargs)
def pcolor(self, *XYC, **kwargs):
return self._pcolor("pcolor", *XYC, **kwargs)
def _contour(self, method_name, *XYCL, **kwargs):
if len(XYCL) <= 2:
C = XYCL[0]
ny, nx = C.shape
gx = np.arange(0., nx, 1.)
gy = np.arange(0., ny, 1.)
X,Y = np.meshgrid(gx, gy)
CL = XYCL
else:
X, Y = XYCL[:2]
CL = XYCL[2:]
contour_routine = self._get_base_axes_attr(method_name)
if kwargs.has_key("transform"):
cont = contour_routine(self, X, Y, *CL, **kwargs)
else:
orig_shape = X.shape
xy = np.vstack([X.flat, Y.flat])
xyt=xy.transpose()
wxy = self.transAux.transform(xyt)
gx, gy = wxy[:,0].reshape(orig_shape), wxy[:,1].reshape(orig_shape)
cont = contour_routine(self, gx, gy, *CL, **kwargs)
for c in cont.collections:
c.set_transform(self._parent_axes.transData)
return cont
def contour(self, *XYCL, **kwargs):
return self._contour("contour", *XYCL, **kwargs)
def contourf(self, *XYCL, **kwargs):
return self._contour("contourf", *XYCL, **kwargs)
def apply_aspect(self, position=None):
self.update_viewlim()
self._get_base_axes_attr("apply_aspect")(self)
#ParasiteAxes.apply_aspect()
_parasite_axes_auxtrans_classes = {}
def parasite_axes_auxtrans_class_factory(axes_class=None):
if axes_class is None:
parasite_axes_class = ParasiteAxes
elif not issubclass(axes_class, ParasiteAxesBase):
parasite_axes_class = parasite_axes_class_factory(axes_class)
else:
parasite_axes_class = axes_class
new_class = _parasite_axes_auxtrans_classes.get(parasite_axes_class)
if new_class is None:
import new
new_class = new.classobj("%sParasiteAuxTrans" % (parasite_axes_class.__name__),
(ParasiteAxesAuxTransBase, parasite_axes_class),
{'_parasite_axes_class': parasite_axes_class,
'name': 'parasite_axes'})
_parasite_axes_auxtrans_classes[parasite_axes_class] = new_class
return new_class
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(axes_class=ParasiteAxes)
def _get_handles(ax):
handles = ax.lines[:]
handles.extend(ax.patches)
handles.extend([c for c in ax.collections
if isinstance(c, mcoll.LineCollection)])
handles.extend([c for c in ax.collections
if isinstance(c, mcoll.RegularPolyCollection)])
handles.extend([c for c in ax.collections
if isinstance(c, mcoll.CircleCollection)])
return handles
class HostAxesBase:
def __init__(self, *args, **kwargs):
self.parasites = []
self._get_base_axes_attr("__init__")(self, *args, **kwargs)
def get_aux_axes(self, tr, viewlim_mode="equal", axes_class=None):
parasite_axes_class = parasite_axes_auxtrans_class_factory(axes_class)
ax2 = parasite_axes_class(self, tr, viewlim_mode)
# note that ax2.transData == tr + ax1.transData
# Anthing you draw in ax2 will match the ticks and grids of ax1.
self.parasites.append(ax2)
return ax2
def _get_legend_handles(self, legend_handler_map=None):
Axes_get_legend_handles = self._get_base_axes_attr("_get_legend_handles")
all_handles = Axes_get_legend_handles(self, legend_handler_map)
for ax in self.parasites:
all_handles.extend(ax._get_legend_handles(legend_handler_map))
return all_handles
def draw(self, renderer):
orig_artists = list(self.artists)
orig_images = list(self.images)
if hasattr(self, "get_axes_locator"):
locator = self.get_axes_locator()
if locator:
pos = locator(self, renderer)
self.set_position(pos, which="active")
self.apply_aspect(pos)
else:
self.apply_aspect()
else:
self.apply_aspect()
rect = self.get_position()
for ax in self.parasites:
ax.apply_aspect(rect)
images, artists = ax.get_images_artists()
self.images.extend(images)
self.artists.extend(artists)
self._get_base_axes_attr("draw")(self, renderer)
self.artists = orig_artists
self.images = orig_images
def cla(self):
for ax in self.parasites:
ax.cla()
self._get_base_axes_attr("cla")(self)
#super(HostAxes, self).cla()
def twinx(self, axes_class=None):
"""
call signature::
ax2 = ax.twinx()
create a twin of Axes for generating a plot with a sharex
x-axis but independent y axis. The y-axis of self will have
ticks on left and the returned axes will have ticks on the
right
"""
if axes_class is None:
axes_class = self._get_base_axes()
parasite_axes_class = parasite_axes_class_factory(axes_class)
ax2 = parasite_axes_class(self, sharex=self, frameon=False)
self.parasites.append(ax2)
# for normal axes
self.axis["right"].toggle(all=False)
self.axis["right"].line.set_visible(False)
ax2.axis["right"].set_visible(True)
ax2.axis["left","top", "bottom"].toggle(all=False)
ax2.axis["left","top", "bottom"].line.set_visible(False)
ax2.axis["right"].toggle(all=True)
ax2.axis["right"].line.set_visible(True)
return ax2
def twiny(self, axes_class=None):
"""
call signature::
ax2 = ax.twiny()
create a twin of Axes for generating a plot with a shared
y-axis but independent x axis. The x-axis of self will have
ticks on bottom and the returned axes will have ticks on the
top
"""
if axes_class is None:
axes_class = self._get_base_axes()
parasite_axes_class = parasite_axes_class_factory(axes_class)
ax2 = parasite_axes_class(self, sharey=self, frameon=False)
self.parasites.append(ax2)
self.axis["top"].toggle(all=False)
self.axis["top"].line.set_visible(False)
ax2.axis["top"].set_visible(True)
ax2.axis["left","right", "bottom"].toggle(all=False)
ax2.axis["left","right", "bottom"].line.set_visible(False)
ax2.axis["top"].toggle(all=True)
ax2.axis["top"].line.set_visible(True)
return ax2
def twin(self, aux_trans=None, axes_class=None):
"""
call signature::
ax2 = ax.twin()
create a twin of Axes for generating a plot with a sharex
x-axis but independent y axis. The y-axis of self will have
ticks on left and the returned axes will have ticks on the
right
"""
if axes_class is None:
axes_class = self._get_base_axes()
parasite_axes_auxtrans_class = parasite_axes_auxtrans_class_factory(axes_class)
if aux_trans is None:
ax2 = parasite_axes_auxtrans_class(self, mtransforms.IdentityTransform(),
viewlim_mode="equal",
)
else:
ax2 = parasite_axes_auxtrans_class(self, aux_trans,
viewlim_mode="transform",
)
self.parasites.append(ax2)
# for normal axes
#self.yaxis.tick_left()
#self.xaxis.tick_bottom()
#ax2.yaxis.tick_right()
#ax2.xaxis.set_visible(True)
#ax2.yaxis.set_visible(True)
#ax2.yaxis.set_label_position('right')
##ax2.xaxis.tick_top()
#ax2.xaxis.set_label_position('top')
self.axis["top","right"].toggle(all=False)
self.axis["top","right"].line.set_visible(False)
#self.axis["left","bottom"].toggle(label=True)
ax2.axis["top","right"].set_visible(True)
ax2.axis["bottom","left"].toggle(all=False)
ax2.axis["bottom","left"].line.set_visible(False)
ax2.axis["top","right"].toggle(all=True)
ax2.axis["top","right"].line.set_visible(True)
# # for axisline axes
# self._axislines["right"].set_visible(False)
# self._axislines["top"].set_visible(False)
# ax2._axislines["left"].set_visible(False)
# ax2._axislines["bottom"].set_visible(False)
# ax2._axislines["right"].set_visible(True)
# ax2._axislines["top"].set_visible(True)
# ax2._axislines["right"].major_ticklabels.set_visible(True)
# ax2._axislines["top"].major_ticklabels.set_visible(True)
return ax2
def get_tightbbox(self, renderer, call_axes_locator=True):
bbs = [ax.get_tightbbox(renderer, call_axes_locator) \
for ax in self.parasites]
get_tightbbox = self._get_base_axes_attr("get_tightbbox")
bbs.append(get_tightbbox(self, renderer, call_axes_locator))
_bbox = Bbox.union([b for b in bbs if b.width!=0 or b.height!=0])
return _bbox
_host_axes_classes = {}
def host_axes_class_factory(axes_class=None):
if axes_class is None:
axes_class = Axes
new_class = _host_axes_classes.get(axes_class)
if new_class is None:
import new
def _get_base_axes(self):
return axes_class
def _get_base_axes_attr(self, attrname):
return getattr(axes_class, attrname)
new_class = new.classobj("%sHostAxes" % (axes_class.__name__),
(HostAxesBase, axes_class),
{'_get_base_axes_attr': _get_base_axes_attr,
'_get_base_axes': _get_base_axes})
_host_axes_classes[axes_class] = new_class
return new_class
def host_subplot_class_factory(axes_class):
host_axes_class = host_axes_class_factory(axes_class=axes_class)
subplot_host_class = subplot_class_factory(host_axes_class)
return subplot_host_class
HostAxes = host_axes_class_factory(axes_class=Axes)
SubplotHost = subplot_class_factory(HostAxes)
def host_axes(*args, **kwargs):
import matplotlib.pyplot as plt
axes_class = kwargs.pop("axes_class", None)
host_axes_class = host_axes_class_factory(axes_class)
fig = plt.gcf()
ax = host_axes_class(fig, *args, **kwargs)
fig.add_axes(ax)
plt.draw_if_interactive()
return ax
def host_subplot(*args, **kwargs):
import matplotlib.pyplot as plt
axes_class = kwargs.pop("axes_class", None)
host_subplot_class = host_subplot_class_factory(axes_class)
fig = plt.gcf()
ax = host_subplot_class(fig, *args, **kwargs)
fig.add_subplot(ax)
plt.draw_if_interactive()
return ax
|