【问题标题】:Matplotlib Table's Font SizeMatplotlib 表格字体大小
【发布时间】:2015-08-23 23:27:35
【问题描述】:

在 Python (2.7.9) 中使用 Matplotlib。我必须在子图中绘制一个表格(在这种情况下,子图名称是选项卡),但我似乎找不到更改表格字体大小的方法(http://imgur.com/0Ttvzee - 左下角)。 Antman 对结果很满意,但我不满意。 这是我一直在使用的代码。

编辑:添加完整代码

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

EDIT2:最终(未)解决了绘制文本而不是表格。够了。

【问题讨论】:

  • 如果你做了很多这样的事情,你可能需要考虑将你的数据导出到 shapefile(例如,networkx)并在 QGIS 中创建你的绘图。在管理多个地图和表格时,QGIS 打印作曲家确实让 matplotlib 感到羞耻。
  • 谢谢,但我不是一个优秀的程序员(正如你从可耻的脚本中看到的那样),我的时间不多了。现在学习 QGIS 并转移所有内容将花费太多时间。对于未来,虽然它是一个很好的工具。谢谢。

标签: python matplotlib


【解决方案1】:

我在更改字体大小时遇到​​了类似的问题。试试下面的

the_table.auto_set_font_size(False)
the_table.set_fontsize(5.5)

为我工作。

【讨论】:

  • 尝试此解决方案时出现此错误:AttributeError: 'NoneType' object has no attribute 'set_fontsize'
  • 如果您收到该错误,但在the_table.auto_set_font_size(False) 上没有收到错误,您可能执行了类似the_table = the_table.auto_set_font_size(False) 的操作,因为这确实会将the_table 设置为None。否则,第二行将在NoneType 上引发错误是没有意义的。
【解决方案2】:

根据the docstable 有一个名为fontsize 的kwarg,它是一个浮点值,以磅为单位。

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

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

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

编辑:使用 Matplotlib 的example,似乎只是将fontsize 传递给表没有任何效果。但是,导入

    from matplotlib.font_manager import FontProperties

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

    cell.set_text_props(fontproperties=FontProperties(size = 5))

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

【讨论】:

  • 试过了。没用。结果完全一样。
  • @GDino 两种方法?您使用的是 pyplot.table 还是 ax.add_table ?你能为 MWE 提供进口和一切吗?
  • 查看了编辑后的答案:for key, cell in the_table.get_celld().items(): row, col = key if row > 0 and col > 0: cell.set_text_props(fontproperties=FontProperties( size=50)) 仍然无法正常工作。还是谢谢。
  • @GDino 以for key, cell in tab.get_celld().items(): 的形式规范地遍历单元格有什么不同吗?
  • 刚刚做了,结果一样。 :(
猜你喜欢
  • 1970-01-01
  • 2014-06-22
  • 2019-05-20
  • 2018-12-20
  • 2021-10-30
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多