2024-heraklion-scientific-p.../code_snippets/walker_with_plotting.ipynb
2024-08-27 15:52:41 +03:00

3.3 KiB

In [ ]:
import numpy as np
import matplotlib.pyplot as plt


class Walker:
    """ The Walker knows how to walk at random on a context map. """

    def __init__(self, sigma_i, sigma_j, size, map_type='flat'):
        # ...

    def plot_trajectory(self, trajectory):
        """ Plot a trajectory over a context map. """
        trajectory = np.asarray(trajectory)
        plt.matshow(self.context_map)
        plt.plot(trajectory[:, 1], trajectory[:, 0], color='r')
        plt.show()

    def plot_trajectory_hexbin(self, trajectory):
        """ Plot an hexagonal density map of a trajectory. """
        trajectory = np.asarray(trajectory)
        with plt.rc_context({'figure.figsize': (4, 4), 
                             'axes.labelsize': 16, 
                             'xtick.labelsize': 14, 
                             'ytick.labelsize': 14}):
            plt.hexbin(
                trajectory[:, 1], trajectory[:, 0], 
                gridsize=30, extent=(0, 200, 0, 200), 
                edgecolors='none', cmap='Reds'
            )
            plt.gca().invert_yaxis()
            plt.xlabel('X')
            plt.ylabel('Y')
In [ ]:
import numpy as np
import matplotlib.pyplot as plt


class Walker:
    """ The Walker knows how to walk at random on a context map. """

    def __init__(self, sigma_i, sigma_j, size, map_type='flat'):
        # ...

    def plot_trajectory(self, trajectory):
        """ Plot a trajectory over a context map. """
        trajectory = np.asarray(trajectory)
        plt.matshow(self.context_map)
        plt.plot(trajectory[:, 1], trajectory[:, 0], color='r')
        plt.show()

    def plot_trajectory_hexbin(self, trajectory):
        """ Plot an hexagonal density map of a trajectory. """
        trajectory = np.asarray(trajectory)
        plt.hexbin(
            trajectory[:, 1], trajectory[:, 0], 
            gridsize=30, extent=(0, 200, 0, 200), 
            edgecolors='none', cmap='Reds'
        )
        plt.gca().invert_yaxis()
        plt.xlabel('X')
        plt.ylabel('Y')