Matplotlib画框

时间:2010-09-27 14:51:47

标签: python drawing matplotlib

我有一组数据,其中每个值都有一个(x,y)坐标。不同的值可以具有相同的坐标。我想把它们画成一个矩形的盒子。

例如,如果我有数据:

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

我想得到以下图:

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

如何使用Matplotlib在Python中完成?

谢谢!

3 个答案:

答案 0 :(得分:2)

想一想,也许你真正想知道的只是这个:

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)]

答案 1 :(得分:1)

使用ReportLab可能会更好。

Example

答案 2 :(得分:1)

# 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) + '+'