matplotlib源码解析标题实现(窗口标题,标题,子图标题不同之间的差异)
在matplotlib中常用的标题主要三种:窗口标题、图像标题和子图标题。
先通过三个案例简要说明这三类标题的实现。
窗口标题、图像标题,子图标题(仅1个子图)
importmatplotlib.pyplotasplt
plt.rcParams['font.sans-serif']='SimHei'
fig=plt.figure()
plt.plot([1,2])
#设置图像标题
plt.suptitle("这是图像标题")
#设置子图标题
plt.title("这是子图标题")
#获取默认窗口标题
current_title=fig.canvas.manager.window.windowTitle()
print("默认窗口:",current_title)
#设置窗口标题方式一
fig.canvas.set_window_title("这是窗口标题")
#设置窗口标题方式二
fig.canvas.manager.window.setWindowTitle("这是窗口标题")
plt.show()
窗口标题、图像标题、子图标题(多子图)
使用subplot函数实现子图
importmatplotlib.pyplotasplt
plt.rcParams['font.sans-serif']='SimHei'
fig=plt.figure()
plt.subplot(1,2,1)
plt.plot([1,2,3,4],[1,4,9,16],"go")
#设置子图1标题
plt.title("子图1标题")
plt.subplot(122)
plt.plot([1,2,3,4],[1,4,9,16],"r^")#r^表示红色(red)三角
#设置子图2标题
plt.title("子图2标题")
#设置图像标题
plt.suptitle("图像标题")
#设置窗口标题
#fig.canvas.set_window_title("这是窗口标题")
fig.canvas.manager.window.setWindowTitle("这是窗口标题")
plt.show()
使用subplots函数subplots实现子图
importmatplotlib.pyplotasplt
plt.rcParams['font.sans-serif']='SimHei'
fig,ax=plt.subplots(nrows=1,ncols=2,figsize=(6,6))
ax[0].plot([1,2,3,4],[1,4,9,16],"go")
#设置子图1标题
ax[0].set_title("子图1标题")
ax[1].plot([1,2,3,4],[1,4,9,16],"r^")
#设置子图2标题
ax[1].set_title("子图2标题")
#设置图像标题
plt.suptitle("图像标题")
#设置窗口标题
fig.canvas.manager.window.setWindowTitle("这是窗口标题")
plt.show()
原理分析
通过前面三个案例可知:
窗口标题
设置窗口标题可以用两种方法:
一种是调用figure.canvas对象的set_window_title方法,一种是figure.canvas.manager.window对象的setWindowTitle方法。通过下面源码可知,这两种方法其实是等价的。
因此在日常实现过程中,关键是获取当前图像对象(figure),即案例中的fig。该方法只有一个参数,类型为字符串。
可以通过
通过figure.canvas.manager.window对象的windowTitle方法可以获取窗口标题。
classFigureManagerQT(FigureManagerBase): defset_window_title(self,title): self.window.setWindowTitle(title)
图像标题
调用plt.suptitle函数即可。根据源码可知,plt.suptitle函数其实是调用了当前figure对象的suptitle方法。
suptitle函数参数
defsuptitle(self,t,**kwargs):
"""
Addacenteredtitletothefigure.
Parameters
----------
t:str
Thetitletext.
x:float,default0.5
Thexlocationofthetextinfigurecoordinates.
y:float,default0.98
Theylocationofthetextinfigurecoordinates.
horizontalalignment,ha:{'center','left',right'},default:'center'
Thehorizontalalignmentofthetextrelativeto(*x*,*y*).
verticalalignment,va:{'top','center','bottom','baseline'},\
default:'top'
Theverticalalignmentofthetextrelativeto(*x*,*y*).
fontsize,size:default::rc:`figure.titlesize`
Thefontsizeofthetext.See`.Text.set_size`forpossible
values.
fontweight,weight:default::rc:`figure.titleweight`
Thefontweightofthetext.See`.Text.set_weight`forpossible
values.
Returns
-------
text
The`.Text`instanceofthetitle.
OtherParameters
----------------
fontproperties:Noneordict,optional
Adictoffontproperties.If*fontproperties*isgiventhe
defaultvaluesforfontsizeandweightaretakenfromthe
`.FontProperties`defaults.:rc:`figure.titlesize`and
:rc:`figure.titleweight`areignoredinthiscase.
**kwargs
Additionalkwargsare`matplotlib.text.Text`properties.
Examples
--------
>>>fig.suptitle('Thisisthefiguretitle',fontsize=12)
"""
子图标题
- 使用subplot函数:在所在子图中,使用plt.title函数。
- 使用subplots函数:使用子图对象调用set_title方法。
- plt.title函数和axes.set_title方法的参数相同。
注意,在使用subplots函数创建子图时,为什么不能使用plt.title函数设置子图标题呢?
根据title函数的源码可知,title函数其实是通过gca()函数获取子图,然后再调用set_title方法设置标题的。根据实验,在使用subplots函数函数创建多个子图时,plt.gca()只能得到最后一个子图的标题,因此,在某些情况下使用plt.title函数可设置最后一个子图的标题。
plt.title函数和axes.set_title方法源码
deftitle(label,fontdict=None,loc=None,pad=None,*,y=None,**kwargs): returngca().set_title( label,fontdict=fontdict,loc=loc,pad=pad,y=y,**kwargs)
Axes.set_title(self,label,fontdict=None,loc=None,pad=None,*,y=None,**kwargs):
"""
Setatitlefortheaxes.
Setoneofthethreeavailableaxestitles.Theavailabletitles
arepositionedabovetheaxesinthecenter,flushwiththeleft
edge,andflushwiththerightedge.
Parameters
----------
label:str
Texttouseforthetitle
fontdict:dict
Adictionarycontrollingtheappearanceofthetitletext,
thedefault*fontdict*is::
{'fontsize':rcParams['axes.titlesize'],
'fontweight':rcParams['axes.titleweight'],
'color':rcParams['axes.titlecolor'],
'verticalalignment':'baseline',
'horizontalalignment':loc}
loc:{'center','left','right'},default::rc:`axes.titlelocation`
Whichtitletoset.
y:float,default::rc:`axes.titley`
Verticalaxesloationforthetitle(1.0isthetop).If
None(thedefault),yisdeterminedautomaticallytoavoid
decoratorsontheaxes.
pad:float,default::rc:`axes.titlepad`
Theoffsetofthetitlefromthetopoftheaxes,inpoints.
Returns
-------
`.Text`
Thematplotlibtextinstancerepresentingthetitle
OtherParameters
----------------
**kwargs:`.Text`properties
Otherkeywordargumentsaretextproperties,see`.Text`foralist
ofvalidtextproperties.
"""
plt.gca()实验
importmatplotlib.pyplotasplt plt.rcParams['font.sans-serif']='SimHei' fig,ax=plt.subplots(nrows=1,ncols=2,figsize=(6,6)) ax[0].plot([1,2,3,4],[1,4,9,16],"go") ax[1].plot([1,2,3,4],[1,4,9,16],"r^") print(plt.gca()) print(ax[0],ax[1])
结果为
AxesSubplot(0.547727,0.11;0.352273x0.77)
AxesSubplot(0.125,0.11;0.352273x0.77)AxesSubplot(0.547727,0.11;0.352273x0.77)
到此这篇关于matplotlib源码解析标题实现(窗口标题,标题,子图标题不同之间的差异)的文章就介绍到这了,更多相关matplotlib标题内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。