默认文本旋转演示

Matplotlib在默认情况下进行文本布局的方式对某些人来说是违反直觉的,因此本示例旨在使其更加清晰。

文本_通过_其_边界_框_ ( _环绕_墨迹_矩形_的_矩形_框_ ) _对齐_ 。_操作顺序是先旋转后对齐。基本上,文本在(x,y)位置居中,围绕该点旋转,然后根据旋转文本的边框对齐。

因此,如果指定“左下对齐”,则旋转文本边框的左下角将位于文本的(x,y)坐标。

但一张照片胜过千言万语!

text rotation
import matplotlib.pyplot as plt
import numpy as np


def addtext(ax, props):
    ax.text(0.5, 0.5, 'text 0', props, rotation=0)
    ax.text(1.5, 0.5, 'text 45', props, rotation=45)
    ax.text(2.5, 0.5, 'text 135', props, rotation=135)
    ax.text(3.5, 0.5, 'text 225', props, rotation=225)
    ax.text(4.5, 0.5, 'text -45', props, rotation=-45)
    for x in range(0, 5):
        ax.scatter(x + 0.5, 0.5, color='r', alpha=0.5)
    ax.set_yticks([0, .5, 1])
    ax.set_xticks(np.arange(0, 5.1, 0.5))
    ax.set_xlim(0, 5)
    ax.grid(True)


# the text bounding box
bbox = {'fc': '0.8', 'pad': 0}

fig, axs = plt.subplots(2, 1, sharex=True)

addtext(axs[0], {'ha': 'center', 'va': 'center', 'bbox': bbox})
axs[0].set_ylabel('center / center')

addtext(axs[1], {'ha': 'left', 'va': 'bottom', 'bbox': bbox})
axs[1].set_ylabel('left / bottom')

plt.show()

关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery