I wish to format one column of a table but when iterating through the rows it appears the width of the column width changes after each iteration.
Source Code
def p_create_table(self, events, dates, rows, columns, portfolio):
"""
:param events: Dict - {Date:Event} where Event is a String identifying
what event happened
:param dates: List - Dates of events
:param rows: Int - number of Dates (rows) to create for the table
:param columns: List - Column headers
:param portfolio: Dataframe - Portfolio with calculated totals and returns
:return:
"""
cell_text = self.p_create_cell_text(events, dates, portfolio)
cell_text.pop(0)
row_labels = self.p_create_row_labels(rows)
row_labels.pop(len(row_labels) - 1)
colors = self.p_set_table_colors(row_labels)
table = plt.table(cellText=cell_text, cellColours=colors[0],
rowColours=colors[1], rowLabels=row_labels,
colColours=colors[2], colLabels=columns,
bbox=[0.0, -1.3, 1.0, 1.0], cellLoc='center')
table.auto_set_font_size(False)
table.set_fontsize(9)
table.scale(2, 2)
cell_dict = table.get_celld()
for i in range(13):
cell_dict[(i,1)].set_width(0.3)
Below is an image of the table BEFORE the resizing. The snapshot was taken after line table.set_fontsize(9)
was executed. I would like to re-size the second column Event
.
Before formatting
Unfortunately, after ever iteration of:
for i in range(13):
cell_dict[(i,1)].set_width(0.3)
it looks like the cell width increases, resulting in something like this:
After formatting
Any suggestions on why this could be happening, or an alternative solution to adjusting the width would be much appreciated!