简体   繁体   中英

Matplotlib Save color from cmap

Been unable to figure this one out so was hoping someone here could point me in the right direction...

I am basically trying to store the color that was used from my colormap such that I can use it later on in the code.

color_map = cm.get_cmap('Spectral')
for grp,frame in x.groupby('time'):
   ax.scatter(x, y, cmap=color_map)
       <other code>
   ax.axvline(x=magic_number, color=<???>)
plt.show()

Pretty much I want to use the same color from my map in the for loop. I believe this is pretty simple to do but I cant seem to find the right combination of things to search for to get the answer.

I couldn't completely understand what you are trying to achieve. I'm not sure that below will be helpful.... (sadly)

your code should be something like this:

ax.axvline(x=magic_number, color=color_map(float(magic_number)/float(max_magix_number) ) )

It works quite simple float(magic_number)/float(max_magix_number) gives a float number in the range from zero to one. color_map(scaled number) returns required color as a tuple of R,G,B and transparancy....

>>> c = get_cmap('Spectral')
>>> c(0.5)
(0.998077662437524, 0.9992310649750096, 0.7460207612456747, 1.0)
>>> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM