Advertisement
Guest User

hist_plot.py

a guest
Aug 26th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from math import ceil
  4. import numpy as np
  5. import matplotlib as mlp
  6. import matplotlib.pyplot as plt
  7.  
  8.  
  9. mlp.rcParams['mathtext.fontset'] = 'stix'
  10. mlp.rcParams['font.family'] = 'STIXGeneral'
  11. mlp.pyplot.title(r'ABC123 vs $\mathrm{ABC123}^{123}$')
  12.  
  13. mlp.rcParams['axes.labelsize']  = 15
  14. mlp.rcParams['axes.titlesize']  = 20
  15. mlp.rcParams['xtick.labelsize'] = 15
  16. mlp.rcParams['ytick.labelsize'] = 15
  17.  
  18.  
  19. def reject_outliers(data, m=2):
  20.     return data[abs(data - np.mean(data)) < m * np.std(data)]
  21.  
  22. def plotDistanceHistogram (distances_real, distances_false, name):
  23.     distances_real = reject_outliers (distances_real, 3)
  24.     distances_false = reject_outliers (distances_false, 3)
  25.  
  26.     weights_real = (np.ones_like (distances_real)) / len (distances_real)
  27.     n, bins_n, patches_n = plt.hist (distances_real, 50, weights = weights_real, histtype = 'bar')
  28.  
  29.     weights_false = (np.ones_like (distances_false)) / len (distances_false)
  30.     m, bins_m, patches_m = plt.hist (distances_false, 50, weights = weights_false, histtype = 'bar')
  31.  
  32.     plt.clf ()
  33.  
  34.     x = [bins_n[0]] + [0.5 * (bins_n[i] + bins_n[i + 1]) for i in xrange (len(bins_n) - 1)] + [bins_n[-1]]
  35.     y = [0.0] + [patch.get_height () for patch in patches_n] + [0.0]
  36.     plt.plot (x, y, color = 'green', alpha = 0.75, label = 'correspondencias reales', marker = 'o')
  37.     plt.fill (x, y, color = 'green', alpha = 0.75)
  38.  
  39.     x = [bins_m[0]] + [0.5 * (bins_m[i] + bins_m[i + 1]) for i in xrange (len(bins_m) - 1)] + [bins_m[-1]]
  40.     y = [0.0] + [patch.get_height () for patch in patches_m] + [0.0]
  41.     plt.plot (x, y, color = 'red', alpha = 0.75, label = 'correspondencias falsas', marker = 'o')
  42.     plt.fill (x, y, color = 'red', alpha = 0.75)
  43.  
  44.     plt.legend (bbox_to_anchor = (0., 1., 1., .12), loc = 'upper center', ncol = 2, mode = 'expand', fontsize = 15)
  45.     plt.grid (True)
  46.  
  47.     plt.savefig ('histograma-distancias-' + name + '.png', bbox_inches = 'tight')
  48.  
  49.  
  50. def main ():
  51.     mu, sigma = 0.25, 0.05
  52.     x = mu + sigma * np.random.randn (10000)
  53.     y = x + 0.2
  54.  
  55.     plotDistanceHistogram (x, y, 'prueba')
  56.  
  57.  
  58. if __name__ == "__main__":
  59.     main ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement