我想添加一个大的绘图区域(轴),然后,一个小的绘图区域(轴)嵌套在中:
big = BigArea(figure, bbox=Bbox([[0.1, 0.2], [0.9, 0.8]]), xlim=(0, 6), ylim=(1, 5))
small = big.add_area(bbox=Bbox([[1, 2], [3, 4]]))
大区域坐标包含在图限制内:Bbox([[0.0,0.0],[1.0,1.0]])
小区域坐标包含在大区域范围内:Bbox([[0.0,1.0],[6.0,5.0]])
我想用matplotlib转换来处理坐标,但我不确定如何正确地做到这一点。这是我的实施:
import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox, BboxTransformTo
class BigArea:
def __init__(self, figure, bbox, xlim, ylim):
ax = figure.add_axes(bbox)
ax.set_xlim(xlim)
ax.set_ylim(ylim)
self._figure = figure
self._bbox = bbox
self._ax = ax
def add_area(self, bbox):
ax = self._ax
t = ax.transLimits + BboxTransformTo(self._bbox)
bbox2 = bbox.transformed(t)
figure = self._figure
small = figure.add_axes(bbox2)
return small
figure = plt.figure()
big = BigArea(figure, bbox=Bbox([[0.1, 0.2], [0.9, 0.8]]), xlim=(0, 6), ylim=(1, 5))
small = big.add_area(bbox=Bbox([[1, 2], [3, 4]]))
# plt.savefig('nested_axes_position_transformation.png')
plt.show()
这是正确的方法吗?有没有更好的办法 ?一个较短的方式?