首页 > 解决方案 > 如何在 matplotlib 中创建垂直垂直方向的文本?

问题描述

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

但我想创建这样的文本对象在此处输入图像描述

如何?

标签: pythonmatplotlibtext-rendering

解决方案


您可以使用在字符串 ( ) 的每个字符之间'\n'.join(my_string)插入换行符( )。\nmy_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()

在此处输入图像描述


推荐阅读