Convert matplotlib data units to normalized units

前端 未结 1 758
有刺的猬
有刺的猬 2021-01-02 17:50

Does anyone know how to convert matplotlib data units into normalized units?

The reason that I need it is that I need to create a subplot on top of another plot. An

1条回答
  •  执笔经年
    2021-01-02 18:35

    Here's one way to do it:

    inner axes printed at 0.5, 2.5, 1.0, 0.3 (in outer axes coords)

    inner axes printed at 0.5, 2.5, 1.0, 0.3 (in outer axes coords)

    You basically need two transformations -- one from src-coords to display, and one from display to dest-coord. From the docs there seems to be no direct way:
    http://matplotlib.org/users/transforms_tutorial.html

    bb_data = Bbox.from_bounds(0.5, 2.5, 1.0, 0.3)
    disp_coords = ax.transData.transform(bb_data)
    fig_coords = fig.transFigure.inverted().transform(disp_coords)
    

    ax and fig both carry transformer with them -- to display-coords!
    If you call inverted on them, you get an transformer for the inverse direction.

    Here's the full code for the above example:

    import matplotlib.pyplot as plt
    from matplotlib.transforms import Bbox
    
    plt.plot([0,2], [2,4])
    fig = plt.gcf()
    ax = plt.gca()
    
    bb_data = Bbox.from_bounds(0.5, 2.5, 1.0, 0.3)
    disp_coords = ax.transData.transform(bb_data)
    fig_coords = fig.transFigure.inverted().transform(disp_coords)
    
    fig.add_axes(Bbox(fig_coords))
    
    plt.show()
    

    0 讨论(0)
提交回复
热议问题