Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyplot tab character

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?

like image 501
user1220022 Avatar asked Dec 20 '12 03:12

user1220022


People also ask

What is the tab character in Python?

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.

How do you print a tab character in Python?

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.

How do you use the Tab key in Python?

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.

How do you print values separated by tab space in Python?

You can directly use the escape sequence “ \t ” tab character to print a list tab-separated in Python.


1 Answers

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()
like image 185
mmgp Avatar answered Sep 17 '22 14:09

mmgp