miscplot.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. f, 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. try:
  29. from urllib.request import urlopen
  30. except ImportError:
  31. from urllib2 import urlopen
  32. from io import BytesIO
  33. url = "https://github.com/mwaskom/seaborn-data/raw/master/png/img{}.png"
  34. pic = np.random.randint(2, 7)
  35. data = BytesIO(urlopen(url.format(pic)).read())
  36. img = plt.imread(data)
  37. f, ax = plt.subplots(figsize=(5, 5), dpi=100)
  38. f.subplots_adjust(0, 0, 1, 1)
  39. ax.imshow(img)
  40. ax.set_axis_off()