1

I am trying to put two tables on the same graph in matplotlib. The only solution I got so far is to use the loc option:

T1 = plt.table(cellText=A, loc='left', rowLabels=names1, colLabels=names2,\
               cellColours=plt.cm.coolwarm(normal(A)))
T2 = plt.table(cellText=B, loc='right', rowLabels=names1, colLabels=names2,\
               cellColours=plt.cm.coolwarm(normal(B)))

which results in two tables being too far from each other and cut by the graph borders.

Is there a way to manually control the distance between the tables?

1 Answer 1

2

You can specify exactly the coordinates of your axes, then you can place each table in a separate axis. See the following example:

left, width = 0.1, 0.6
bottom, height = 0.1, 0.8
left_table = left+width+0.1
table_width = 0.15
table_height = width/2.

rect_main = [left, bottom, width, height]
rect_table1 = [left_table, table_height+bottom , table_width, table_height]
rect_table2 = [left_table, bottom, table_width, table_height]

axMain = plt.axes(rect_main)
axTable1 = plt.axes(rect_table1, frameon =False)
axTable2 = plt.axes(rect_table2, frameon =False)
axTable1.axes.get_xaxis().set_visible(False)
axTable2.axes.get_xaxis().set_visible(False)
axTable1.axes.get_yaxis().set_visible(False)
axTable2.axes.get_yaxis().set_visible(False)

axMain.plot([1,2,3])
axTable1.table(cellText=[[1,1],[2,2]], loc='upper center',
               rowLabels=['row1','row2'], colLabels=['col1','col2'])
axTable2.table(cellText=[[3,3],[4,4]], loc='upper center',
               rowLabels=['row1','row2'], colLabels=['col1','col2'])

The output of this looks like: enter image description here

Alternatively you can use the subplot feature to arrange the axes for you:

axMain = subplot(1,2,1)
axTable1 = subplot(2,2,2, frameon =False)
axTable2 = subplot(2,2,4, frameon =False)
setp(axTable1, xticks=[], yticks=[]) # another way of turning off ticks
setp(axTable2, xticks=[], yticks=[]) # another way of turning off ticks

axMain.plot([1,2,3])
axTable1.table(cellText=[[1,1],[2,2]], loc='upper center',
               rowLabels=['row1','row2'], colLabels=['col1','col2'])
axTable2.table(cellText=[[3,3],[4,4]], loc='upper center',
               rowLabels=['row1','row2'], colLabels=['col1','col2'])

The outcome of this is: enter image description here

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

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.