欢迎关注WX公众号:【程序员管小亮】
plt.title()函数用于设置图像标题。
参数:
-
fontsize设置字体大小,默认12,可选参数 [‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’,‘x-large’, ‘xx-large’]
-
fontweight设置字体粗细,可选参数 [‘light’, ‘normal’, ‘medium’, ‘semibold’, ‘bold’, ‘heavy’, ‘black’]
-
fontstyle设置字体类型,可选参数[ ‘normal’ | ‘italic’ | ‘oblique’ ],italic斜体,oblique倾斜
-
verticalalignment设置水平对齐方式 ,可选参数 : ‘center’ , ‘top’ , ‘bottom’ , ‘baseline’
-
horizontalalignment设置垂直对齐方式,可选参数:left,right,center
-
rotation(旋转角度)可选参数为:vertical,horizontal 也可以为数字
-
alpha透明度,参数值0至1之间
-
backgroundcolor标题背景颜色
-
bbox给标题增加外框 ,常用参数如下:
- boxstyle方框外形
- facecolor(简写fc)背景颜色
- edgecolor(简写ec)边框线条颜色
- edgewidth边框线条大小
例子1:
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[3,6,7,9,2]
fig,ax=plt.subplots(1,1)
plt.title('Interesting Graph')
ax.plot(x,y,label='trend')
plt.show()
例子2:
plt.title(‘Interesting Graph’,fontsize=‘xx-large’,fontweight=‘heavy’) 设置字体大小与格式
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[3,6,7,9,2]
fig,ax=plt.subplots(1,1)
plt.title('Interesting Graph',fontsize='xx-large',fontweight='heavy')
ax.plot(x,y,label='trend')
plt.show()
例子3:
plt.title(‘Interesting Graph’,color=‘blue’) 设置字体颜色
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[3,6,7,9,2]
fig,ax=plt.subplots(1,1)
plt.title('Interesting Graph',color='blue')
ax.plot(x,y,label='trend')
plt.show()
例4:
plt.title(‘Interesting Graph’,fontstyle=‘italic’) 设置字体类型
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[3,6,7,9,2]
fig,ax=plt.subplots(1,1)
plt.title('Interesting Graph',fontstyle='italic')
ax.plot(x,y,label='trend')
plt.show()
例5:
plt.title(‘Interesting Graph’,loc =‘left’) 设置字体位置
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[3,6,7,9,2]
fig,ax=plt.subplots(1,1)
plt.title('Interesting Graph',loc ='left')
ax.plot(x,y,label='trend')
plt.show()
例6:
plt.title(‘Interesting Graph’,verticalalignment=‘bottom’) 设置垂直对齐方式
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[3,6,7,9,2]
fig,ax=plt.subplots(1,1)
plt.title('Interesting Graph',verticalalignment='bottom')
ax.plot(x,y,label='trend')
plt.show()
例7:
plt.title(‘Interesting Graph’,rotation=45) 设置字体旋转角度
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[3,6,7,9,2]
plt.title('Interesting Graph',rotation=45)
ax.plot(x,y,label='trend')
plt.show()
例8:
plt.title(‘Interesting’,bbox=dict(facecolor=‘g’, edgecolor=‘blue’, alpha=0.65 )) 标题边框
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[3,6,7,9,2]
plt.title('Interesting',bbox=dict(facecolor='y', edgecolor='blue', alpha=0.65 ))
ax.plot(x,y,label='trend')
plt.show()
python课程推荐。