antennaeKDE.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """Plots the Antennae using Kernel Density Estimation to map the density"""
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from scipy.stats import gaussian_kde
  5. from analysis.colormaps import red_map
  6. from analysis import utils
  7. def plotKDE(i, theta, phi, view):
  8. """Plot the Antennae galaxies using kernel density estimation
  9. Parameters:
  10. i (int): Timestep at which to plot them
  11. theta (float): polar angle
  12. phi (float): azimuthal angle
  13. view (float): rotation along the line of sight
  14. """
  15. data = utils.loadData('antennae', 13500)
  16. r_vec = data['r_vec'][::]
  17. # Rotate to view from (theta, phi, view) viewpoint
  18. M1 = np.array([[1, 0, 0],
  19. [0, np.cos(theta), np.sin(theta)],
  20. [0, -np.sin(theta), np.cos(theta)]])
  21. M2 = np.array([[np.cos(phi), np.sin(phi), 0],
  22. [-np.sin(phi), np.cos(phi), 0],
  23. [0, 0, 1]])
  24. M3 = np.array([[np.cos(view), np.sin(view), 0],
  25. [-np.sin(view), np.cos(view), 0],
  26. [0, 0, 1]])
  27. M = np.matmul(M2, np.matmul(M1, M3))
  28. r_vec = np.tensordot(r_vec, M, axes=[1,0])
  29. # Plotting
  30. plt.figure(figsize=(4,5), dpi=400)
  31. # Perform Kernel Density Estimation to colour by density
  32. xy = np.vstack([r_vec[:,0],r_vec[:,1]])
  33. c = gaussian_kde(xy)(xy)
  34. # Densest points are plotted last
  35. idx = c.argsort()
  36. x, y, c = r_vec[:,0][idx], r_vec[:,1][idx], c[idx]
  37. plt.scatter(x, y, c=c, s=3, edgecolor='', cmap=red_map)
  38. plt.axis('square')
  39. plt.show()
  40. plotKDE(135, 1.86, 4.10, 5.10)