Table with extra column Matplotlib
Question:
I am plotting 2 simple tables but an extra column keeps appearing on both. I checked that my data has only 3 columns and so is the colLabel.
from matplotlib import pyplot as plt
header= ['Country','HDI','IPV']
header_colors1 = ['#03a1fc', '#03a1fc', '#03a1fc']
row_colors1 = ['#f1f1f2', 'w'] * 10
header_colors2 = ['#dd8452', '#dd8452', '#dd8452']
row_colors2 = ['w', '#f1f1f2'] * 10
fig, axs = plt.subplots(1, 2, figsize=(8, 4))
fig.patch.set_visible(False)
axs[0].axis('off')
axs[0].axis('tight')
least_dev_values = [str(x) for x in range(10)]
most_dev_values = [str(x) for x in range(10)]
least_dev_tabel= axs[0].table(cellText=least_dev_values, colLabels=header, loc='center',
cellLoc='center', colLoc='center', rowColours=row_colors1,
colColours=header_colors1)
axs[1].axis('off')
axs[1].axis('tight')
axs[1].table(cellText=most_dev_values, colLabels=header, loc='center',
cellLoc='center', colLoc='center', rowColours=row_colors2,
colColours=header_colors2)
plt.subplots_adjust(wspace=0.8)
plt.show()
Answers:
If you remove the rowColours it works as expected
least_dev_tabel= axs[0].table(cellText=least_dev_values, colLabels=header, loc='center',
cellLoc='center', colLoc='center', #rowColours=row_colors1,
colColours=header_colors1)
It looks like you might want to add more columns to the table based on your headers? The docs say that these are the colors of your header rows. If you want to color your cells you can use cellClours
instead
I am plotting 2 simple tables but an extra column keeps appearing on both. I checked that my data has only 3 columns and so is the colLabel.
from matplotlib import pyplot as plt
header= ['Country','HDI','IPV']
header_colors1 = ['#03a1fc', '#03a1fc', '#03a1fc']
row_colors1 = ['#f1f1f2', 'w'] * 10
header_colors2 = ['#dd8452', '#dd8452', '#dd8452']
row_colors2 = ['w', '#f1f1f2'] * 10
fig, axs = plt.subplots(1, 2, figsize=(8, 4))
fig.patch.set_visible(False)
axs[0].axis('off')
axs[0].axis('tight')
least_dev_values = [str(x) for x in range(10)]
most_dev_values = [str(x) for x in range(10)]
least_dev_tabel= axs[0].table(cellText=least_dev_values, colLabels=header, loc='center',
cellLoc='center', colLoc='center', rowColours=row_colors1,
colColours=header_colors1)
axs[1].axis('off')
axs[1].axis('tight')
axs[1].table(cellText=most_dev_values, colLabels=header, loc='center',
cellLoc='center', colLoc='center', rowColours=row_colors2,
colColours=header_colors2)
plt.subplots_adjust(wspace=0.8)
plt.show()
If you remove the rowColours it works as expected
least_dev_tabel= axs[0].table(cellText=least_dev_values, colLabels=header, loc='center',
cellLoc='center', colLoc='center', #rowColours=row_colors1,
colColours=header_colors1)
It looks like you might want to add more columns to the table based on your headers? The docs say that these are the colors of your header rows. If you want to color your cells you can use cellClours
instead