8

It is easy to create a Text object in Matplotlib rotated 90 degrees with rotation='vertical', like this enter image description here

But I want to create Text objects like this enter image description here

How?

Siyuan Ren
  • 7,573
  • 6
  • 47
  • 61
  • I don't think there is such method because what you want is a splitted string. This is one way you can do: `text = '2018-08-11'` and then extract individual characters as `chars = [j for i in text.split('-') for j in i]` and then loop over them and use `plt.text` with a fixed `x` and changing `y` value – Sheldore Oct 16 '18 at 10:14

1 Answers1

17

You can use '\n'.join(my_string) to insert newline characters (\n) between each character of the string (my_string).

If you also want to strip out the - symbols (which is implied in your question), you can use the .replace() function to remove them.

Consider the following:

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

tmdavison
  • 64,360
  • 12
  • 187
  • 165