主要問題:根據代碼隱藏坐標軸,此時原有的圖像也被隱藏不正常顯示
原代碼:
while True:
#創建一個RandomWalk實例,並將其包含的點都繪制出來
rw = RandomWalk()
rw.fill_walk()
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues,
edgecolors='none', s=15)
#突出起點和終點
plt.scatter(0, 0, c='green', edgecolors='none', s=100)
plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolors='none',
s=100)
#隱藏坐標軸
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show()
keep_running = input("Make another Walk? (y/n):")
if keep_running == 'n':
break

執行結果:坐標軸沒有隱藏,原有的圖形沒有正常顯示
解決方法:
while True:
#創建一個RandomWalk實例,並將其包含的點都繪制出來
rw = RandomWalk()
rw.fill_walk()
#隱藏坐標軸
# plt.axes().get_xaxis().set_visible(False)
# plt.axes().get_yaxis().set_visible(False)
current_axes = plt.axes()
current_axes.xaxis.set_visible(False)
current_axes.yaxis.set_visible(False)
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues,
edgecolors='none', s=15)
#突出起點和終點
plt.scatter(0, 0, c='green', edgecolors='none', s=100)
plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolors='none',
s=100)
plt.show()
keep_running = input("Make another Walk? (y/n):")
if keep_running == 'n':
break
主要修改兩個地方:
1.將:
plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False)
改為:
current_axes = plt.axes() current_axes.xaxis.set_visible(False) current_axes.yaxis.set_visible(False)
2.將隱藏坐標軸的代碼移到plt.show上一行
這兩個步驟缺一不可。
最終結果:

