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

Siy*_*Ren 6 python matplotlib text-rendering

在 Matplotlib 中创建一个旋转 90 度的 Text 对象很容易rotation='vertical',就像这样在此处输入图片说明

但我想创建这样的 Text 对象 在此处输入图片说明

如何?

tmd*_*son 8

您可以使用在字符串 ( ) 的每个字符之间'\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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明