从文件(.h)添加新的colormap - Python

时间:2013-10-21 15:08:02

标签: python matplotlib plot color-mapping

我有一个色彩图,它被优化用于从另一个程序(ncview)以准轮廓方式显示细节,我想在python图中使用它(使用matplotlib)

色彩映射文件是.h(colormaps_manga.h),它包含在:

static int cmap_manga[] = {  47, 55,159,  43, 52,160,  33, 43,162,  21, 32,163,
11, 23,165,   5, 18,166,   2, 16,168,   0, 15,169,   0, 16,170,   0, 16,170,   0, 16,170,
 0, 16,166,   0, 16,158,   0, 15,144,   0, 13,125,   0, 12,110,   0, 12,105,   0, 13,114,
 0, 16,133,   0, 19,154,   0, 22,170,   0, 24,180,   0, 26,186,   0, 27,189,   0, 29,191,
 1, 31,193,   3, 33,194,   7, 38,195,  15, 45,196,  27, 57,197,  43, 71,199,  56, 82,200,
60, 87,201,  54, 83,202,  40, 73,203,  25, 62,204,  14, 55,205,   7, 51,206,   3, 50,207,
 2, 50,208,   2, 52,209,   2, 53,209,   2, 55,207,   2, 55,203,   2, 54,194,   2, 51,178,
 2, 48,160,   2, 44,145,   2, 45,141,   3, 50,152,   4, 58,171,   5, 67,191,   6, 74,206,
 7, 80,215,   8, 84,220,   8, 88,222,  10, 92,224,  12, 96,225,  15,101,226,  21,107,227,
31,115,228,  46,127,228,  64,139,229,  78,149,230,  82,153,231,  75,152,232,  61,148,232,
47,144,233,  38,143,234,  33,144,234,  32,147,235,  33,151,235,  35,155,236,  38,159,235,
40,161,233,  41,162,228,  42,158,217,  41,150,201,  40,139,182,  39,131,168,  41,133,165,
46,145,177,  55,164,196,  63,184,216,  71,200,229,  78,211,238,  84,219,242,  89,225,244,
94,230,245, 100,234,246, 106,238,246, 114,242,247, 125,246,247, 139,247,246, 153,248,244,
164,248,243, 169,249,242, 167,249,240, 164,249,238, 161,250,236, 162,250,234, 164,250,232,
169,251,231, 173,251,230, 178,250,229, 183,249,228, 186,247,225, 186,240,220, 181,229,209,
171,212,194, 159,192,177, 151,179,165, 153,178,165, 167,191,178, 188,210,197, 207,228,215,
222,241,229, 232,248,237, 238,252,242, 242,253,245, 245,254,247, 248,254,249, 250,254,250,
251,254,252, 253,254,253, 254,254,254, 254,254,254, 254,254,254, 254,254,254, 254,254,254,
254,254,254, 253,254,253, 252,254,251, 250,254,250, 249,254,248, 247,254,245, 245,253,242,
242,252,238, 237,248,232, 229,241,222, 215,228,207, 197,210,188, 178,191,167, 165,178,153,
165,179,151, 177,192,159, 194,212,171, 209,229,181, 220,240,186, 225,247,186, 228,249,183,
229,250,178, 230,251,173, 231,251,169, 232,250,164, 234,250,162, 236,250,161, 238,249,164,
240,249,167, 242,249,169, 243,248,164, 244,248,153, 246,247,139, 247,246,125, 247,242,114,
246,238,106, 246,234,100, 245,230, 94, 244,225, 89, 242,219, 84, 238,211, 78, 229,200, 71,
216,184, 63, 196,164, 55, 177,145, 46, 165,133, 41, 168,131, 39, 182,139, 40, 201,150, 41,
217,158, 42, 228,162, 41, 233,161, 40, 235,159, 38, 236,155, 35, 235,151, 33, 235,147, 32,
234,144, 33, 234,143, 38, 233,144, 47, 232,148, 61, 232,152, 75, 231,153, 82, 230,149, 78,
229,139, 64, 228,127, 46, 228,115, 31, 227,107, 21, 226,101, 15, 225, 96, 12, 224, 92, 10,
222, 88,  8, 220, 84,  8, 215, 80,  7, 206, 74,  6, 191, 67,  5, 171, 58,  4, 152, 50,  3,
141, 45,  2, 145, 44,  2, 160, 48,  2, 178, 51,  2, 194, 54,  2, 203, 55,  2, 207, 55,  2,
209, 53,  2, 209, 52,  2, 208, 50,  2, 207, 50,  3, 206, 51,  7, 205, 55, 14, 204, 62, 25,
203, 73, 40, 202, 83, 54, 201, 87, 60, 200, 82, 56, 199, 71, 43, 197, 57, 27, 196, 45, 15,
195, 38,  7, 194, 33,  3, 193, 31,  1, 191, 29,  0, 189, 27,  0, 186, 26,  0, 180, 24,  0,
170, 22,  0, 154, 19,  0, 133, 16,  0, 114, 13,  0, 105, 12,  0, 110, 12,  0, 125, 13,  0,
144, 15,  0, 158, 16,  0, 166, 16,  0, 170, 16,  0, 170, 16,  0, 170, 16,  0, 169, 15,  0,
168, 16,  2, 166, 18,  5, 165, 23, 11, 163, 32, 21, 162, 43, 33, 160, 52, 43, 159, 55, 47};

有没有办法导入这个?

2 个答案:

答案 0 :(得分:1)

以下是工作代码:

cmap_manga[...]
cmap_manga = np.array(cmap_manga)/255.0

ctmp = []
i = 0
while i < len(cmap_manga):
    ctmp.append([cmap_manga[i], cmap_manga[i+1], cmap_manga[i+2]])
    i += 3

mangacm = ListedColormap.ListedColormap(ctmp, name='manga', N=None)

感谢您的帮助frits =)

答案 1 :(得分:0)

可能有几种方法,但这可能有用。 重新组织您的数组并使其成为色彩映射:

cmap_manga[...]
ctmp = []
for i in range(int(len(cmap_manga)/3)):
        ctmp.append([cmap_manga[i],cmap_manga[i+1],cmap_manga[i+2]])
# Create a new colourmap from the ctmp array
mangacm = ListedColormap(ctmp,name='manga',N=None)

(我没试过......)