Underlining Text in Python/Matplotlib

2020-02-01 20:15发布

问题:

I couldn't find another thread or documentation on this topic - has anyone ever been successful underlining in pythons matplotlib package? The syntax I am using is something like this for all the other attributes:

plt.text(0.05, 0.90, 'Parameters: ', fontsize=12)

However, I can't figure out how to underline this text short of actually coding a line into the file.

Thoughts?

回答1:

Matplotlib can use LaTeX to handle all text, see this page of the documnetation for more information. The command for underlining text in LaTeX is simply \underline. From the docstring of one of the example scripts:

You can use TeX to render all of your matplotlib text if the rc parameter text.usetex is set. This works currently on the agg and ps backends, and requires that you have tex and the other dependencies described at http://matplotlib.sf.net/matplotlib.texmanager.html properly installed on your system. The first time you run a script you will see a lot of output from tex and associated tools. The next time, the run may be silent, as a lot of the information is cached in ~/.tex.cache

So as a simple example we can do

import matplotlib.pyplot as plt
from matplotlib import rc

rc('text', usetex=True)

plt.sunplot(111)

plt.text(0.05, 0.90, r'\underline{Parameters}: ', fontsize=12)

to get underlined text.