Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Plot a matrix with different labels in each cell as a series
  4. of circles with colors that represent each label.
  5.  
  6. @author: Nicolas Guarin-Zapata
  7. """
  8. from __future__ import division, print_function
  9. import numpy as np
  10. import matplotlib.pyplot as plt
  11. from collections import OrderedDict
  12.  
  13.  
  14. plt.style.use("seaborn-white")
  15. fig = plt.figure()
  16. ax = plt.subplot(111)
  17.  
  18. # Data and labels loading
  19. data = np.loadtxt("Vieira_data.csv", dtype=str, delimiter=";")
  20. nrows, ncols = data.shape
  21. colors = {'COA': "#e41a1c",
  22. 'RAG': "#377eb8",
  23. 'CHK': "#4daf4a",
  24. 'VAR': "#984ea3",
  25. 'DAT': "#ff7f00",
  26. 'EXE': "#ffff33"}
  27.  
  28. # Plot circles
  29. for row in range(nrows):
  30. for col in range(ncols):
  31. cell_content = map(str.strip, data[row, col].split(','))
  32. hor_space = 1/(len(cell_content) + 1)
  33. for cont, label in enumerate(cell_content):
  34. ax.plot(col + (cont + 1)*hor_space, nrows - row - 0.5,
  35. 'o', ms=40, color=colors[label], label=label)
  36.  
  37. # Plot grid
  38. for row in range(nrows):
  39. for col in range(ncols):
  40. ax.plot([0, ncols], [row, row], color="black")
  41. ax.plot([col, col], [0, nrows], color="black")
  42.  
  43. # Labels
  44. xticks = [k + 0.5 for k in range(ncols)]
  45. xlabels = ["Section %i"%k for i in range(1, ncols + 1)]
  46. yticks = [nrows - k - 0.5 for k in range(nrows)]
  47. ylabels = ["Student %i"%k for k in range(1, nrows + 1)]
  48. plt.xticks(xticks, xlabels)
  49. plt.yticks(yticks, ylabels)
  50. plt.tick_params(labeltop=True, labelbottom=False)
  51.  
  52. # Legend
  53. box = ax.get_position()
  54. ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
  55. handles, labels = plt.gca().get_legend_handles_labels()
  56. by_label = OrderedDict(zip(labels, handles))
  57. plt.legend(by_label.values(), by_label.keys(), markerscale=0.4,
  58. loc='center left', bbox_to_anchor=(1, 0.5))
  59.  
  60. plt.xlim(0, ncols)
  61. plt.ylim(0, nrows)
  62. plt.savefig("Matrix_plot.png", dpi=300, bbox_inches="tight")
  63. plt.savefig("Matrix_plot.svg", bbox_inches="tight")
  64. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement