使用Matplotlib绘制Go-Board

var*_*tir 0 python matplotlib

可以在matplotlib中绘制Go-Board吗?我不会向你展示我可怕的尝试(包括一些补丁),只要你不要求它们,我希望你能提出更好的想法.

或者甚至更好:有一个库,或者有人已经编程了吗?那样就好了!

(为什么有人需要在matplotlib中使用GO Board?有很多原因.我的AI无论如何都适用于python/C++以及性能的一些可视化,它们在matplotlib中绘制.现在可以导出/导入到. sgf,但这包括一个外部查看器,如果需要很多图表,它会很慢.)

DrV*_*DrV 7

当然.可以绘制任何东西,只需要大量的代码......

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)
Run Code Online (Sandbox Code Playgroud)

给出这个:

在此输入图像描述

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

一个好处是,如果你拿走返回的对象ax.plot,你可以删除它们并重新绘制板,而不需要做很多工作.

ax.lines.remove(s1)
Run Code Online (Sandbox Code Playgroud)

或者干脆

s1.remove()
Run Code Online (Sandbox Code Playgroud)

第一个显示正在发生的事情; 线对象从线列表中删除,第二个更快地键入,因为线对象知道它的父对象.

其中任何一个,它已经消失了.(您可能需要致电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)
Run Code Online (Sandbox Code Playgroud)

结果基本相同.