用Matplotlib绘制围棋棋盘

2024-09-29 19:34:25 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以在matplotlib中绘制Go-Board? 我不会给你看我可怕的尝试(其中包括一些补丁的工作),只要你不要求他们,我希望你能想出更好的主意。在

或者更好:有一个库可以用来做这个,或者已经有人编程了? 那太好了!在

(为什么有人需要matplotlib中的围棋板?原因很多。我的AI用Python/C++来工作,以及一些性能的可视化,这是在MatPultLB中绘制的。现在可以导出/导入到.sgf,但这包括一个外部查看器,如果需要多个绘图,则速度较慢。)在


Tags: boardgo绘图matplotlib可视化编程绘制原因
1条回答
网友
1楼 · 发布于 2024-09-29 19:34:25

当然可以。任何东西都可以画出来,只是需要多少代码。。。在

import matplotlib.pyplot as plt

# create a 8" x 8" board
fig = plt.figure(figsize=[8,8])
fig.patch.set_facecolor((1,1,.8))

ax = fig.add_subplot(111)

# draw the grid
for x in range(19):
    ax.plot([x, x], [0,18], 'k')
for y in range(19):
    ax.plot([0, 18], [y,y], 'k')

# scale the axis area to fill the whole figure
ax.set_position([0,0,1,1])

# get rid of axes and everything (the figure background will show through)
ax.set_axis_off()

# scale the plot area conveniently (the board is in 0,0..18,18)
ax.set_xlim(-1,19)
ax.set_ylim(-1,19)

# draw Go stones at (10,10) and (13,16)
s1, = ax.plot(10,10,'o',markersize=30, markeredgecolor=(0,0,0), markerfacecolor='w', markeredgewidth=2)
s2, = ax.plot(13,16,'o',markersize=30, markeredgecolor=(.5,.5,.5), markerfacecolor='k', markeredgewidth=2)

给出了:

enter image description here

如果你不喜欢背景,你甚至可以用imshow在那里放一些围棋板的漂亮照片或任何你需要的东西。在

一件好事是,如果使用ax.plot返回的对象,则可以删除它们并在不做大量工作的情况下重新填充板。在

^{pr2}$

或者干脆

s1.remove()

第一个显示正在进行的操作;line对象从line列表中删除,第二个对象输入速度更快,因为line对象知道它的父对象。在

不管怎样,它都不见了。(您可能需要调用draw来查看更改。)


在python中有很多方法可以完成任务,matplotlib也不例外。根据tcaswell的建议,这些线被网格代替,圆形标记用圆形补丁代替。而且,现在黑白宝石是从原型中创造出来的。在

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import copy

fig = plt.figure(figsize=[8,8], facecolor=(1,1,.8))
ax = fig.add_subplot(111, xticks=range(19), yticks=range(19), axis_bgcolor='none', position=[.1,.1,.8,.8])
ax.grid(color='k', linestyle='-', linewidth=1)
ax.xaxis.set_tick_params(bottom='off', top='off', labelbottom='off')
ax.yaxis.set_tick_params(left='off', right='off', labelleft='off')

black_stone = mpatches.Circle((0,0), .45, facecolor='k', edgecolor=(.8,.8,.8, 1), linewidth = 2, clip_on=False, zorder=10)
white_stone = copy.copy(black_stone)
white_stone.set_facecolor((.9, .9, .9))
white_stone.set_edgecolor((.5, .5, .5))

s1 = copy.copy(black_stone)
s1.center = (18,18)
ax.add_patch(s1)

s2 = copy.copy(white_stone)
s2.center = (6,10)
ax.add_patch(s2)

结果基本相同。在

相关问题 更多 >

    热门问题