python - Formatting issues with table image -
so main issues having table coloring in ('#eeece1') color , formatting there no white area around it. way, can insert ppt. doc. without looking off border , space.
i have next 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:
any suggestions?
your problem seems axes. have big white axis area around table, , png created tight borders around axis, not table. removing axis background colour helps bit:
ax.set_axisbgcolor((0,0,0,0))
gets transparent graph area. point transparent bg should remain transparent in ppt well. way specify transparency when create subplot:
add_subplot(111, axisbg='none')
then have adjust axis position relative figure, example:
ax.set_position([0, 0, 1, 1])
to fill whole figure.
the cell colours can set when creating table, seems bit clumsy:
tbl=plt.table(celltext=data, cellcolours=[["#eeece1"] * len(data[0])]*len(data))
n.b. @ to the lowest degree matplotlib buggy here, see end of answer.
so, 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 table transparent background. if want have background colour cells, alter "none" in cellcolours. kw args rowcolours
, colcolours
determine colours behind column , row labels. if want set background whole image, alter in axisbg
argument of add_subplot
.
and warned: seems different versions of matplotlib bit different things scaling.
update: there may bug in table.py
in mill function table
... seems if both colcolors
, collabels
defined, cell offset 0, , rows jump wrong place.
the workaround ugly hack, but:
table = ax.table(celltext=data, collabels=cols, rowlabels=rows, loc='upper center',cellloc='center') cidx in table._cells: table._cells[cidx].set_facecolor('none')
will do. table._cells
dictionary of cells (which built on rectangle
class). same trick enables whatever fancy colouring cell faces or edges, , dict keys cell coordinates.
python table matplotlib formatting
No comments:
Post a Comment