Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create upright vertical oriented text in matplotlib?

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?

like image 959
Siyuan Ren Avatar asked Oct 16 '18 09:10

Siyuan Ren


People also ask

How do I make vertical labels in matplotlib?

In general, to show any text in matplotlib with a vertical orientation, you can add the keyword rotation='vertical' .

How do I align text in matplotlib?

MatPlotLib with PythonPlace legend using legend() method and initialize a method. Iterate the legend. get_texts() method to set the horizontal alignment. To display the figure, use show() method.


1 Answers

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

like image 190
tmdavison Avatar answered Sep 19 '22 00:09

tmdavison