Python matplotlib 画图 字体、字体大小、字体粗细、文字方向、斜体、旋转角度 全集
warning:
这篇文章距离上次修改已过183天,其中的内容可能已经有所变动。
在Python的matplotlib库中,可以使用matplotlib.font_manager
模块来设置字体的样式,包括字体、大小、粗细以及文字的斜体、旋转角度等属性。
以下是一个示例代码,演示如何设置这些属性:
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 创建一个FontProperties对象来设置字体属性
font = FontProperties()
font.set_family('serif') # 设置字体为serif
font.set_style('italic') # 设置字体样式为斜体
font.set_stretch('condensed') # 设置字体伸缩为condensed(窄体)
font.set_weight('bold') # 设置字体粗细为bold
font.set_size(14) # 设置字体大小
# 绘图
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
# 添加标题、X轴和Y轴标签,并设置字体属性
plt.title('Example Plot', fontproperties=font)
plt.xlabel('X Axis', fontproperties=font)
plt.ylabel('Y Axis', fontproperties=font, rotation=45) # 设置标签旋转角度为45度
# 显示图形
plt.show()
在这个例子中,我们首先创建了一个FontProperties
对象,并通过其方法设置了字体的属性。然后,在绘图时将这个字体属性对象应用到标题、X轴标签和Y轴标签上,包括设置标签旋转的角度。
评论已关闭