数学字体系列#
简单的示例,展示了新的 math_fontfamily
参数,该参数可用于更改图中每个单独文本元素的字体系列。
如果未设置参数,将使用全局值 rcParams["mathtext.fontset"]
(默认值:'dejavusans'
)。
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 5))
# A simple plot for the background.
ax.plot(range(11), color="0.9")
# A text mixing normal text and math text.
msg = (r"Normal Text. $Text\ in\ math\ mode:\ "
r"\int_{0}^{\infty } x^2 dx$")
# Set the text in the plot.
ax.text(1, 7, msg, size=12, math_fontfamily='cm')
# Set another font for the next text.
ax.text(1, 3, msg, size=12, math_fontfamily='dejavuserif')
# *math_fontfamily* can be used in most places where there is text,
# like in the title:
ax.set_title(r"$Title\ in\ math\ mode:\ \int_{0}^{\infty } x^2 dx$",
math_fontfamily='stixsans', size=14)
# Note that the normal text is not changed by *math_fontfamily*.
plt.show()
![../../../_images/77c6f2d77fbc8f150f60cb6ce8f4d5f16780c132b187d13b0d05b22a39cf29f9.png](../../../_images/77c6f2d77fbc8f150f60cb6ce8f4d5f16780c132b187d13b0d05b22a39cf29f9.png)