1

So the main issues I am having with the table is coloring it completely in ('#EEECE1') color and formatting it so that there is no white area around it. This way, I can easily insert it into a ppt. doc. without it looking off with the border and extra space.

I have the following code:

def tables():
    data=[[10,10,10,5,5,5,2,2,2,4,4,4,4,4,4,5,5,5],[10,10,10,5,5,5,2,2,2,4,4,4,4,4,4,5,5,5],[10,10,10,5,5,5,2,2,2,4,4,4,4,4,4,5,5,5],[10,10,10,5,5,5,2,2,2,4,4,4,4,4,4,5,5,5]]
    fig= plt.figure()
    ax = fig.add_subplot(111)
    ax.axis('off')
    rows=['AB','BC','SK','MB']
    cols=["Gas", "Wk/Wk", "Yr/Yr","Oil", "Wk/Wk", "Yr/Yr","Bit", "Wk/Wk", "Yr/Yr","Total", "Wk/Wk", "Yr/Yr","Hor", "Wk/Wk", "Yr/Yr","Vert/Dir", "Wk/Wk", "Yr/Yr"]
    table = ax.table(cellText=data, colLabels=cols,rowLabels=rows, loc='upper center',cellLoc='center')
    table.auto_set_font_size(True)
    table.scale(1.5,1.5)
    plt.savefig(filenameTemplate2, format='png',bbox_inches='tight')

Which gives me this:

enter image description here

Any suggestions?

3 Answers 3

2

Your problem seems to be with the axes. You have a big white axis area around your table, and the png is created with tight borders around the axis, not the table. Removing the axis background colour helps a bit:

ax.set_axisbgcolor((0,0,0,0))

gets you transparent graph area. The point with a transparent bg is that it should stay transparent in PPT as well. Another way to specify the transparency is when you create the subplot:

add_subplot(111, axisbg='none')

Then you will also have to adjust the axis position relative to the figure, for example:

ax.set_position([0, 0, 1, 1])

to fill the whole figure.

The cell colours can be set when creating the table, but it seems to be a bit clumsy:

tbl=plt.table(cellText=data, cellColours=[["#EEECE1"] * len(data[0])]*len(data))

N.B. At least my matplotlib is buggy here, see the end of the answer.


So, something along these lines:

data=[[10,10,10,5,5,5,2,2,2,4,4,4,4,4,4,5,5,5],[10,10,10,5,5,5,2,2,2,4,4,4,4,4,4,5,5,5],[10,10,10,5,5,5,2,2,2,4,4,4,4,4,4,5,5,5],[10,10,10,5,5,5,2,2,2,4,4,4,4,4,4,5,5,5]]
fig = plt.figure(figsize=(6,2))
ax = fig.add_subplot(111, axisbg='none')
ax.axis('off')
ax.set_position([0, 0, 1, 1])
rows=['AB','BC','SK','MB']
cols=["Gas", "Wk/Wk", "Yr/Yr","Oil", "Wk/Wk", "Yr/Yr","Bit", "Wk/Wk", "Yr/Yr","Total", "Wk/Wk", "Yr/Yr","Hor", "Wk/Wk", "Yr/Yr","Vert/Dir", "Wk/Wk", "Yr/Yr"]
table = ax.table(cellText=data, colLabels=cols, rowLabels=rows, rowColours=['none']*len(data), colColours=['none']*len(data[0]), loc='upper center',cellLoc='center', cellColours=[["none"] * len(data[0])] * len(data))
table.auto_set_font_size(True)
table.scale(1.5, 1.5)
plt.savefig("test.png", format='png',bbox_inches='tight')

This should produce a table with fully transparent background. If you want to have background colour for the cells, change the "none" in cellColours. Kw args rowColours and colColours determine the colours behind the column and row labels. If you want to set the background for the whole image, change it in the axisbg argument of add_subplot.

And be warned: it seems that different versions of matplotlib do a bit different things with the scaling.

UPDATE: There may be a bug in table.py in factory function table... It seems that if both colColors and colLabels are defined, the cell offset is 0, and the rows jump to a wrong place.

The workaround is an ugly hack, but:

table = ax.table(cellText=data, colLabels=cols, rowLabels=rows, loc='upper center',cellLoc='center')
for cidx in table._cells:
    table._cells[cidx].set_facecolor('none')

will do. table._cells is a dictionary of cells (which are built on the Rectangle class). The same trick enables you to do whatever fancy colouring for the cell faces or edges, and the dict keys are cell coordinates.

Sign up to request clarification or add additional context in comments.

1 Comment

Just now trying to set the column/row widths. What is the syntax for doing that in table?
2

There are a few problems going on here:

The first is that although you have ax.axes('off), this just turns off the drawing of the lines and labels in the axes, but doesn't remove the axes, and this is causing all the white space below the table. To solve this, I don't know how completely turn off the drawing of the axes, but a kludge is the make the aspect ration smaller than the table, and then expand the table but the same ratio (see example below).

For the colors, it needs to be kept in mind: 1) that the axes color and figure color are different (eg, in the usual gui display, the figure color is gray and axes are white); 2) the figure color in saved figures is different than the displayed figures, and this needs to be set directly in savefig.

Finally, you might want to use transparency here, rather than the color you specifically asked for (eg, in savefig set the keyword transparent=True), but this depends a bit on what you're doing with the figure afterwards, and whether transparency is supported, so I'll just answer the question about colors that way you asked it.

Here's the figure and code:

enter image description here

data=[[10,10,10,5,5,5,2,2,2,4,4,4,4,4,4,5,5,5],[10,10,10,5,5,5,2,2,2,4,4,4,4,4,4,5,5,5],[10,10,10,5,5,5,2,2,2,4,4,4,4,4,4,5,5,5],[10,10,10,5,5,5,2,2,2,4,4,4,4,4,4,5,5,5]]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.axis('off')
ax.set_aspect(.1)
rows=['AB','BC','SK','MB']
cols=["Gas", "Wk/Wk", "Yr/Yr","Oil", "Wk/Wk", "Yr/Yr","Bit", "Wk/Wk", "Yr/Yr","Total", "Wk/Wk", "Yr/Yr","Hor", "Wk/Wk", "Yr/Yr","Vert/Dir", "Wk/Wk", "Yr/Yr"]
table = ax.table(cellText=data, colLabels=cols,rowLabels=rows, loc='upper center',cellLoc='center')
table.auto_set_font_size(True)
table.scale(1.5,15.)
plt.savefig("test.png", format='png',bbox_inches='tight', facecolor='#EEECE1', transparent=True)

1 Comment

This answer is producing the result I am looking for but I would like to set the color of the table itself, rather than the background of the figure that I am saving. Any suggestions?
0

You could try: .set_backgroundcolor('#EEECE1') you may need to set it on table, ax or fig.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.