小编jma*_*jma的帖子

Matplotlib表 - 为不同的列指定不同的文本对齐方式

我正在创建一个两列表,并希望文本尽可能接近.如何指定第一列是右对齐而第二列是左对齐?

我试过将一般cellloc设置为一边(cellloc设置文本对齐)

from matplotlib import pyplot as plt

data = [['x','x'] for x in range(10)]
bbox = [0,0,1,1]

tb = plt.table(cellText = data, cellLoc='right', bbox = bbox)
plt.axis('off') # get rid of chart axis to only show table
Run Code Online (Sandbox Code Playgroud)

然后循环遍历第二列中的单元格以将它们设置为左对齐:

for key, cell in tb.get_celld().items():
    if key[1] == 1: # if the y value is equal to 1, meaning the second column
        cell._text.set_horizontalalignment('left') # then change the alignment
Run Code Online (Sandbox Code Playgroud)

紧接在上面的这个循环没有效果,并且文本保持右对齐.

我错过了什么吗?或者这不可能吗?

编辑

解决方法是我将数据分成两个不同的列表,每列一个.这产生了我正在寻找的结果,但我想知道是否有人知道另一种方式.

data_col1 = [xy[0] for xy in data]
data_col2 = …
Run Code Online (Sandbox Code Playgroud)

python matplotlib

5
推荐指数
1
解决办法
1237
查看次数

标签 统计

matplotlib ×1

python ×1