如何在matplotlib中创建垂直的垂直文本?

时间:2018-10-16 09:54:12

标签: python matplotlib text-rendering

rotation='vertical'旋转90度的Matplotlib中创建文本对象很容易,就像这样enter image description here

但是我想创建类似enter image description here

的Text对象

如何?

1 个答案:

答案 0 :(得分:3)

您可以使用'\n'.join(my_string)在字符串(\n)的每个字符之间插入换行符(my_string)。

如果您还想去除-符号(在您的问题中暗示),则可以使用.replace()函数将其删除。

请考虑以下内容:

import matplotlib.pyplot as plt

my_string = '2018-08-11'

fig, ax = plt.subplots(1)

ax.text(0.1, 0.5, my_string, va='center')
ax.text(0.3, 0.5, my_string, rotation=90, va='center')
ax.text(0.5, 0.5, '\n'.join(my_string), va='center')
ax.text(0.7, 0.5, '\n'.join(my_string.replace('-', '')), va='center')

plt.show()

enter image description here