Python 在Matplotlib表格中修改水平对齐

Python 在Matplotlib表格中修改水平对齐,python,matplotlib,text-alignment,Python,Matplotlib,Text Alignment,matplotlib.table.table类没有文档记录,很难理解其逻辑 我可以更改字体,但不能更改“水平对齐” 此代码生成: 我们看到行['1','2','3']使用了奇怪的字体,并且是红色的。 但标题仍然“居中”,表格数据是右对齐而不是左对齐 “setfontsize”“scale”和for循环给出的结果稍有不同的顺序 matplotlib.table代码如下: import matplotlib.pyplot as plt from matplotlib.font_manager imp

matplotlib.table.table类没有文档记录,很难理解其逻辑

我可以更改字体,但不能更改“水平对齐”

此代码生成: 我们看到行['1','2','3']使用了奇怪的字体,并且是红色的。 但标题仍然“居中”,表格数据是右对齐而不是左对齐

“setfontsize”“scale”和for循环给出的结果稍有不同的顺序

matplotlib.table代码如下:

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
fig = plt.figure(figsize=(20,16))
ax1 = fig.add_subplot(1,1,1)
columns = ["label1","label2","label3"]
cell_text = [['1', '2', '3'], ['4', '5', '6']]
fontname = os.path.join(matplotlib.get_data_path(),
                            'fonts', 'ttf', 'cmex10.ttf')
fig.subplots_adjust(left=0.02, bottom=0.05, right=0.70, top=0.92)
table=ax1.table(cellText=cell_text,colLabels=columns,loc='right',
                    colWidths=[.035]*len(columns))
table.set_fontsize(48)
table.scale(1.9,2.5)
for key, cell in table.get_celld().items():
    row, col = key
    #if row > 0 and col > 0:
    cell.get_text().set_horizontalalignment('left') # this should work...
    cell.set_text_props(horizontalalignment='left') # this also...

    if row ==1:
        cell.set_text_props(fontproperties=FontProperties(fname=fontname)) # This does work!
        cell.get_text().set_color('#ff0000') # this also works !

fig.show()
print(cell.get_text().get_horizontalalignment()) # this print 'left' as expected. But after a "fig.savefig('image.png')" is prints "right" ...