Skip to content

Inconsistent indexing of row and column label cells by Tables #12931

Closed
@anntzer

Description

@anntzer

In presence of row and column labels, the cell indexing by the Table class is inconsistent: column labels have row index 0, but row labels have column index -1:

from pylab import *
gca().set_axis_off()
t = table(
    cellText=[["a", "b"], ["c", "d"]],
    rowLabels=["row1", "row2"],
    colLabels=["col1", "col2"],
)
for (i, j), cell in sorted(t.get_celld().items()):
    print(i, j, cell.get_text())
show()

gives

0 0 Text(0, 0, 'col1')
0 1 Text(0, 0, 'col2')
1 -1 Text(0, 0, 'row1')
1 0 Text(0, 0, 'a')
1 1 Text(0, 0, 'b')
2 -1 Text(0, 0, 'row2')
2 0 Text(0, 0, 'c')
2 1 Text(0, 0, 'd')

As an example of bug caused by this API, the text_labels_and_annotations/font_table_ttf_sfskip.py example contains the seemingly innocuous code

    if row > 0 and col > 0:
        cell.set_text_props(fontproperties=FontProperties(fname=fontname))

but running the example, passing a local font pathname as argument (e.g. python examples/text_labels_and_annotations/font_table_ttf_sgskip.py lib/matplotlib/mpl-data/fonts/ttf/cmr10.ttf from the mpl root directory) shows that the first column of characters is not using the correct font.
test

Indeed, the correct code would have been

    if row > 0 and col > -1:
        cell.set_text_props(fontproperties=FontProperties(fname=fontname))

which gives
correct

I think the inconsistency is error-prone enough to consider changing it (probably handling the deprecations depending on the value of a global switch).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions