I am trying to use the tab character in pyplot. However, the tab character is displaying a square, not the tab space I require.
My code is:
fig = pl.figure()
fig.text(.1,.05, "test \t test", bbox=dict(facecolor='red', alpha=0.5))
Does anyone know why this doesn't work?
In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.
The easiest way to print a tab character in Python is to use the short-hand abbreviation '\t' . To see the tab spaced character in the REPL wrap any variable containing a tab character in the built-in print() function.
The action of the tab key depends on the Editor > Keyboard > Personality preference, the file type being edited, and the position within the file. To insert a real tab character, press Ctrl-T.
You can directly use the escape sequence “ \t ” tab character to print a list tab-separated in Python.
I was surprised that pylab.show
actually shows some spaces in place of your square. But savefig
does not try to be smart and gives what you describe. Now, how many spaces does one tab take ? This question makes no sense. Is it 4 for you ? I will pick 4.2 for me. This means you need to decide how many spaces you want a tab to take in your text, i.e.:
fig = pylab.figure()
text = "test \t test".replace("\t", " ")
fig.text(.1,.05, text, bbox=dict(facecolor='red', alpha=0.5))
Or, maybe you want to simply expand your tabs using "standard" conventions:
text = "test \t test".expandtabs()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With