Matplotlib表中的文本未居中

3j86kqsm  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(160)

我尝试了几种方法,但不知道如何正确地将文本集中在列中并收紧所有文本。

plt.close()
colors = []
for _, row in mail_decision_table.iterrows():
    colors_in_column = ["aliceblue", "lightgrey"]
    if row["Decision"]=="SELL":
        colors_in_column[1] = "lightcoral"
    else:
        colors_in_column[1] = "lightgreen"
    colors.append(colors_in_column)

fig, ax = plt.subplots()
ax.axis('tight')
ax.axis('off')


the_table = ax.table(cellText = mail_decision_table.values,
    colLabels = mail_decision_table.columns,
    loc='center',
    rowLoc='center',
    colColours = ["grey","grey"],
    cellColours=colors)

for key, cell in the_table.get_celld().items():
    cell.set_linewidth(0.5)
    cell.set_linestyle("dashed")
    cell.set_edgecolor("grey")
    cell._loc = 'center'
    if key[0] ==0 :
        cell._text.set_color('white')

plt.show()

c6ubokkw

c6ubokkw1#

cellLoc='center'选项添加到the_table变量:

the_table = ax.table(cellText = mail_decision_table.values,
    colLabels = mail_decision_table.columns,
    loc='center',
    cellLoc='center',
    rowLoc='center',
    colColours = ["grey","grey"],
    cellColours=colors)

相关问题