get aspect ratio of axes

前端 未结 4 1675
[愿得一人]
[愿得一人] 2020-12-06 11:49

Is there an easy and reliable way to determine the current aspect ratio of an axes when its aspect is set to \'auto\'?

The obvious thing to

4条回答
  •  一向
    一向 (楼主)
    2020-12-06 12:37

    The best thing I can find is this:

    def get_aspect(ax=None):
        if ax is None:
            ax = plt.gca()
        fig = ax.figure
    
        ll, ur = ax.get_position() * fig.get_size_inches()
        width, height = ur - ll
        axes_ratio = height / width
        aspect = axes_ratio / ax.get_data_ratio()
    
        return aspect
    

    But it's surprisingly complicated, and I'm not sure if it is reliable under transforms, etc, as I know nothing about the bbox and transform objects.

提交回复
热议问题