Matplotlib更改表内的字体大小(仅标题)-python

时间:2018-07-12 13:32:07

标签: python matplotlib fonts size

我希望列标题的字体比单元格中的值小,以便它们可读(我将在所附的jpeg中显示我的意思)。现在,所有内容都具有相同的字体大小。

表代码示例:

fig = plt.figure(figsize=(11, 8.27))
ax = fig.add_subplot(111)
ax.axis('off')

index_length = len(well_data_table.index)

table_1 = well_data_table.iloc[0:30]
table_2= well_data_table.iloc[30:60]
table_3 = well_data_table.iloc[60:-1]

q='lightsalmon'
colors3 = [q,q,q,q,q,q,q,q,q,q,q]
the_table1 = ax.table(cellText=table_1.values, colWidths = 
[.1]*len(table_1.columns),
  rowLabels=table_1.index,
  colColours = colors3,
  colLabels=table_1.columns,
  cellLoc = 'center', rowLoc = 'center',
  loc='bottom',
  bbox=[.1, 0, 1, 1]) 

the_table1.auto_set_font_size(False)
the_table1.set_fontsize(8)
the_table1.scale(1, 1)


ax.title.set_text("""TEST""")


pdf.savefig(facecolor='w')

JPEG Example of the issue

2 个答案:

答案 0 :(得分:0)

您可以循环访问应该具有不同字体大小的表单元格,并在该循环中设置字体大小。

class EventSerializer(serializers.ModelSerializer):

    class Meta:
        model = Event
        fields = '__all__'

    def to_representation(self, instance):
        result = {'ts': instance.ts}
        if isinstance(instance, Foo):
            result['foo'] = FooSerializer(instance).data
        return result

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username')

class FooSerializer(serializers.ModelSerializer):
   # user = UserSerializer(read_only=True) # with this I have an error: Got AttributeError when attempting to get a value for field 'username' on #serializer 'UserSerializer'

    class Meta:
        model = Foo
        fields = '__all__'
        depth = 1

答案 1 :(得分:0)

这是一个示例,它帮助我使用索引元组(在示例中为“键”)遍历单元格,以执行删除边框或设置标头(在此示例中为row和col标头)之类的事情:

for key, cell in table.get_celld().items():
    # scrub borders for clean look(see source below)
    cell.set_linewidth(0)

    # adjust format for only header col and row to help with space issues
    # col header on 0, row header on -1.
    if key[0] == 0 or key[1] == -1:
        cell.set_fontsize(6)

我感谢this post by Bart帮助我在特定情况下消除了边界。