使用 format() 函数的粗体文本
Bold text using format() function
如何在不导入任何模块的情况下使用 format()
Python 内部函数将文本变为粗体?
然后我将那段文字结合起来并在 ax.text()
函数中引入它(来自 matplotlib library
),所以如果我能在整个 format
函数中做到这一点就太好了。
令我惊讶的是格式化函数没有包含实现它的方法...
编辑答案以包含在 matplotlib 中生成粗体文本的示例,并保留我在 linux 中使用 python:
生成粗体文本的一般答案
您需要使用 fontweight='bold'
作为函数调用中的附加参数。对于下面图表的标题,我增加了字体大小并使用以下内容将文本加粗:plt.title('Info', fontsize=20, fontweight='bold')
import matplotlib
import matplotlib.pyplot as plt
x = [5,2,7]
y = [2,16,4]
plt.plot(x,y)
plt.title('Info', fontsize=20, fontweight='bold')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
如果你只想加粗一个单词,比如在多词标题中,这里有另一种方法:
import matplotlib
import matplotlib.pyplot as plt
x = [5,2,7]
y = [2,16,4]
plt.plot(x,y)
plt.title("This is my " + r"$\bf{" + 'title' + "}$")
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
您会在下面注意到 This is the title
中只有单词 title
以粗体显示:
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
关于使用 PYTHON 在 LINUX 中生成粗体文本的原始答案:
为了在 linux 中生成粗体文本,这里使用和不使用 format 函数来首先说明这个概念:
加粗句子中的一个词无格式功能(linux):
print("The last word in this sentence is",'3[1m' + 'bolded' + '3[0m')
使用 format() 函数将句子中的单词加粗 (linux):
print("The last word in this sentence is {}bolded{}".format('3[1m','3[0m'))
使用 format() 函数将整个句子加粗 (linux):
print("{}This entire sentence is bolded{}".format('3[1m','3[0m'))
Python 字符串没有字体或颜色等属性,因此 format 无法添加它们。
如果您要打印到终端,很有可能可以通过字符串中嵌入的代码来控制终端,正如其他几个答案和评论所提到的那样。
但是,这不适用于 matplotlib 的情况。我不是 matplotlib 专家,但看起来您可以通过 Axes.text() 的可选参数对文本外观施加一些控制,但该信息必须与文本分开传送。
如何在不导入任何模块的情况下使用 format()
Python 内部函数将文本变为粗体?
然后我将那段文字结合起来并在 ax.text()
函数中引入它(来自 matplotlib library
),所以如果我能在整个 format
函数中做到这一点就太好了。
令我惊讶的是格式化函数没有包含实现它的方法...
编辑答案以包含在 matplotlib 中生成粗体文本的示例,并保留我在 linux 中使用 python:
生成粗体文本的一般答案您需要使用 fontweight='bold'
作为函数调用中的附加参数。对于下面图表的标题,我增加了字体大小并使用以下内容将文本加粗:plt.title('Info', fontsize=20, fontweight='bold')
import matplotlib
import matplotlib.pyplot as plt
x = [5,2,7]
y = [2,16,4]
plt.plot(x,y)
plt.title('Info', fontsize=20, fontweight='bold')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
如果你只想加粗一个单词,比如在多词标题中,这里有另一种方法:
import matplotlib
import matplotlib.pyplot as plt
x = [5,2,7]
y = [2,16,4]
plt.plot(x,y)
plt.title("This is my " + r"$\bf{" + 'title' + "}$")
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
您会在下面注意到 This is the title
中只有单词 title
以粗体显示:
_____________________________________________________________ _____________________________________________________________ _____________________________________________________________
关于使用 PYTHON 在 LINUX 中生成粗体文本的原始答案: 为了在 linux 中生成粗体文本,这里使用和不使用 format 函数来首先说明这个概念:
加粗句子中的一个词无格式功能(linux):
print("The last word in this sentence is",'3[1m' + 'bolded' + '3[0m')
使用 format() 函数将句子中的单词加粗 (linux):
print("The last word in this sentence is {}bolded{}".format('3[1m','3[0m'))
使用 format() 函数将整个句子加粗 (linux):
print("{}This entire sentence is bolded{}".format('3[1m','3[0m'))
Python 字符串没有字体或颜色等属性,因此 format 无法添加它们。
如果您要打印到终端,很有可能可以通过字符串中嵌入的代码来控制终端,正如其他几个答案和评论所提到的那样。
但是,这不适用于 matplotlib 的情况。我不是 matplotlib 专家,但看起来您可以通过 Axes.text() 的可选参数对文本外观施加一些控制,但该信息必须与文本分开传送。