Johann Rohwer wrote:
I'm trying to insert a custom set of axes within a subplot, much like
the axes_demo.py. Only difference is, now I want an inset graph
inside each of a number of subplots. Using ASCII art, much like the
following:
|-------------------|
| |
| |
| ----- |
| | | |
| ----- |
|-------------------|
|-------------------|
| |
| |
| ----- |
| | | |
| ----- |
|-------------------|
Where the whole thing would be the figure, containing 2 subplots each
with an inset.
Is there any way to have the rect coordinates of
fig.add_axes(rect)
to refer to the axes coordinates of the respective subplot, and not of
the complete figure? (In reality there are actually 12 subplots, not
only 2....
To answer my own question, after browsing the docstrings I came up with the
following attached minimal script to illustrate a solution to the problem.
However, it appears crufty. Specifically,
1. Can the fig.add_axes() call not take a transform directly as optional
argument, like in fig.add_axes([.4, .1, .5, .3], transform=ax.transAxes)?
This would appear the natural solution but does not work.
2. The transformed bounding box gives coordinates in points, necessitating
the division by figure width and figure height to revert back to fractional
coordinates.
3. Re-calculating [l, b, w, h] from tBbox seems cumbersome. Can the
add_axes() not call a Bbox instance directly?
I'm sure I'm missing something obvious but don't have the time to delve into
the transforms sourcecode, so any pointer is appreciated :-)
Johann
#!/usr/bin/env python
# test script to insert custom subplots within subplots
# jr -- 20090223
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(8,6))
fw = fig.get_figwidth()*fig.get_dpi()
fh = fig.get_figheight()*fig.get_dpi()
x = np.linspace(1,10,100)
y = np.sin(x)
for i in range(4):
ax = fig.add_subplot(2,2,i+1)
ax.plot(x,3+y,'b-')
ax.set_ylim(0,4.5)
Bbox = matplotlib.transforms.Bbox.from_bounds(.4, .1, .5, .3)
tBbox = matplotlib.transforms.TransformedBbox(Bbox, ax.transAxes).get_points()
l, b, w, h = tBbox[0,0]/fw, tBbox[0,1]/fh, (tBbox[1,0]-tBbox[0,0])/fw, (tBbox[1,1]-tBbox[0,1])/fh
axins = fig.add_axes([l, b, w, h])
axins.plot(x,y,'r-')
fig.show()
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users