Guest User

Untitled

a guest
Aug 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.05 KB | None | 0 0
  1. # Copyright (c) 2017, Battelle Memorial Institute
  2. #
  3. # 1. Battelle Memorial Institute (hereinafter Battelle) hereby grants
  4. # permission to any person or entity lawfully obtaining a copy of this software
  5. # and associated documentation files (hereinafter “the Software”) to
  6. # redistribute and use the Software in source and binary forms, with or without
  7. # modification. Such person or entity may use, copy, modify, merge, publish,
  8. # distribute, sublicense, and/or sell copies of the Software, and may permit
  9. # others to do so, subject to the following conditions:
  10. #
  11. # + Redistributions of source code must retain the above copyright notice, this
  12. # list of conditions and the following disclaimers.
  13. #
  14. # + Redistributions in binary form must reproduce the above copyright notice,
  15. # this list of conditions and the following disclaimer in the documentation
  16. # and/or other materials provided with the distribution.
  17. #
  18. # + Other than as used herein, neither the name Battelle Memorial Institute or
  19. # Battelle may be used in any form whatsoever without the express written
  20. # consent of Battelle.
  21. #
  22. # 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24. # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. # ARE DISCLAIMED. IN NO EVENT SHALL BATTELLE OR CONTRIBUTORS BE LIABLE FOR ANY
  26. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  29. # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32.  
  33. import numpy as np
  34. import matplotlib.pyplot as pl
  35.  
  36. from matplotlib.colors import LinearSegmentedColormap
  37.  
  38. def convert_color_array_to_cdict(arr):
  39.  
  40. cdict = { 'red' : [],
  41. 'green' : [],
  42. 'blue' : [],
  43. 'alpha': [ (0.0, 1.0, 1.0), (1.0, 1.0, 1.0) ],
  44. }
  45.  
  46. N = arr.shape[0]
  47. X = np.linspace(0,1,N)
  48.  
  49. for x, (r,g,b) in zip(X, arr):
  50. cdict['red'].append( (x, r, r) )
  51. cdict['green'].append( (x, g, g) )
  52. cdict['blue'].append( (x, b, b) )
  53.  
  54. for k in cdict.keys():
  55. cdict[k] = tuple(cdict[k])
  56.  
  57. return cdict
  58.  
  59. cividis = np.array([
  60. [0.0000,0.1262,0.3015],
  61. [0.0000,0.1292,0.3077],
  62. [0.0000,0.1321,0.3142],
  63. [0.0000,0.1350,0.3205],
  64. [0.0000,0.1379,0.3269],
  65. [0.0000,0.1408,0.3334],
  66. [0.0000,0.1437,0.3400],
  67. [0.0000,0.1465,0.3467],
  68. [0.0000,0.1492,0.3537],
  69. [0.0000,0.1519,0.3606],
  70. [0.0000,0.1546,0.3676],
  71. [0.0000,0.1574,0.3746],
  72. [0.0000,0.1601,0.3817],
  73. [0.0000,0.1629,0.3888],
  74. [0.0000,0.1657,0.3960],
  75. [0.0000,0.1685,0.4031],
  76. [0.0000,0.1714,0.4102],
  77. [0.0000,0.1743,0.4172],
  78. [0.0000,0.1773,0.4241],
  79. [0.0000,0.1798,0.4307],
  80. [0.0000,0.1817,0.4347],
  81. [0.0000,0.1834,0.4363],
  82. [0.0000,0.1852,0.4368],
  83. [0.0000,0.1872,0.4368],
  84. [0.0000,0.1901,0.4365],
  85. [0.0000,0.1930,0.4361],
  86. [0.0000,0.1958,0.4356],
  87. [0.0000,0.1987,0.4349],
  88. [0.0000,0.2015,0.4343],
  89. [0.0000,0.2044,0.4336],
  90. [0.0000,0.2073,0.4329],
  91. [0.0055,0.2101,0.4322],
  92. [0.0236,0.2130,0.4314],
  93. [0.0416,0.2158,0.4308],
  94. [0.0576,0.2187,0.4301],
  95. [0.0710,0.2215,0.4293],
  96. [0.0827,0.2244,0.4287],
  97. [0.0932,0.2272,0.4280],
  98. [0.1030,0.2300,0.4274],
  99. [0.1120,0.2329,0.4268],
  100. [0.1204,0.2357,0.4262],
  101. [0.1283,0.2385,0.4256],
  102. [0.1359,0.2414,0.4251],
  103. [0.1431,0.2442,0.4245],
  104. [0.1500,0.2470,0.4241],
  105. [0.1566,0.2498,0.4236],
  106. [0.1630,0.2526,0.4232],
  107. [0.1692,0.2555,0.4228],
  108. [0.1752,0.2583,0.4224],
  109. [0.1811,0.2611,0.4220],
  110. [0.1868,0.2639,0.4217],
  111. [0.1923,0.2667,0.4214],
  112. [0.1977,0.2695,0.4212],
  113. [0.2030,0.2723,0.4209],
  114. [0.2082,0.2751,0.4207],
  115. [0.2133,0.2780,0.4205],
  116. [0.2183,0.2808,0.4204],
  117. [0.2232,0.2836,0.4203],
  118. [0.2281,0.2864,0.4202],
  119. [0.2328,0.2892,0.4201],
  120. [0.2375,0.2920,0.4200],
  121. [0.2421,0.2948,0.4200],
  122. [0.2466,0.2976,0.4200],
  123. [0.2511,0.3004,0.4201],
  124. [0.2556,0.3032,0.4201],
  125. [0.2599,0.3060,0.4202],
  126. [0.2643,0.3088,0.4203],
  127. [0.2686,0.3116,0.4205],
  128. [0.2728,0.3144,0.4206],
  129. [0.2770,0.3172,0.4208],
  130. [0.2811,0.3200,0.4210],
  131. [0.2853,0.3228,0.4212],
  132. [0.2894,0.3256,0.4215],
  133. [0.2934,0.3284,0.4218],
  134. [0.2974,0.3312,0.4221],
  135. [0.3014,0.3340,0.4224],
  136. [0.3054,0.3368,0.4227],
  137. [0.3093,0.3396,0.4231],
  138. [0.3132,0.3424,0.4236],
  139. [0.3170,0.3453,0.4240],
  140. [0.3209,0.3481,0.4244],
  141. [0.3247,0.3509,0.4249],
  142. [0.3285,0.3537,0.4254],
  143. [0.3323,0.3565,0.4259],
  144. [0.3361,0.3593,0.4264],
  145. [0.3398,0.3622,0.4270],
  146. [0.3435,0.3650,0.4276],
  147. [0.3472,0.3678,0.4282],
  148. [0.3509,0.3706,0.4288],
  149. [0.3546,0.3734,0.4294],
  150. [0.3582,0.3763,0.4302],
  151. [0.3619,0.3791,0.4308],
  152. [0.3655,0.3819,0.4316],
  153. [0.3691,0.3848,0.4322],
  154. [0.3727,0.3876,0.4331],
  155. [0.3763,0.3904,0.4338],
  156. [0.3798,0.3933,0.4346],
  157. [0.3834,0.3961,0.4355],
  158. [0.3869,0.3990,0.4364],
  159. [0.3905,0.4018,0.4372],
  160. [0.3940,0.4047,0.4381],
  161. [0.3975,0.4075,0.4390],
  162. [0.4010,0.4104,0.4400],
  163. [0.4045,0.4132,0.4409],
  164. [0.4080,0.4161,0.4419],
  165. [0.4114,0.4189,0.4430],
  166. [0.4149,0.4218,0.4440],
  167. [0.4183,0.4247,0.4450],
  168. [0.4218,0.4275,0.4462],
  169. [0.4252,0.4304,0.4473],
  170. [0.4286,0.4333,0.4485],
  171. [0.4320,0.4362,0.4496],
  172. [0.4354,0.4390,0.4508],
  173. [0.4388,0.4419,0.4521],
  174. [0.4422,0.4448,0.4534],
  175. [0.4456,0.4477,0.4547],
  176. [0.4489,0.4506,0.4561],
  177. [0.4523,0.4535,0.4575],
  178. [0.4556,0.4564,0.4589],
  179. [0.4589,0.4593,0.4604],
  180. [0.4622,0.4622,0.4620],
  181. [0.4656,0.4651,0.4635],
  182. [0.4689,0.4680,0.4650],
  183. [0.4722,0.4709,0.4665],
  184. [0.4756,0.4738,0.4679],
  185. [0.4790,0.4767,0.4691],
  186. [0.4825,0.4797,0.4701],
  187. [0.4861,0.4826,0.4707],
  188. [0.4897,0.4856,0.4714],
  189. [0.4934,0.4886,0.4719],
  190. [0.4971,0.4915,0.4723],
  191. [0.5008,0.4945,0.4727],
  192. [0.5045,0.4975,0.4730],
  193. [0.5083,0.5005,0.4732],
  194. [0.5121,0.5035,0.4734],
  195. [0.5158,0.5065,0.4736],
  196. [0.5196,0.5095,0.4737],
  197. [0.5234,0.5125,0.4738],
  198. [0.5272,0.5155,0.4739],
  199. [0.5310,0.5186,0.4739],
  200. [0.5349,0.5216,0.4738],
  201. [0.5387,0.5246,0.4739],
  202. [0.5425,0.5277,0.4738],
  203. [0.5464,0.5307,0.4736],
  204. [0.5502,0.5338,0.4735],
  205. [0.5541,0.5368,0.4733],
  206. [0.5579,0.5399,0.4732],
  207. [0.5618,0.5430,0.4729],
  208. [0.5657,0.5461,0.4727],
  209. [0.5696,0.5491,0.4723],
  210. [0.5735,0.5522,0.4720],
  211. [0.5774,0.5553,0.4717],
  212. [0.5813,0.5584,0.4714],
  213. [0.5852,0.5615,0.4709],
  214. [0.5892,0.5646,0.4705],
  215. [0.5931,0.5678,0.4701],
  216. [0.5970,0.5709,0.4696],
  217. [0.6010,0.5740,0.4691],
  218. [0.6050,0.5772,0.4685],
  219. [0.6089,0.5803,0.4680],
  220. [0.6129,0.5835,0.4673],
  221. [0.6168,0.5866,0.4668],
  222. [0.6208,0.5898,0.4662],
  223. [0.6248,0.5929,0.4655],
  224. [0.6288,0.5961,0.4649],
  225. [0.6328,0.5993,0.4641],
  226. [0.6368,0.6025,0.4632],
  227. [0.6408,0.6057,0.4625],
  228. [0.6449,0.6089,0.4617],
  229. [0.6489,0.6121,0.4609],
  230. [0.6529,0.6153,0.4600],
  231. [0.6570,0.6185,0.4591],
  232. [0.6610,0.6217,0.4583],
  233. [0.6651,0.6250,0.4573],
  234. [0.6691,0.6282,0.4562],
  235. [0.6732,0.6315,0.4553],
  236. [0.6773,0.6347,0.4543],
  237. [0.6813,0.6380,0.4532],
  238. [0.6854,0.6412,0.4521],
  239. [0.6895,0.6445,0.4511],
  240. [0.6936,0.6478,0.4499],
  241. [0.6977,0.6511,0.4487],
  242. [0.7018,0.6544,0.4475],
  243. [0.7060,0.6577,0.4463],
  244. [0.7101,0.6610,0.4450],
  245. [0.7142,0.6643,0.4437],
  246. [0.7184,0.6676,0.4424],
  247. [0.7225,0.6710,0.4409],
  248. [0.7267,0.6743,0.4396],
  249. [0.7308,0.6776,0.4382],
  250. [0.7350,0.6810,0.4368],
  251. [0.7392,0.6844,0.4352],
  252. [0.7434,0.6877,0.4338],
  253. [0.7476,0.6911,0.4322],
  254. [0.7518,0.6945,0.4307],
  255. [0.7560,0.6979,0.4290],
  256. [0.7602,0.7013,0.4273],
  257. [0.7644,0.7047,0.4258],
  258. [0.7686,0.7081,0.4241],
  259. [0.7729,0.7115,0.4223],
  260. [0.7771,0.7150,0.4205],
  261. [0.7814,0.7184,0.4188],
  262. [0.7856,0.7218,0.4168],
  263. [0.7899,0.7253,0.4150],
  264. [0.7942,0.7288,0.4129],
  265. [0.7985,0.7322,0.4111],
  266. [0.8027,0.7357,0.4090],
  267. [0.8070,0.7392,0.4070],
  268. [0.8114,0.7427,0.4049],
  269. [0.8157,0.7462,0.4028],
  270. [0.8200,0.7497,0.4007],
  271. [0.8243,0.7532,0.3984],
  272. [0.8287,0.7568,0.3961],
  273. [0.8330,0.7603,0.3938],
  274. [0.8374,0.7639,0.3915],
  275. [0.8417,0.7674,0.3892],
  276. [0.8461,0.7710,0.3869],
  277. [0.8505,0.7745,0.3843],
  278. [0.8548,0.7781,0.3818],
  279. [0.8592,0.7817,0.3793],
  280. [0.8636,0.7853,0.3766],
  281. [0.8681,0.7889,0.3739],
  282. [0.8725,0.7926,0.3712],
  283. [0.8769,0.7962,0.3684],
  284. [0.8813,0.7998,0.3657],
  285. [0.8858,0.8035,0.3627],
  286. [0.8902,0.8071,0.3599],
  287. [0.8947,0.8108,0.3569],
  288. [0.8992,0.8145,0.3538],
  289. [0.9037,0.8182,0.3507],
  290. [0.9082,0.8219,0.3474],
  291. [0.9127,0.8256,0.3442],
  292. [0.9172,0.8293,0.3409],
  293. [0.9217,0.8330,0.3374],
  294. [0.9262,0.8367,0.3340],
  295. [0.9308,0.8405,0.3306],
  296. [0.9353,0.8442,0.3268],
  297. [0.9399,0.8480,0.3232],
  298. [0.9444,0.8518,0.3195],
  299. [0.9490,0.8556,0.3155],
  300. [0.9536,0.8593,0.3116],
  301. [0.9582,0.8632,0.3076],
  302. [0.9628,0.8670,0.3034],
  303. [0.9674,0.8708,0.2990],
  304. [0.9721,0.8746,0.2947],
  305. [0.9767,0.8785,0.2901],
  306. [0.9814,0.8823,0.2856],
  307. [0.9860,0.8862,0.2807],
  308. [0.9907,0.8901,0.2759],
  309. [0.9954,0.8940,0.2708],
  310. [1.0000,0.8979,0.2655],
  311. [1.0000,0.9018,0.2600],
  312. [1.0000,0.9057,0.2593],
  313. [1.0000,0.9094,0.2634],
  314. [1.0000,0.9131,0.2680],
  315. [1.0000,0.9169,0.2731],
  316. ])
  317.  
  318. cividis_cmap = convert_color_array_to_cdict(cividis)
  319. cividis_linear_segmented = LinearSegmentedColormap('cividis', cividis_cmap)
  320.  
  321. pl.register_cmap(cmap=cividis_linear_segmented)
  322.  
  323. def get_cividis_colors(N, n_start=0, n_end=255):
  324. # get a list of colors
  325. indices = np.linspace(n_start, n_end, N)
  326. indices = np.array(indices, dtype=int)
  327. return cividis[indices]
  328.  
  329. if __name__=="__main__":
  330. print(get_cividis_colors(12))
  331.  
  332. x = np.arange(0, 2*np.pi, 0.01)
  333. y = np.arange(0, 2*np.pi, 0.01)
  334. X, Y = np.meshgrid(x,y)
  335. Z = np.cos(X) * np.sin(Y) * 20
  336.  
  337. pl.imshow(Z,cmap='cividis')
  338. pl.colorbar()
  339.  
  340. pl.show()
Add Comment
Please, Sign In to add comment