0

Here is my idea, and I met some problem that I can't cope with.

My attempt

Creat cmap_1, cmap_2, cmap_3, ....by pre-defined color_list.

My code

color_list = ['#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99',\
              '#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a','#ffff99']    

## generate the suffix (1,2,3,4...)
letter = np.arange(1,len(site_s)+1,1)    
## Loop to generate cmap_i = colors.ListedColormap(["w",color_list[i]]) 
for i in range(0,len(letter),1):
    eval("cmap_%s" % (letter[i])) = colors.ListedColormap(["w",color_list[i]])   

But it seems that eval(xxx) doesn't work. I think eval function only can be used when cmap_1,...cmap_n are already exist.

So, here is my question in one word

How to creat a new variable cmap_i?

Add 1

  1. After creating these variable, I will plot 2-d pcolormesh with 11 2-d np-array using cmap_i in one figure.

For example

  • value is a 3-d numpy array in the shape of (11,50,50). '11' is the amount of the 2-d array.
  • value only has two number 0 & 1. So, I want to creat different two colormap correspoding to each 2-d numpy array. And then, "1" for each 2-d array has a different color. That's what I'm going to do
Code:
for i in range(0,value.shape[0],1):
    plt.pcolormesh(value[i,:,:],cmap = cmap_i) ##  cmap = cmap_i was the fake code.
  1. I have read this question("How do I do variable variables in Python?"), and I'm still confused.

Add 2
Here is what I do for now

color_list =['#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99',\
             '#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a','#ffff99']
cmap1 =cmap2=cmap3=cmap4=cmap5=cmap6=cmap7=\
cmap8=cmap9=cmap10=cmap11 = plt.cm.jet
CMAP = [cmap1 , cmap2, cmap3, cmap4, cmap5, cmap6, cmap7, 
        cmap8, cmap9, cmap10, cmap11 ]
for i in range(0,len(color_list),1):
    CMAP[i]= colors.ListedColormap([color_list[i]])
Han Zhengzu
  • 3,694
  • 7
  • 44
  • 94

0 Answers0