我try 了几种方法,但都不知道如何正确地将列中的文本居中并将其全部收紧.如果能帮上忙,我们将不胜感激.


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()

enter image description here

推荐答案

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)

Python相关问答推荐

Python tkinter关闭第一个窗口,同时打开第二个窗口

使用decorator 重复超载

Flask:如何在完整路由代码执行之前返回验证

如何使用关键参数按列对Pandas rame进行排序

如何对行使用分段/部分.diff()或.pct_change()?

两极:如何分割一个大 pyramid 并并行保存每个

如何在Python中增量更新DF

避免循环的最佳方法

是什么导致对Python脚本的jQuery Ajax调用引发500错误?

如何用symy更新分段函数

将HLS纳入媒体包

如何计算两极打印机中 * 所有列 * 的出现次数?

如何将ctyles.POINTER(ctyles.c_float)转换为int?

运行终端命令时出现问题:pip start anonymous"

更改键盘按钮进入'

当独立的网络调用不应该互相阻塞时,'

Python—从np.array中 Select 复杂的列子集

递归访问嵌套字典中的元素值

为什么Django管理页面和我的页面的其他CSS文件和图片都找不到?'

在两极中过滤