Question about bounding boxes of characters (am I just totally misunderstanding something or is this a bug)


I am trying to understand the bounding boxes of text in matplotlib and how rotation influences them.

See here (code below):

In that image, the top orange box is the bounding box of a period ("."). It has not been rotated. The period is also precisely at x = 0.5 (indicated by the dashed line). The lower bounding boxes have been rotated 90, 180, and 270 degrees. All of the boxes follow the dashed line, but here, the periods position is no longer on the dashed line.

I want all my periods to be on the dashed line, and their surrounding bounding boxes to look like the top one. Basically, I just want precise control over where that character is placed.

My intuition is that I am doing something wrong with the anchoring (tutorial/examples here). However, I have no clue what this wrong would be, and I this issue doesn’t seem to fix itself when I change the horizontal or vertical alignment settings.

I pasted the code that generates that picture, below. Does anyone have an idea?

import matplotlib.pyplot as plt # obviously

def plot_rotation_period(y, rotation, r):
    c0 = plt.text(0.5, y, '.', rotation=rotation, rotation_mode='anchor', va='bottom', fontfamily='monospace',
                 ha='center', fontsize=80)
    bb0 = c0.get_window_extent(renderer=r).transformed(plt.gca().transData.inverted())
    rect0 = plt.Rectangle((bb0.x0, bb0.y0), bb0.width, bb0.height,
                         facecolor="C1", alpha=0.3, zorder=2)

if __name__ == '__main__':
    fig = plt.gcf()
    r = fig.canvas.get_renderer()
    fig.set_size_inches(15, 15)

    plt.xlim(0, 1)
    plt.ylim(0, 1)

    plot_rotation_period(0.8, 0, r)
    plot_rotation_period(0.6, 90, r)
    plot_rotation_period(0.4, 180, r)
    plot_rotation_period(0.2, 270, r)

    plt.plot([0.5, 0.5], [0, 1], linestyle='--')

I think that this is an example of this bug: Center of rotation for text with rotation_mode='anchor' · Issue #13044 · matplotlib/matplotlib · GitHub .

Thanks, I couldn’t tell if that was the same error, but I suppose it is. In either case, I appreciate your reply.