Matplotlib 绘制盒子

发布于 2024-09-25 06:14:06 字数 408 浏览 3 评论 0原文

我有一组数据,其中每个值都有一个 (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 中完成它?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

捂风挽笑 2024-10-02 06:14:06

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

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

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)]
苏辞 2024-10-02 06:14:06
# 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) + '+'
# 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) + '+'
探春 2024-10-02 06:14:06

也许使用 ReportLab 会更好。

示例

Perhaps it would be better to use the ReportLab.

Example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文