Thanks to visit codestin.com
Credit goes to github.com

Skip to content

table.py: fix issue when specifying both column header text and color #2389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Feb 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/matplotlib/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,26 +495,29 @@ def table(ax,
if colWidths is None:
colWidths = [1.0 / cols] * cols

# Check row and column labels
# Fill in missing information for column
# and row labels
rowLabelWidth = 0
if rowLabels is None:
if rowColours is not None:
rowLabels = [''] * cols
rowLabels = [''] * rows
rowLabelWidth = colWidths[0]
elif rowColours is None:
rowColours = 'w' * rows

if rowLabels is not None:
assert len(rowLabels) == rows

offset = 0
# If we have column labels, need to shift
# the text and colour arrays down 1 row
offset = 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide a comment here explaining the logic. Something along the lines of "Assume the table has a column label row, so offset by one". I am not familiar enough with the tables feature to understand the oddity of having blank column labels if column colors are specified, so an explanation there would be useful as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - TBH I don't understand the use case for that either, but that's the way the code worked previously. I was really only interested in fixing the case where both color and text were specified.

if colLabels is None:
if colColours is not None:
colLabels = [''] * rows
offset = 1
colLabels = [''] * cols
else:
offset = 0
elif colColours is None:
colColours = 'w' * cols
offset = 1

if rowLabels is not None:
assert len(rowLabels) == rows
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions lib/matplotlib/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,41 @@ def test_zorder():
zorder=4,
)
plt.yticks([])


@image_comparison(baseline_images=['table_labels'],
extensions=['png'])
def test_label_colours():
dim = 3

c = np.linspace(0, 1, dim)
colours = plt.cm.RdYlGn(c)
cellText = [['1'] * dim] * dim

fig = plt.figure()

ax1 = fig.add_subplot(4, 1, 1)
ax1.axis('off')
ax1.table(cellText=cellText,
rowColours=colours,
loc='best')

ax2 = fig.add_subplot(4, 1, 2)
ax2.axis('off')
ax2.table(cellText=cellText,
rowColours=colours,
rowLabels=['Header'] * dim,
loc='best')

ax3 = fig.add_subplot(4, 1, 3)
ax3.axis('off')
ax3.table(cellText=cellText,
colColours=colours,
loc='best')

ax4 = fig.add_subplot(4, 1, 4)
ax4.axis('off')
ax4.table(cellText=cellText,
colColours=colours,
colLabels=['Header'] * dim,
loc='best')