當然。什麼都可以得出,它僅僅是一個所需的代碼量的事......
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)
給出了這樣的:
如果你不喜歡的背景,你甚至可以放在那裏使用imshow
拍攝的棋盤或任何您需要的照片。
一件好事就是如果你拿出由ax.plot
返回的對象,你可以刪除它們並重新繪製板子而不需要做很多工作。
ax.lines.remove(s1)
或者乾脆
s1.remove()
第一個顯示是怎麼回事;線對象從線列表中刪除,第二個類型鍵入更快,因爲線對象知道它的父項。
其中之一,它已經消失。 (您可能需要調用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)
結果基本相同。
也許,但我認爲Pygame會更好(或只是繪製ASCII藝術風格)。 – rlms