Python matplotlib,嵌套轴位置变换
我想添加一个大的打印区域(轴),然后添加一个嵌套在内部的小打印区域(轴):Python matplotlib,嵌套轴位置变换,python,matplotlib,Python,Matplotlib,我想添加一个大的打印区域(轴),然后添加一个嵌套在内部的小打印区域(轴): 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],[
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()
这样做对吗?有更好的办法吗?较短的方法?看起来您可能正在尝试关联两个轴的比例/范围。如果是这样,您可以在
轴网格1
中查看插入轴:另一方面,如果您只需要知道“给定屏幕位置x,y,任一/两个轴中的数据坐标位置”,那么这就太过分了。改用ax.transData.inversed()
。看起来您可能正在尝试关联两个轴的比例/范围。如果是这样,您可以在轴网格1
中查看插入轴:另一方面,如果您只需要知道“给定屏幕位置x,y,任一/两个轴中的数据坐标位置”,那么这就太过分了。改用ax.transData.inversed()
。