colormaps.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import pylab as plt
  4. def sine_wave_triplets(n=8, phi=0, saturation=1.00, start=0.0, stop=1.0):
  5. x = np.linspace(start, stop, n)
  6. angle = 2 * np.pi * (x-start)/(stop - start) + phi
  7. y = 0.5 * saturation * np.sin(angle) + 0.5
  8. return np.array([x,y,y]).transpose()
  9. def saturated_tripplets(n_sin=5, phi=0, n_sat=1, rel_space=0.0):
  10. sat_list = np.linspace(0, 1, n_sat)
  11. sin_list = [sine_wave_triplets(n=n_sin,
  12. phi=phi,
  13. saturation=sat,
  14. start=float(i)/len(sat_list),
  15. stop=float(i + 1-rel_space)/len(sat_list))
  16. for i, sat in enumerate(sat_list)]
  17. if rel_space > 0:
  18. sin_list.append(np.array([1, 0.5, 0.5]))
  19. return np.vstack(sin_list)
  20. def cmap_color_circle(npoints=10):
  21. npoints = 5
  22. cdict = {'red': sine_wave_triplets(n=npoints, phi=0),
  23. 'green': sine_wave_triplets(n=npoints, phi=2*np.pi/3.),
  24. 'blue': sine_wave_triplets(n=npoints, phi=4*np.pi/3.)}
  25. my_cmap = plt.matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,256)
  26. my_cmap.set_under('black')
  27. my_cmap.set_over('white')
  28. return my_cmap
  29. def cmap_color_circle_saturation(n_sat=16, n_sin=5, rel_space=0.0):
  30. cdict = {'red': saturated_tripplets(n_sin, phi=0, n_sat=n_sat, rel_space=rel_space),
  31. 'green': saturated_tripplets(n_sin, phi=2*np.pi/3., n_sat=n_sat, rel_space=rel_space),
  32. 'blue': saturated_tripplets(n_sin, phi=4*np.pi/3., n_sat=n_sat, rel_space=rel_space),}
  33. n_colors = n_sat*n_sin
  34. if n_colors < 256:
  35. n_colors = n_sat*n_sin*(np.floor(256/n_colors)+1)
  36. my_cmap = plt.matplotlib.colors.LinearSegmentedColormap('circlesat', cdict, n_colors)
  37. my_cmap.set_under('black')
  38. my_cmap.set_over('white')
  39. return my_cmap
  40. def calc_angle(x, y, center):
  41. dx = x - center[0]
  42. dy = y - center[1]
  43. return np.arctan2(dy, dx)
  44. def point_point_distance(px, py, point):
  45. dx = px - point[0]
  46. dy = py - point[1]
  47. dist = np.sqrt(dx*dx + dy*dy)
  48. return dist
  49. def show_color_circle(cmap=plt.cm.gray, nx=100, ny=100):
  50. y, x, = np.mgrid[:ny, :nx]
  51. center = [0.5*nx, 0.5*ny]
  52. data = calc_angle(x, y, center) + np.pi
  53. dist = point_point_distance(x, y, center)
  54. rad1 = 0.3*nx
  55. rad2 = 0.4*nx
  56. data[dist < rad1] = -1
  57. data[dist > rad2] = -1
  58. data[0.4*nx:0.6*nx, 0.4*ny:0.6*ny] = 10 # square of points above range --> set_over color
  59. plt.subplot(1, 2, 1)
  60. norm = plt.matplotlib.colors.Normalize(vmin=0*np.pi, vmax=2*np.pi)
  61. im = plt.imshow(data, interpolation='nearest', cmap=cmap)
  62. im.set_norm(norm)
  63. plt.colorbar(shrink=0.5)
  64. n_sat = 50
  65. n_sin = 200
  66. rel_space=0.1
  67. sgl_sat_range = 10
  68. angle_range = (1-rel_space)*sgl_sat_range
  69. angles = calc_angle(x, y, center)
  70. distances = point_point_distance(x, y, center)
  71. distances[distances > 0.4*nx] = 0
  72. distances -= 0.2*nx
  73. middle_indices = distances < 0
  74. distances[middle_indices] = 0
  75. distances *= (n_sat-1) / np.max(distances.flat)
  76. distances = np.round(distances)
  77. angles_normed = (angles + np.pi)/(2*np.pi)
  78. combined = angle_range*angles_normed + sgl_sat_range*distances
  79. combined[middle_indices] = -1
  80. cm = cmap_color_circle_saturation(n_sat, n_sin, rel_space=rel_space)
  81. plt.subplot(1, 2, 2)
  82. plt.imshow(combined, cmap=cm)
  83. plt.clim(0, sgl_sat_range*n_sat)
  84. plt.colorbar(shrink=0.5)
  85. plt.show()
  86. def main():
  87. my_cmap = cmap_color_circle()
  88. show_color_circle(my_cmap, nx=500, ny=500)
  89. if __name__=="__main__":
  90. main()