python编程动态,如何在python中绘制动态编程表

What is a good way to draw a dynamic programming such as this one (with the path) in python?

30f630b2096083e8009e9442678a655a.png

I have looked online and I see pygame but is that really the best option for this sort of technical drawing?

One option might be to use matplotlib using something like

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

How can I draw the line on the table?

解决方案

The following code yields an approximation of the figure you want, using native Matplotlib tables:

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

Result:

9342411fd0571dacb4aa4de405ec82fb.png

Note that the result is not extremely beautiful, I could for example not figure out how to change the width of the column with row-labels. There is also the issue that the table is drawn in 'figure coordinates', while the line is drawn in 'data-coordinates', so if you zoom in the line and the table no longer overlap. I struggled for quite some time with these tables, but in my opinion they are quite a PITA to work with and the resulting code is hard to understand.

My preferred solution is to just draw the table by hand:

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

with result:

f7449defa22a3f15a3938ddf0b9440e0.png

This looks much nicer, and the code is straightforward to understand. You might want to adjust some line-widths and font-sizes to your own taste, and hide the axis.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值