Matplotlib表的字体大小

GDi*_*ino 3 python matplotlib

在Python中使用Matplotlib(2.7.9).我必须在子图中绘制一个表(在这种情况下,子图名称是tab)但我似乎找不到改变表的字体大小的方法(http://imgur.com/0Ttvzee-左下角) .安特曼对结果感到高兴,我不是.这是我一直在使用的代码.

编辑:添加完整代码

def stat_chart(self):

DN      = self.diff
ij      = self.ij_list
mcont   = self.mcont
ocont   = self.ocont
ucont   = self.ucont
dist    = self.widths
clon    = '%1.2f' %self.mclon
clat    = '%1.2f' %self.mclat
clonlat = "{0}/{1}".format(clon,clat)
area    = self.area
perim   = self.perimeter
mdist   = np.array(self.widths)
mdist   = mdist[:,0]*10
mdist   = np.mean(mdist)
pstat   = self.polygon_status
if pstat == 1:
  status = "Overestimation"
else:
  status = "Underestimation"

# Setting up the plot (2x2) and subplots
fig   = plt.figure()
gs    = gridspec.GridSpec(2,2,width_ratios=[2,1],height_ratios=[4,1])
main  = plt.subplot(gs[0,0])
polyf = plt.subplot(gs[0,1])
tab   = plt.subplot(gs[1,0])
leg   = plt.subplot(gs[1,1])
tab.set_xticks([])
leg.set_xticks([])
tab.set_yticks([])
leg.set_yticks([])
tab.set_frame_on(False)
leg.set_frame_on(False)

# Main image on the top left
main.imshow(DN[::-1],cmap='winter')
x1,x2,y1,y2 = np.min(ij[:,1])-15,np.max(ij[:,1])+15,np.min(ij[:,0])-15,np.max(ij[:,0])+15
main.axvspan(x1,x2,ymin=1-((y1-320)/float(len(DN)-320)),ymax=1-((y2-320)/float(len(DN)-320)),color='red',alpha=0.3)
main.axis([0,760,0,800])

# Polygon image on the top right
polyf.imshow(DN,cmap='winter')
polyf.axis([x1,x2,y2,y1])
polyf.plot(mcont[:,1],mcont[:,0],'ro',markersize=4)
polyf.plot(ocont[:,1],ocont[:,0],'yo',markersize=4)
polyf.plot(ucont[:,1],ucont[:,0],'go',markersize=4)
for n,en in enumerate(dist):
  polyf.plot([en[2],en[4]],[en[1],en[3]],color='grey',alpha=0.3)

# Legend on the bottom right
mc = mlines.Line2D([],[],color='red',marker='o')
oc = mlines.Line2D([],[],color='yellow',marker='o')
uc = mlines.Line2D([],[],color='green',marker='o')
ed = mlines.Line2D([],[],color='black',alpha=0.5)
pos_p = mpatches.Patch(color='lightgreen')
neg_p = mpatches.Patch(color='royalblue')
leg.legend([mc,oc,uc,ed,pos_p,neg_p],("Model Cont.","Osisaf Cont.","Unknown Cont.","Dist. Mdl to Osi", \
  'Model Overestimate','Model Underestimate'),loc='center')

# Statistics table on the bottom left
stats = [[clonlat+' degrees' ,'%1.4E km^2' %area,'%1.4E km' %perim,'%1.4f km' %mdist,status]]
columns = ('Center Lon/Lat','Area','Perimeter','Mean Width','Status')
rows = ['TODOpolyname']
cwid = [0.1,0.1,0.1,0.1,0.1,0.1]
the_table = tab.table(cellText=stats,colWidths=cwid,rowLabels=rows,colLabels=columns,loc='center')
table_props = the_table.properties()
table_cells = table_props['child_artists']
for cell in table_cells: cell.set_height(0.5)
plt.show()

return
Run Code Online (Sandbox Code Playgroud)

EDIT2:最终(un)解决了绘图文本而不是表格.够好了.

use*_*362 10

我在更改fontsize时遇到了类似的问题.请尝试以下方法

the_table.auto_set_font_size(False)
the_table.set_fontsize(5.5)
Run Code Online (Sandbox Code Playgroud)

为我工作.


mmd*_*ger 2

根据文档table有一个名为 的 kwarg fontsize,它是一个以点为单位的大小的浮点值。

在上面的示例中,对于 5 磅的字体大小,您将使用:

    the_table =tab.table(cellText=stats,colWidths=cwid,rowLabels=rows,colLabels=columns,loc='center',fontsize=5)
Run Code Online (Sandbox Code Playgroud)

如果您需要更好的控制,您可以将FontManager实例传递给方法,如本示例cell.set_text_props()中所述。除了尺寸之外,您还可以设置系列、间距、样式等。

编辑:玩弄 Matplotlib 的示例,似乎仅传递fontsize到表没有效果。然而,导入

    from matplotlib.font_manager import FontProperties
Run Code Online (Sandbox Code Playgroud)

然后循环遍历单元格并运行

    cell.set_text_props(fontproperties=FontProperties(size = 5))
Run Code Online (Sandbox Code Playgroud)

确实达到了预期的效果。目前尚不清楚为什么记录的 kwargfontsize在这种情况下(或显然在您的情况下)不起作用。

  • 试过了。没用。结果是完全一样的。 (2认同)