miscplot.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import numpy as np
  2. import matplotlib as mpl
  3. import matplotlib.pyplot as plt
  4. import matplotlib.ticker as ticker
  5. __all__ = ["palplot", "dogplot"]
  6. def palplot(pal, size=1):
  7. """Plot the values in a color palette as a horizontal array.
  8. Parameters
  9. ----------
  10. pal : sequence of matplotlib colors
  11. colors, i.e. as returned by seaborn.color_palette()
  12. size :
  13. scaling factor for size of plot
  14. """
  15. n = len(pal)
  16. _, ax = plt.subplots(1, 1, figsize=(n * size, size))
  17. ax.imshow(np.arange(n).reshape(1, n),
  18. cmap=mpl.colors.ListedColormap(list(pal)),
  19. interpolation="nearest", aspect="auto")
  20. ax.set_xticks(np.arange(n) - .5)
  21. ax.set_yticks([-.5, .5])
  22. # Ensure nice border between colors
  23. ax.set_xticklabels(["" for _ in range(n)])
  24. # The proper way to set no ticks
  25. ax.yaxis.set_major_locator(ticker.NullLocator())
  26. def dogplot(*_, **__):
  27. """Who's a good boy?"""
  28. from urllib.request import urlopen
  29. from io import BytesIO
  30. url = "https://github.com/mwaskom/seaborn-data/raw/master/png/img{}.png"
  31. pic = np.random.randint(2, 7)
  32. data = BytesIO(urlopen(url.format(pic)).read())
  33. img = plt.imread(data)
  34. f, ax = plt.subplots(figsize=(5, 5), dpi=100)
  35. f.subplots_adjust(0, 0, 1, 1)
  36. ax.imshow(img)
  37. ax.set_axis_off()