user3012486 user3012486 - 1 year ago 133
Python Question

Convert given value of contourf color to rgb code

I created a 2D contourf plot with viridis color scheme in bounds vmin=0,vmax=4. Here is the code:


Now I have from another calculation a data point at a specific location with data=3.5 and want to superimpose this to the contour using a colored circle patch.

So how can I add the rgb color information to the circle within the range and used colorscheme of my contourf

Thanks for help

tom tom
Answer Source

you can use a matplotlib.colors.Normalize instance to do this:

import matplotlib.pyplot as plt
import matplotlib.colors as colors

norm = colors.Normalize(vmin=0,vmax=4)
cmap =

mycolor = cmap(norm(3.5))
print mycolor
# (0.67848900000000001, 0.86374200000000001, 0.189503, 1.0)

Note, if the colormap has been imported from a different file, then it will need to be registered with matplotlib first before this works. For example:

import myviridisfile as vs
plt.register_cmap(name='viridis', cmap=vs.viridis)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download