You can do this with the code below, and the code in your question was actually very close to what you needed, all you have to do is call the cmap object you have.
import matplotlib
cmap = matplotlib.cm.get_cmap('Spectral')
rgba = cmap(0.5)
print(rgba) # (0.99807766255210428, 0.99923106502084169, 0.74602077638401709, 1.0)
For values outside of the range [0.0, 1.0] it will return the under and over colour (respectively). This, by default, is the minimum and maximum colour within the range (so 0.0 and 1.0). This default can be changed with cmap.set_under() and cmap.set_over().
For "special" numbers such as np.nan and np.inf the default is to use the 0.0 value, this can be changed using cmap.set_bad() similarly to under and over as above.
Finally it may be necessary for you to normalize your data such that it conforms to the range [0.0, 1.0]. This can be done using matplotlib.colors.Normalize simply as shown in the small example below where the arguments vmin and vmax describe what numbers should be mapped to 0.0 and 1.0 respectively.
import matplotlib
norm = matplotlib.colors.Normalize(vmin=10.0, vmax=20.0)
print(norm(15.0)) # 0.5
A logarithmic normaliser (matplotlib.colors.LogNorm) is also available for data ranges with a large range of values.
(Thanks to both Joe Kington and tcaswell for suggestions on how to improve the answer.)
Answer from Ffisegydd on Stack OverflowYou can do this with the code below, and the code in your question was actually very close to what you needed, all you have to do is call the cmap object you have.
import matplotlib
cmap = matplotlib.cm.get_cmap('Spectral')
rgba = cmap(0.5)
print(rgba) # (0.99807766255210428, 0.99923106502084169, 0.74602077638401709, 1.0)
For values outside of the range [0.0, 1.0] it will return the under and over colour (respectively). This, by default, is the minimum and maximum colour within the range (so 0.0 and 1.0). This default can be changed with cmap.set_under() and cmap.set_over().
For "special" numbers such as np.nan and np.inf the default is to use the 0.0 value, this can be changed using cmap.set_bad() similarly to under and over as above.
Finally it may be necessary for you to normalize your data such that it conforms to the range [0.0, 1.0]. This can be done using matplotlib.colors.Normalize simply as shown in the small example below where the arguments vmin and vmax describe what numbers should be mapped to 0.0 and 1.0 respectively.
import matplotlib
norm = matplotlib.colors.Normalize(vmin=10.0, vmax=20.0)
print(norm(15.0)) # 0.5
A logarithmic normaliser (matplotlib.colors.LogNorm) is also available for data ranges with a large range of values.
(Thanks to both Joe Kington and tcaswell for suggestions on how to improve the answer.)
In order to get rgba integer value instead of float value, we can do
rgba = cmap(0.5,bytes=True)
So to simplify the code based on answer from Ffisegydd, the code would be like this:
#import colormap
from matplotlib import cm
#normalize item number values to colormap
norm = matplotlib.colors.Normalize(vmin=0, vmax=1000)
#colormap possible values = viridis, jet, spectral
rgba_color = cm.jet(norm(400),bytes=True)
#400 is one of value between 0 and 1000
python - How to map number to color using matplotlib's colormap? - Stack Overflow
How does color mapping in Matplotlib work?
Colormaps explained courtesy of matplotlib. You spend a lot of time on your simulations, make sure you spend a little bit of time selecting the correct colormap for your figures.
Legend in matplotlib scatterplot doesn't match colormap of the plot
It's as simple as cm.hot(0.3):
import matplotlib.cm as cm
print(cm.hot(0.3))
(0.8240081481370484, 0.0, 0.0, 1.0)
If you also want to have the normalizer, use
import matplotlib as mpl
import matplotlib.cm as cm
norm = mpl.colors.Normalize(vmin=-20, vmax=10)
cmap = cm.hot
x = 0.3
m = cm.ScalarMappable(norm=norm, cmap=cmap)
print(m.to_rgba(x))
(1.0, 0.8225486412996345, 0.0, 1.0)
You can get a color from a colormap by supplying an argument between 0 and 1, e.g. cm.autumn(0.5).
If there is a normalization instance in the game, use the return of the Normalization instead:
import matplotlib.cm as cm
from matplotlib.colors import Normalize
cmap = cm.autumn
norm = Normalize(vmin=-20, vmax=10)
print cmap(norm(5))
I'm still a beginner therefore don't want to be getting too deep into this subject, but can someone explain what's happening with c=point_numbers, cmap=plt.get_cmap("Reds") in the below code?
Normally "c=" is used to assign a color, which makes perfect sense as that's what even documentation says, but why in the example below we can assign an array of numbers to "c=" and it does't throw an error, since array is not a color obviously?
Help on clearing this up would be much appreciated as I've spent quite few hours yesterday and today trying to look it up...
import matplotlib.pyplot as plt
from random import choice
class RandomWalk:
'''A class to generate random walks'''
def __init__(self, num_points=5000):
'''Initialize attributes of walk'''
self.num_points = num_points
#All walks start from (0, 0).
self.x_values = [0]
self.y_values = [0]
#Keep taking steps until the walk reaches the desired length (5000)
while len(self.x_values) < self.num_points:
#Decide in which direction and how far to go
x_direction = choice([-1, 1])
x_distance = choice([0, 1, 2, 3, 4])
x_step = x_direction * x_distance
y_direction = choice([-1, 1])
y_distance = choice([0, 1, 2, 3, 4])
y_step = y_direction * y_distance
#Reject moves that go nowhere
if x_step == 0 and y_step == 0:
continue
#Calculate new position
x = self.x_values[-1] + x_step
y = self.y_values[-1] + y_step
self.x_values.append(x)
self.y_values.append(y)
my_first_rw = RandomWalk()
plt.style.use("classic")
fig, ax = plt.subplots()
point_numbers = range(my_first_rw.num_points)
ax.scatter(my_first_rw.x_values, my_first_rw.y_values,c=point_numbers, cmap=plt.get_cmap("Reds"), edgecolors="none", s=5)
plt.show()