miscplot.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import numpy as np
  2. import matplotlib as mpl
  3. import matplotlib.pyplot as plt
  4. __all__ = ["palplot", "dogplot"]
  5. def palplot(pal, size=1):
  6. """Plot the values in a color palette as a horizontal array.
  7. Parameters
  8. ----------
  9. pal : sequence of matplotlib colors
  10. colors, i.e. as returned by seaborn.color_palette()
  11. size :
  12. scaling factor for size of plot
  13. """
  14. n = len(pal)
  15. f, ax = plt.subplots(1, 1, figsize=(n * size, size))
  16. ax.imshow(np.arange(n).reshape(1, n),
  17. cmap=mpl.colors.ListedColormap(list(pal)),
  18. interpolation="nearest", aspect="auto")
  19. ax.set_xticks(np.arange(n) - .5)
  20. ax.set_yticks([-.5, .5])
  21. ax.set_xticklabels([])
  22. ax.set_yticklabels([])
  23. def dogplot(*_, **__):
  24. """Who's a good boy?"""
  25. try:
  26. from urllib.request import urlopen
  27. except ImportError:
  28. from urllib2 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()