0

I'm learning basics of matplotlib library with "Python Crash Course" by Eric Matthew. However, I've bogged on p. 337. When I was trying to hide axes as shown in the book, my visualization literally went crazy. The random walk disappeared and axes remained visible. What's going on here? And how can I fix it?

A code:

https://pastebin.com/FmzYRCkg

import matplotlib.pyplot as plt
from random_walk import RandomWalk
 
while True:
    rw = RandomWalk(5000)
    rw.fill_walk()
    points_number = rw.x_points[:]
    plt.scatter(rw.x_points, rw.y_points, s=15, c=points_number,
    cmap = plt.cm.Greens, edgecolor='none')
    plt.scatter(rw.x_points[0], rw.y_points[0], s=45, c='Red')
    plt.scatter(rw.x_points[-1], rw.y_points[-1], s=45, c='Red')
    plt.axes().get_yaxis().set_visible(False)
    plt.axes().get_xaxis().set_visible(False)
    plt.show()
    answer = input('An another random walk? (Y/N) ')
    if answer.upper().strip() == 'N':
        break

An exemplary visualization:

https://i.stack.imgur.com/pb0Iq.jpg

I think something must be wrong with 12th and 13th lines. When I comment them out, the random walk appear again.

nyani
  • 3
  • 2
  • `plt.axes()` creates new empty axes on top of the existing plot. You need `ax = plt.gca()` and then `ax.xaxis.set_visible(....)` or `ax.axis('off')`. – JohanC Apr 04 '22 at 21:38
  • I'm the author of Python Crash Course. This code is from the first edition of the book, which went out of print in 2019 and is now pretty out of date. The second edition is fully up to date, and if you want to do the rest of the projects in the book I strongly recommend you find a copy of the second edition. – japhyr Apr 05 '22 at 00:48

0 Answers0