docs for maze-dataset v1.3.2
View Source on GitHub

maze_dataset.plotting.plot_dataset

plot_dataset_mazes will plot several mazes using as_pixels

print_dataset_mazes will use as_ascii to print several mazes


 1"""`plot_dataset_mazes` will plot several mazes using `as_pixels`
 2
 3`print_dataset_mazes` will use `as_ascii` to print several mazes
 4"""
 5
 6import matplotlib.pyplot as plt  # type: ignore[import]
 7
 8from maze_dataset.dataset.maze_dataset import MazeDataset
 9
10
11def plot_dataset_mazes(
12	ds: MazeDataset,
13	count: int | None = None,
14	figsize_mult: tuple[float, float] = (1.0, 2.0),
15	title: bool | str = True,
16) -> tuple | None:
17	"plot `count` mazes from the dataset `d` in a single figure using `SolvedMaze.as_pixels()`"
18	count = count or len(ds)
19	if count == 0:
20		print("No mazes to plot for dataset")
21		return None
22	fig, axes = plt.subplots(
23		1,
24		count,
25		figsize=(count * figsize_mult[0], figsize_mult[1]),
26	)
27	if count == 1:
28		axes = [axes]
29	for i in range(count):
30		axes[i].imshow(ds[i].as_pixels())
31		# remove ticks
32		axes[i].set_xticks([])
33		axes[i].set_yticks([])
34
35	# set title
36	if title:
37		if isinstance(title, str):
38			fig.suptitle(title)
39		else:
40			kwargs: dict = {
41				"grid_n": ds.cfg.grid_n,
42				# "n_mazes": ds.cfg.n_mazes,
43				**ds.cfg.maze_ctor_kwargs,
44			}
45			fig.suptitle(
46				f"{ds.cfg.to_fname()}\n{ds.cfg.maze_ctor.__name__}({', '.join(f'{k}={v}' for k, v in kwargs.items())})",
47			)
48
49	# tight layout
50	fig.tight_layout()
51	# remove whitespace between title and subplots
52	fig.subplots_adjust(top=1.0)
53
54	return fig, axes
55
56
57def print_dataset_mazes(ds: MazeDataset, count: int | None = None) -> None:
58	"print ascii representation of `count` mazes from the dataset `d`"
59	count = count or len(ds)
60	if count == 0:
61		print("No mazes to print for dataset")
62		return
63	for i in range(count):
64		print(ds[i].as_ascii(), "\n\n-----\n")

def plot_dataset_mazes( ds: maze_dataset.MazeDataset, count: int | None = None, figsize_mult: tuple[float, float] = (1.0, 2.0), title: bool | str = True) -> tuple | None:
12def plot_dataset_mazes(
13	ds: MazeDataset,
14	count: int | None = None,
15	figsize_mult: tuple[float, float] = (1.0, 2.0),
16	title: bool | str = True,
17) -> tuple | None:
18	"plot `count` mazes from the dataset `d` in a single figure using `SolvedMaze.as_pixels()`"
19	count = count or len(ds)
20	if count == 0:
21		print("No mazes to plot for dataset")
22		return None
23	fig, axes = plt.subplots(
24		1,
25		count,
26		figsize=(count * figsize_mult[0], figsize_mult[1]),
27	)
28	if count == 1:
29		axes = [axes]
30	for i in range(count):
31		axes[i].imshow(ds[i].as_pixels())
32		# remove ticks
33		axes[i].set_xticks([])
34		axes[i].set_yticks([])
35
36	# set title
37	if title:
38		if isinstance(title, str):
39			fig.suptitle(title)
40		else:
41			kwargs: dict = {
42				"grid_n": ds.cfg.grid_n,
43				# "n_mazes": ds.cfg.n_mazes,
44				**ds.cfg.maze_ctor_kwargs,
45			}
46			fig.suptitle(
47				f"{ds.cfg.to_fname()}\n{ds.cfg.maze_ctor.__name__}({', '.join(f'{k}={v}' for k, v in kwargs.items())})",
48			)
49
50	# tight layout
51	fig.tight_layout()
52	# remove whitespace between title and subplots
53	fig.subplots_adjust(top=1.0)
54
55	return fig, axes

plot count mazes from the dataset d in a single figure using SolvedMaze.as_pixels()