views:

32

answers:

3

Hi,

I have a set of data, where each value has a (x, y) coordinate. Different values can have the same coordinate. And I want to draw them in a rectangular collection of boxes.

For example, if I have the data:

A -> (0, 0)
B -> (0, 1)
C -> (1, 2)
D -> (0, 1)

I want to get the following drawing:

    0   1   2
  +++++++++++++
0 + A + B +   +
  +   + D +   +
  +++++++++++++
1 +   +   + C +
  +++++++++++++
2 +   +   +   +
  +++++++++++++

How can I do it in Python using Matplotlib?

THANKS!

+1  A: 

Perhaps it would be better to use the ReportLab.

Example

iluzion
Any idea how to insert multiple values in the same cell?
Yassin
+1  A: 
# enter the data like this
X={'A':(0,0),'B':(0,1),'C':(1,2),'D':(0,1)}

# size of grid
xi=map(tuple.__getitem__,X.values(),[1]*len(X))
yi=map(tuple.__getitem__,X.values(),[0]*len(X))
xrng = (min(xi), max(xi)+1)
yrng = (min(yi), max(yi)+1)

for y in range(*yrng):         # rows
  print '+' * ((xrng[1]-xrng[0])*3) + '+'
  k={}  # each item k[x] is list of elements in xth box in this row
  for x in range(*xrng):
    # list of items in this cell
    k[x]=[u for u in X.keys() if X[u]==(y,x)]
  h=max(map(len, k.values()))  # row height
  for v in range(h):           # lines of row
    c=[]
    for x in range(*xrng):     # columns
      if k[x]: 
        c.append(k[x][0])
        del k[x][0]
      else:    c.append(' ')   # shorter cell
    s="+ " + "+ ".join(c) + "+"
    print s
print "+" * ((xrng[1]-xrng[0])*3) + '+'
Sanjay Manohar
Thanks for the code!
Yassin
+2  A: 

Just thought, maybe what you actually wanted to know was just this:

def drawbox(list,x,y):
  # write some graphics code to draw box index x,y containing items 'list'

[[drawbox(u,x,y) for u in X.keys() if X[u]==(y,x)] for x in range(0,3) for y in range(0,3)]
Sanjay Manohar