Side-specific padding for matplotlib text bbox

前端 未结 1 425
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-21 16:35

Is it possible to specify the padding on a specific side of the bbox when adding text in matplotlib? I\'m adding a LaTex table as text and for some reason the table is misal

相关标签:
1条回答
  • 2020-12-21 17:10

    I've found a workaround which is based on the answer suggested in this post. An easier solution would also be appreciated. I should also say that the vertical misalignment seems to happen only when I set usetex: True.

    Here's the modified version of the above:

    import matplotlib
    matplotlib.rc('text',usetex=True)
    import matplotlib.pyplot as plt
    from matplotlib.patches import Rectangle
    import numpy as np
    
    text = '\\begin{tabular}{|c|c|}\\hline1&2\\\\\\hline3&4\\\\\\hline\\end{tabular}'
    
    fig, ax = plt.subplots(1)
    
    img = ax.imshow(np.zeros((10,10)), cmap=plt.cm.gray)
    txt = ax.text( 4.5,
              4.5,
              text,
              fontsize=24,
              ha='center',
              va='center',
              bbox=dict(alpha=0))
    
    fig.canvas.draw()
    bbox = txt.get_bbox_patch()
    xmin = bbox.get_window_extent().xmin
    xmax = bbox.get_window_extent().xmax
    ymin = bbox.get_window_extent().ymin
    ymax = bbox.get_window_extent().ymax
    
    xmin, ymin = fig.transFigure.inverted().transform((xmin, ymin))
    xmax, ymax = fig.transFigure.inverted().transform((xmax, ymax))
    
    dx = xmax-xmin
    dy = ymax-ymin
    
    # The bounding box vals can be tweaked manually here.
    rect = Rectangle((xmin-0.02,ymin-0.01), dx+0.04, dy+0.05, fc='w', transform=fig.transFigure)
    
    ax.add_patch(rect)
    fig.canvas.draw()
    ax.axis('off')
    plt.savefig('ok.png',bbox_inches='tight')
    

    This produces:

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