python - 使用 format() 函数的粗体文本
问题描述
如何在format()
不导入任何模块的情况下使用 Python 内部函数将文本变为粗体?
然后我结合该文本并在ax.text()
函数中引入它(从matplotlib library
),所以如果我能在整个format
函数中做到这一点会非常好。
我很惊讶格式功能不包括这样做的方法......
解决方案
编辑答案以包括在 matplotlib 中生成粗体文本的示例,并保留我关于使用 python 在 linux 中生成粗体文本的一般答案:
您将希望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()
您会在下面注意到,只有单词title
以粗体显示 out of This is the title
:
_____________________________________________________________ _____________________________________________________________ _____________________________________________________________
关于使用 PYTHON 在 LINUX 中生成粗体文本的原始答案:为了在 linux 中生成粗体文本,这里有带和不带 format 函数来首先说明这个概念:
没有格式化功能的句子中的粗体字(linux):
print("The last word in this sentence is",'\033[1m' + 'bolded' + '\033[0m')
使用 format() 函数在句子中加粗一个单词(linux):
print("The last word in this sentence is {}bolded{}".format('\033[1m','\033[0m'))
使用 format() 函数将整个句子加粗(linux):
print("{}This entire sentence is bolded{}".format('\033[1m','\033[0m'))
推荐阅读
- python - Python 加法程序错误 - 语法无效
- angular - 为什么我可以通过修改从选择器接收到的对象来修改状态?
- sql - sql语句中的foxpro是否可以根据条件最大分数和具有不同名称的id填充winners_name列
- python - 如何在python中逐行拆分文本
- c++ - 使用 C++ 在 Qt5 中显示曲线
- mapbox - 我可以将 Azure Maps 磁贴用作 Mapbox 样式 JSON 文件的数据源吗?
- c++ - 编写内核配置时“不是目录”
- javascript - 如何用时间数据制作进度条?
- reactjs - 引入 ALB 后,发生混合内容错误
- asp.net - 如何在一个逗号分隔值中获取列值