【问题标题】:How to draw dynamic programming table in python如何在python中绘制动态规划表
【发布时间】:2014-02-28 20:03:31
【问题描述】:

在python中绘制动态编程(例如这个(带有路径))的好方法是什么?

我在网上查到了pygame,但这真的是这种技术绘图的最佳选择吗?

一种选择可能是使用 matplotlib 之类的东西

import matplotlib.pylab as plt
plt.figure()
col_labels=['col1','col2','col3']
row_labels=['row1','row2','row3']
table_vals=[[11,12,13],[21,22,23],[31,32,33]]

the_table = plt.table(cellText=table_vals,
                  colWidths = [0.1]*3,
                  rowLabels=row_labels,
                  colLabels=col_labels,
                  loc='center right')
plt.text(12,3.4,'Table Title',size=8)

plt.show()

如何在桌子上画线?

【问题讨论】:

  • PyGame 出了什么问题?名字?
  • @IgnacioVazquez-Abrams pygame 不是关于交互性的吗?我只是想画一幅画。
  • 是的,但显示图像是一种交互形式,可能会受到限制。
  • 可以是,但不是必须的。我也许会尝试使用 matplotlib。使用imshow() 绘制某种表格图,为轴创建自定义标签,然后首先绘制该线。

标签: python matplotlib pygame dynamic-programming


【解决方案1】:

以下代码使用原生 Matplotlib 表生成所需图形的近似值:

import matplotlib.pylab as plt
import numpy as np

def get_coord(table, irow, icol):
    # get coordinates of a cell. This seems to work, don't ask why.
    cell = table.get_celld()[irow+1,icol] # row 0 is column headers
    box = cell.get_bbox().get_points() # [[x0, y0],[x1, y1]]
    xc, yc = box.mean(axis=0) # get center
    return xc, yc

col_labels=['G','A','T','C','C']
row_labels= ['G','T','G','C','C']
table_vals= [
    ['x','','','',''],
    ['','','x','',''],
    ['x','','','',''],
    ['','','','x','x'],
    ['','','','x','x']]
line = [(0,0), (0,1), (1,2), (2,2), (3,3), (4,4)]    

# draw table
the_table = plt.table(cellText=table_vals,
    colWidths = [0.1]*len(col_labels),
    rowLabels=row_labels, colLabels=col_labels,
    cellLoc = 'center', rowLoc = 'center', bbox=[.1,.1,.8,.8])
plt.draw() # lay out table, so that cell coordinates are calculated

# look up line coordinates
x = []; y = []
for irow, icol in line:
    xc, yc = get_coord(the_table, irow, icol)
    x.append(xc)
    y.append(yc)

# draw line    
plt.plot(x, y, 'r', linewidth = 5, alpha=0.5)
plt.xlim([0,1])
plt.ylim([0,1])
plt.show()

结果:

请注意,结果不是非常漂亮,例如,我可能不知道如何使用行标签更改列的宽度。还有一个问题是表格是在“图形坐标”中绘制的,而线条是在“数据坐标”中绘制的,所以如果你放大线条并且表格不再重叠。我在这些表上苦苦挣扎了很长一段时间,但在我看来,它们是一个非常适合使用的 PITA,生成的代码很难理解。

我首选的解决方案是手动绘制表格:

import matplotlib.pylab as plt
import numpy as np

col_labels=['G','A','T','C','C']
row_labels= ['G','T','G','C','C']
table_vals= [
    ['X','','','',''],
    ['','','X','',''],
    ['X','','','',''],
    ['','','','X','X'],
    ['','','','X','X']]
line = np.array([
    [0, 1, 2, 2, 3, 4],
    [0, 0, 1, 2, 3, 4]])    
ncol = len(col_labels)
nrow = len(row_labels)

# draw grid lines
plt.plot(np.tile([0, ncol+1], (nrow+2,1)).T, np.tile(np.arange(nrow+2), (2,1)),
    'k', linewidth=3)
plt.plot(np.tile(np.arange(ncol+2), (2,1)), np.tile([0, nrow+1], (ncol+2,1)).T,
    'k', linewidth=3)

# plot labels
for icol, col in enumerate(col_labels):
    plt.text(icol + 1.5, nrow + 0.5, col, ha='center', va='center')
for irow, row in enumerate(row_labels):
    plt.text(0.5, nrow - irow - 0.5, row, ha='center', va='center')

# plot table content
for irow, row in enumerate(table_vals):
    for icol, cell in enumerate(row):
        plt.text(icol + 1.5, nrow - irow - 0.5, cell, ha='center', va='center')

# plot line
plt.plot(line[0] + 1.5, nrow - line[1] - 0.5, 'r', linewidth = 5, alpha = 0.5)

plt.axis([-0.5, ncol + 1.5, -0.5, nrow+1.5])
plt.show()

结果:

这看起来好多了,代码也很容易理解。您可能想根据自己的喜好调整一些线宽和字体大小,并隐藏轴。

【讨论】:

  • 非常感谢!您的第二个解决方案看起来很棒。
  • 嘿家伙,我怎样才能给这张桌子的一个正方形上色?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 2012-08-16
  • 2018-02-25
相关资源
最近更新 更多