Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib - MatplotlibDeprecationWarning

I have made a program, that makes a random walk and plots it. And it works perfectly, but I get a warning every time I run the program. It says:

MatplotlibDeprecationWarning: 
Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  
In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed,
and the future behavior ensured, by passing a unique label to each axes instance.

The warning warns me about, when I try to remove the x and y axis in rw_visual.py

My random_walk.py (If you need to test it):

from random import choice

class RandomWalk():
    """A class to generate random walks."""

    def __init__(self, num_points=5000):
        """Initialize attributes of a walk."""
        self.num_points = num_points

        # All walks start at (0, 0).
        self.x_values = [0]
        self.y_values = [0]

    def fill_walk(self):
        """Calculate all the points in the walk."""

        # Keep taking steps until the walk reaches the desired length.
        while len(self.x_values) < self.num_points:

            # Decide which direction to go and how far to go in that direction.
            x_direction = choice([1, -1])
            x_distance = choice([0, 1, 2, 3, 4])
            x_step = x_direction * x_distance

            y_direction = choice([1, -1])
            y_distance = choice([0, 1, 2, 3, 4])
            y_step = y_direction * y_distance

            # Reject moves that go nowhere.
            if x_step == 0 and y_step == 0:
                continue

            # Calculate the next x and y values.
            next_x = self.x_values[-1] + x_step
            next_y = self.y_values[-1] + y_step

            self.x_values.append(next_x)
            self.y_values.append(next_y)

My rw_visual.py (Where the warning occurs):

import matplotlib.pyplot as plt

from random_walk import RandomWalk

# Keep making new walks, as long as the program is active.
while True:
    # Make random walk, and plot the points.
    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,
                edgecolor='none', s=10)

    # Emphasize the first and last points.
    plt.scatter(0, 0, c='green', edgecolors='none', s=150)
    plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolors='none',
                s=150)

    # Remove the axes.
    plt.axes().get_xaxis().set_visible(False) # This is what it warns me about, I think.
    plt.axes().get_yaxis().set_visible(False) # It also warns me about this.
    plt.show()

    keep_running = input("Make another walk? (y/n): ")
    if keep_running == 'n':
        break

Any help is appreciated. Thanks!

like image 920
Laurits L. L. Avatar asked Aug 31 '18 19:08

Laurits L. L.


1 Answers

plt.axes() activates the already created axes (which was created when you did plt.scatter). The warning is telling you that in future releases, rather than activating the already created axes, it will create a new axes instance.

What you probably want is to get the axes that is currently being using. You can do this easily using plt.gca().

So replace

plt.axes().get_xaxis().set_visible(False) 
plt.axes().get_yaxis().set_visible(False) 

with

plt.gca().get_xaxis().set_visible(False)
plt.gca().get_yaxis().set_visible(False) 
like image 153
DavidG Avatar answered Oct 13 '22 18:10

DavidG