Matplotlib text dimensions

前端 未结 4 1084
青春惊慌失措
青春惊慌失措 2020-11-30 03:08

Is it possible to determine the dimensions of a matplotlib text object? How can I find the width and height in pixels?

Thanks

Edit: I think

4条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 03:48

    Here is a small modification to the accepted answer;

    If you want to get the width and height in axes coordinates, you can use the following:

    from matplotlib import pyplot as plt
    
    fig, ax = plt.subplots()
    r = fig.canvas.get_renderer()
    t = ax.text(0.5, 0.5, 'test')
    
    bb = t.get_window_extent(renderer=r).inverse_transformed(ax.transData)
    width = bb.width
    height = bb.height
    

提交回复
热议问题