📚 The CoCalc Library - books, templates and other resources
License: OTHER
import pandas as pd1import numpy as np2import matplotlib.pyplot as plt3import seaborn as sns45import matplotlib as mpl6mpl.rcParams['lines.linewidth'] = 2.0789def three_frame(world, n_seq, seed=17):10"""Draw three timesteps.1112world: object with step, loop, and draw13n_seq: 3-tuple, number of steps before each draw14seed: random see for NumPy15"""16np.random.seed(seed)17plt.figure(figsize=(10, 4))1819for i, n in enumerate(n_seq):20plt.subplot(1, 3, i+1)21world.loop(n)22world.draw()2324plt.tight_layout()252627def savefig(filename, **options):28"""Save the current figure.2930Keyword arguments are passed along to plt.savefig3132https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html3334filename: string35"""36print("Saving figure to file", filename)37plt.savefig(filename, **options)383940def underride(d, **options):41"""Add key-value pairs to d only if key is not in d.4243d: dictionary44options: keyword args to add to d45"""46for key, val in options.items():47d.setdefault(key, val)4849return d505152def decorate(**options):53"""Decorate the current axes.5455Call decorate with keyword arguments like5657decorate(title='Title',58xlabel='x',59ylabel='y')6061The keyword arguments can be any of the axis properties6263https://matplotlib.org/api/axes_api.html6465In addition, you can use `legend=False` to suppress the legend.6667And you can use `loc` to indicate the location of the legend68(the default value is 'best')69"""70loc = options.pop("loc", "best")71if options.pop("legend", True):72legend(loc=loc)7374plt.gca().set(**options)75plt.tight_layout()767778def legend(**options):79"""Draws a legend only if there is at least one labeled item.8081options are passed to plt.legend()82https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html8384"""85underride(options, loc="best", frameon=False)8687ax = plt.gca()88handles, labels = ax.get_legend_handles_labels()89if handles:90ax.legend(handles, labels, **options)919293def set_palette(*args, **kwds):94"""Set the matplotlib color cycler.9596args, kwds: same as for sns.color_palette9798Also takes a boolean kwd, `reverse`, to indicate99whether the order of the palette should be reversed.100101returns: list of colors102"""103reverse = kwds.pop('reverse', False)104palette = sns.color_palette(*args, **kwds)105106palette = list(palette)107if reverse:108palette.reverse()109110cycler = plt.cycler(color=palette)111plt.gca().set_prop_cycle(cycler)112return palette113114115116117