如何合并matplotlib表中的单元格

时间:2019-04-15 20:26:10

标签: python matplotlib

我使用matplotlib创建了一个表,我想合并一些单元格。索引中具有相同字符串的单元格将合并为一个单元格。

问题被标记为重复,但链接无法回答我的问题

#tbl_handler is an instance of matplotlib.pyplot.subplots
#headers is the column names
#rows are the row names or indices (similiar to a dataframe) similar indices are merged. the values inside this list are sorted so that repeated values are next to each other.
def create_table(self, tbl_handler, headers, rows, data):
   tbl_handler.axis("off")

   #create table
   table = tbl_handler.table(cellText=data,
                             rowLabels=rows, colLoc = 'left',
                             colLabels=headers, loc="upper center")
   table.auto_set_font_size(False)
   table.scale(1, 1.2)
   a = 0
   b = 0
   for i in range(0, len(rows)):
      #merge the indices that are equal
      if rows[i] != rows[a]:
         b = i - 1

         #merge function goes here. below line is what I initially thought 
         #was in matplotlib
         table.cell(a,0).merge(table.cell(b,0))
         a = i
   self.__fig.tight_layout()

我应该能够合并相等的索引

0 个答案:

没有答案