Akavall
Akavall

Reputation: 86276

Subplot of tables in pylab

I am looking for a way to create figure that contains several subplots which are tables. Let me try to explain what I am talking about. Below is a figure that has several subplots that are imshow plots. I want the exact same figure, but instead of `imshow' plots I want tables, just plain tables. In my example they would just display values 1 and 2:

[1, 2]
[2, 1]

How should I do this?

Thank You in Advance

enter image description here

Here is the code I used to generate the graph.

import pylab
import numpy as np

x = np.array([[1,2],[2,1]])

fig = pylab.figure()

fig_list = []

for i in xrange(5):

    fig_list.append( fig.add_subplot(2,3,i+1) )
    fig_list[i] = pylab.imshow(x)


pylab.savefig('my_fig.pdf')
pylab.show()

Upvotes: 0

Views: 1537

Answers (1)

stanri
stanri

Reputation: 2972

You can use the pylab.table command, documentation found here.

For example:

import pylab
import numpy as np

x = [[1,2],[2,1]]

fig = pylab.figure()

axes_list = []
table_list = []

for i in xrange(5):
    axes_list.append( fig.add_subplot(2,3,i+1) )
    axes_list[i].set_xticks([])
    axes_list[i].set_yticks([])
    axes_list[i].set_frame_on(False)
    table_list.append(pylab.table(cellText=x,colLabels = ['col']*2,rowLabels=['row']*2,colWidths = [0.3]*2,loc='center'))

pylab.savefig('my_fig.pdf')
pylab.show()

I've also created an additional list variable, and renamed the fig_list, because the axes instance was being overwritten by the instance of the plotted object. Now you have access to both handles.

Other useful commands include:

# Specify a title for the plot
axes_list[i].set_title('test')

# Specify the axes size and position
axes_list[i].set_position([left, bottom, width, height])

# The affect of the above set_position can be seen by turning the axes frame on, like so:
axes_list[i].set_frame_on(True)

Documentation:

Upvotes: 2

Related Questions