How to implement python matplotlib heatmap colorbar like this?
plt.imshow(a,aspect='auto', cmap=plt.cm.gist_rainbow_r)
plt.colorbar()
This example from the matplotlib gallery shows some different ways to make custom colormaps, including transparency: https://matplotlib.org/examples/pylab_examples/custom_cmap.html
In your case, it looks like you want a modified version of the gist_rainbow colormap. You can achieve this by modifying the alpha channel as follows:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
# get colormap
ncolors = 256
color_array = plt.get_cmap('gist_rainbow')(range(ncolors))
# change alpha values
color_array[:,-1] = np.linspace(1.0,0.0,ncolors)
# create a colormap object
map_object = LinearSegmentedColormap.from_list(name='rainbow_alpha',colors=color_array)
# register this new colormap with matplotlib
plt.register_cmap(cmap=map_object)
# show some example data
f,ax = plt.subplots()
h = ax.imshow(np.random.rand(100,100),cmap='rainbow_alpha')
plt.colorbar(mappable=h)
Related
How do I set the color cycler of one matplotlib.pyplot.axes object (or a figure alternatively) to a predefined colomap without changing the default as plt.set_cmap would do?
Say I'd like to use the 'tab20' colormap in one plot, using the following code:
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
# something like ax.set_cmap('tab20')
ax.scatter([1,2,3],[2,1,3])
ax.scatter([1,2,3],[3,2,1])
I found a solution, the following does the trick:
from matplotlib import pyplot as plt
from cycler import cycler
import numpy as np
fig, ax = plt.subplots()
ax.set_prop_cycle(cycler('color', plt.cm.tab20.colors))
ax.scatter([1,2,3],[2,1,3])
ax.scatter([1,2,3],[3,2,1])
It creates a color cycle with the colors from a colormap and assigns it to the axes object.
To call plt.colorbar I need a "mappable", which I usually create by plt.imshow or plt.contour. Is there a "reasonable" way to create a mappable without these?
More specifically, my code is as follows:
import matplotlib.pyplot as plt
from matplotlib.cm import viridis
colors = viridis(np.linspace(0,1,10))
for i, col in enumerate(colors):
plt.plot(i, 'o', color = col)
I would then like to call plt.colorbar, but I don't have a mappable.
My usual work around is cmap = plt.scatter(np.linspace(0,1), np.full(50, np.nan), c = np.linspace(0,1)), which works perfectly well, but I find utterly ugly.
In that case, you can use scatter not only for generating the mappable, but doing all the job at once (plotting the dots, and returning the corresponding mappable).
EDIT
In general, Matplotlib always provides a way to make a collection of objects (see for instance the LineCollection usage example) allowing to plot a collection of lines (or any other object) with varying properties like color, line width, etc.
import matplotlib.pyplot as plt
import numpy as np
colors = np.linspace(0,1,10)
mappable = plt.scatter(np.zeros(10), colors, s=30, c=colors, cmap='viridis')
plt.colorbar(mappable)
plt.show()
Which produce the following image
From matplotlib doc, colorbar accepts a ColorMappable object. So we can create such an object with ScalarMappable and Normalize:
from matplotlib.cm import ScalarMappable
from matplotlib.colors import Normalize
colors = viridis(np.linspace(0,1,10))
for i, col in enumerate(colors):
plt.plot(i, 'o', color = col)
cmappable = ScalarMappable(Normalize(0,1))
plt.colorbar(cmappable)
Output:
I want to create a colormap like heatmap but the colors are defined as a RGB values in a data.
Each cell contains specific color value which are needed to be plotted in the image.
I want to plot these values that looks like similar to this:
How to generate this kind of colormap without using matplotlib tables.
I have accomplished this using matplotlib table by taking reference from here:
matplotlib table color
But I want to implement this without using tables. Is these any method other than using matplotlib tables.
It wasn't a color bar, was it? If you can create an RGB relationship with the data, you can express it. I set 'c=colors', but I think it would be better to specify a column with RGB converted to hexadecimal.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors
data = [np.random.randn(50),np.random.randn(50),np.random.randint(1,5,(50,))]
fig = plt.figure(figsize=(10,7))
# RGB:[[100,125,200],[100,125,100],[100.25.50],[122,125,10],[100,25,201]]
# hex=(['#647dc8','#647d64','#641932','#7a7d0a'])
colors = ["#647dc8","#647d64","#641932","#7a7d0a","#ff19c9"]*10
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", colors)
im = plt.scatter(data[0], data[1], c=colors, linewidths=5, alpha=1.0)
plt.show()
I have an array in python, using matplotlib, with floats ranging between 0 and 1.
I am displaying this array with imshow, I am trying to create a custom cmap, which is identical to Greens, however when a cell becomes 0 I would like to be able to map that value to red, and leave the rest of he spectrum unchanged.
If anyone more familiar with matplotlib would be able to help me I would greatly appreciate it!
For instance how would I edit this script so that the zero value in the matrix showed as red?
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
x = np.array([[0,1,2],[3,4,5],[6,7,8]])
fig = plt.figure()
cmap_custom = matplotlib.cm.Greens
plt.imshow( x, interpolation='nearest' ,cmap = cmap_custom)
plt.colorbar()
plt.show()
The colormaps in matplotlib allow you to set special colors for values that are outside of the defined range. In your case specify the color for values below the defined range with cmap_custom.set_under('r').
Then you also need to specify the lower end of the range: vmin=0.01 (just some value > 0).
Finally create the colorbar with plt.colorbar(extend='min').
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
x = np.array([[0,1,2],[3,4,5],[6,7,8]])
fig = plt.figure()
cmap_custom = matplotlib.cm.Greens
cmap_custom.set_under('r')
plt.imshow( x, interpolation='nearest' ,cmap = cmap_custom, vmin=0.01)
plt.colorbar(extend='min')
plt.show()
I am trying to replace the colorbar given by "hp.mollview" with a custom one. In particular I am interested in:
Rotating the colorbar by 90 degrees (i.e. replacing the horizontal by a vertical one)
Using two labels (left and right of the colorbar)
Setting custom ticks
Indicating that the range is set (via the "max" parameter) by setting "cmap.set_over".
Minimal amount of code:
import numpy as np
import healpy as hp
m = np.arange(hp.nside2npix(32))
hp.mollview(m)
Any help?
I'll expand my comment here:
import numpy as np
import healpy as hp
import matplotlib.pyplot as plt
m = np.arange(hp.nside2npix(32))
hp.mollview(m, cbar=None)
fig = plt.gcf()
ax = plt.gca()
image = ax.get_images()[0]
cmap = fig.colorbar(image, ax=ax)
Then you can customize the colorbar with the function arguments.