Generating user-defined colormap in Python matplotlib - python

Here is my question.
When I want use a lot of colormap, I could use
CMAP = ["summer_r", "brg_r", "Dark2", "prism", "PuOr_r", "afmhot_r", "terrain_r", "PuBuGn_r", "RdPu", \
"gist_ncar_r", "gist_yarg_r", "Dark2_r", "YlGnBu", "RdYlBu", "hot_r"]
## value was a 3-d array, the first dimension represent the amount of 2-d array with the value (0, 1).
## I just plot the value 1 for each value[i,:,:]
for i in range(0,len(CMAP),1):
plt.pcolor(xx,yy,value[i,:,:], cmap = CMAP[i])
And I can get this:
http://i8.tietuku.com/cdcdcd5f539c124b.png
But I can't clearly realize the each grid's color befor generating the figure.
Because some colormap which I add in CMAP may have the same start color. SO, some value[ i, :, :] grids will be hard to distinguish.
My idea
Using one colormap instead and split into single color for each value[ i, :, :]. So, each value grid has a different color.
For example:
## 1. cut the colormap, take "jet" for example
cMap = plt.cm.get_cmap("jet",lut=6)
http://i4.tietuku.com/be127c44e87a03fc.png
## 2. I havn't figured it out
## This is the fake code
CMAP = Func[one color -> colormap](cMap)
Update -2016-01-18
This is my code to set different cmap and loop, but it was a bit of rigid.
cmap1 = colors.ListedColormap(["w",'red'])
cmap2 = colors.ListedColormap(["w",'blue'])
cmap3 = colors.ListedColormap(["w",'yellow'])
CMAP = [cmap1,cmap2,cmap3]
Then, I can cope with my original attempt.
But I was wondering is there a smart way to generate the cmap1,cmap2,......?

The hard part of this is coming up with N distinctive colors. In practice, it's usually easiest to just grab random colors as long as N is small. If you'd prefer a bit nicer way of getting N distinct colors, have a look at how seaborn's husl_palette and hsl_palette are implemented. They choose N evenly spaced colors in HSL/HUSL space and convert it back to RGB.
At any rate, there are two parts to tying specific values to specific colors in matplotlib. One is the colormap and the other is the norm. The Normalize instance (the norm) handles transforming the data ranges into a 0-1 space for the colormap.
There's a function to make this use-case easier:matplotlib.colors.from_levels_and_colors. It returns a cmap and norm instance that you can pass in to imshow/pcolormesh/scatter/etc.
As a stand-alone example, let's generate data with a random number of unique integer values. We'll use random pastel colors instead of trying to do something fancy.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import from_levels_and_colors
nvals = np.random.randint(2, 20)
data = np.random.randint(0, nvals, (10, 10))
colors = np.random.random((nvals, 3))
# Make the colors pastels...
colors = colors / 2.5 + 0.55
levels = np.arange(nvals + 1) - 0.5
cmap, norm = from_levels_and_colors(levels, colors)
fig, ax = plt.subplots()
im = ax.imshow(data, interpolation='nearest', cmap=cmap, norm=norm)
fig.colorbar(im, ticks=np.arange(nvals))
plt.show()
Not the nicest looking color palette, but it's not awful. Here's another run:
Even with 17 values, we're still getting fairly distinct colors by choosing random values.

Related

Discrete pyplot scatter colobar

I am creating a scatterplot with a colorbar
plt.scatter(X, Y, c=Z)
plt.colorbar()
plt.show()
plt.close()
where X and Y are float arrays and Z is an integer array.
Even though Z is an integer array (here 1-14), the colorbar displays floats.
How can I display a discrete colorbar 1-14?
I found something attempting to answer a similar question here, but I don't understand the answer (containing some complications to make 0 be gray) well enough to apply it.
Check out the second answer to your linked question. If you discretize your colourmap before calling scatter, it will automatically work as you want it to:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
n = 14
X = np.random.rand(20)
Y = np.random.rand(20)
Z = np.random.randint(low=0,high=n,size=X.shape)
plt.figure()
plt.scatter(X,Y,c=Z,cmap=cm.hot)
plt.colorbar()
plt.figure()
plt.scatter(X,Y,c=Z,cmap=cm.get_cmap('hot',n))
plt.colorbar()
Results for comparison:
Note that the default colourmap is jet. But only until viridis kicks in starting from version 2.0 as the new (and wonderful) default.
If what's bothering you is that the numbers are floating-point on the colourbar, you can set manual ticks in it, irrespective of the discretization of colours:
plt.figure()
plt.scatter(X,Y,c=Z,cmap=cm.jet)
plt.colorbar(ticks=np.unique(Z))
#or
#plt.colorbar(ticks=range(Z.min(),Z.max()+1))
Result:
Note that since I used a few random-generated points, not every number is present in Z, so unique might not be the best approach (see the missing ticks in the above figure). This is why I also added a solution based on min/max. You can tailor the limits to your needs depending on your actual application.
Here is my discrete colorbar for land use type, it seems like your work,because the Z value is also an interger array from 1-14.
My method
creat the colormap and colorbar label manually learned from here
My Code
cMap = ListedColormap(['white', '#8dd3c7','#ffffb3','#bebada', \
'#b2182b','#80b1d3','#fdb462','#b3de69','#6a3d9a',\
'#b2df8a', '#1f78b4', '#ccebc5','#ffed6f'])
## If you want to use the colormap from plt.cm..., you can use(take 'jet' for example)
cMap = plt.cm.get_cmap("jet",lut=13)
### here you can change your data in
lulc = plt.pcolormesh(lulc,cmap = cMap,alpha = 0.7)
z_range = np.linspace(1,14,14)
list = z_range.astype('S10')
k = -0.05
for i in range(0,13,1):
k = k + 1/13.0
ax.annotate(list[i],xycoords='axes fraction',xy=(1.12,k),fontsize = 14, \
fontstyle = 'italic',zorder =3)
cbar = plt.colorbar(lulc,ticks = [ ])
for label in cbar.ax.yaxis.get_ticklabels()[::-1]:
label.set_visible(False)
My result
(source: tietuku.com)
Wish it can help!

Use a variable to set alpha opacity in a colormap

I would like to plot the 2 output variables, say map1 and map2, as a function of 2 input variables, say x and y using colormaps. So as to do so, I want to represent map1 using a color scale while map2 would rely on a transparency scale. Yet, the alpha option cannot take an np.array as an argument and the following code is doomed to failure.
fig=plt.figure(num=None, figsize=(21,12), dpi=80, facecolor='w', edgecolor='k')
ax1=plt.subplot(211)
im = ax1.pcolor(map1, cmap='Spectral_r', alpha=map2)
fig.colorbar(im)
Would anybody see a way to do this? I don't want to use another overlapped color scale and really want map2 to be represented with a transparency function so as the visibility of a background grid for instance would tell the reader the amplitude of map2.
You could do this with pcolormesh, and set the alpha for the faces of the QuadMesh afterwards. For example:
import numpy as np
import matplotlib.pyplot as plt
fig,ax = plt.subplots(1)
ax.set_aspect('equal')
# The data array
m1 = np.random.rand(5,5)
# The alpha array. Normalize your map2 to the range 0,1
m2 = np.linspace(0,1,25).reshape(5,5)
p = ax.pcolormesh(m1)
plt.savefig('myfig.png') # or fig.canvas.draw()
for i,j in zip(p.get_facecolors(),m2.flatten()):
i[3] = j # Set the alpha value of the RGBA tuple using m2
plt.savefig('myfig.png')
Note: you seem to have to save the figure (or plt.show() or fig.canvas.draw()) after the pcolormesh command, to generate the p.get_facecolors array; that's why I save the figure twice. There is probably a more elegant solution to that, but I can't think of it off the top of my head. Here's the output; notice the alpha increase from the bottom left towards the top right:

Select starting color in matplotlib colormap

I have the figure shown below. Presently the figure's colorscheme uses the entire range of the colormap (mpl.cm.Paired). What I want to do, and have been unable to figure out, is how to limit matplotlib to use only a subset of the colormap. In this case I am trying to get the starting color to be a darker shade of blue. Here's the plotting section of my code:
Figure = plt.figure(figsize=(22,10))
Map = Basemap(projection='robin', lon_0=0, resolution='l')
x, y = Map(LONS, LATS)
levels = np.arange(0, 4100, 100)
fcp = Map.contourf(x, y, data, levels, interpolation="bicubic", cmap=mpl.cm.Paired)
cb = Map.colorbar(fcp, "bottom", size="5%", pad='5%', extendrect=False)
cb.ax.tick_params(labelsize=18)
cb.solids.set_edgecolor("face")
cb.set_label("metres",fontsize=18)
cb.ax.set_aspect(0.047)
Map.drawcoastlines(linewidth=1)
Map.drawmapboundary(linewidth=1)
Map.drawmeridians([-150,-100,-50,0,50,100, 150],labels=[1,1,1,0],fontsize=18)
Map.drawparallels([-60,-30,0,30,60],labels=[1,1,1,1],fontsize=18)
One way to do this would be to call the function mpl.cm.Paired() for a subset of the normalised range (i.e., [0-1]) and then use the list of colors that it returns to define a new colormap:
import matplotlib.colors as mcol
lvTmp = np.linspace(0.1,1.0,len(levels)-1)
cmTmp = mlp.cm.Paired(lvTmp)
newCmap = mcol.ListedColormap(cmTmp)
You'll need to fiddle about with the 0.1 value in that linspace to get the start color that you want from the built in colormap.

plotting coordinate as a matrix matplotlib python

I have a set of coordinates, say [(2,3),(45,4),(3,65)]
I need to plot them as a matrix is there anyway I can do this in matplotlib so I want it to have this sort of look http://imgur.com/Q6LLhmk
Edit: My original answer used ax.scatter. There is a problem with this: If two points are side-by-side, ax.scatter may draw them with a bit of space in between, depending on the scale:
For example, with
data = np.array([(2,3),(3,3)])
Here is a zoomed-in detail:
So here is a alternative solution that fixes this problem:
import matplotlib.pyplot as plt
import numpy as np
data = np.array([(2,3),(3,3),(45,4),(3,65)])
N = data.max() + 5
# color the background white (1 is white)
arr = np.ones((N,N), dtype = 'bool')
# color the dots black (0)
arr[data[:,1], data[:,0]] = 0
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.imshow(arr, interpolation='nearest', cmap = 'gray')
ax.invert_yaxis()
# ax.axis('off')
plt.show()
No matter how much you zoom in, the adjacent squares at (2,3) and (3,3) will remain side-by-side.
Unfortunately, unlike ax.scatter, using ax.imshow requires building an N x N array, so it could be more memory-intensive than using ax.scatter. That should not be a problem unless data contains very large numbers, however.

Displaying multiple masks in different colours in pylab

I've an array that includes decent observations, irrelevant observations (that I would like to mask out), and areas where there are no observations (that i would also like to mask out). I want to display this array as an image (using pylab.imshow) with two separate masks, where each mask is shown in a different colour.
I've found code for a single mask (here) in a certain colour, but nothing for two different masks:
masked_array = np.ma.array (a, mask=np.isnan(a))
cmap = matplotlib.cm.jet
cmap.set_bad('w',1.)
ax.imshow(masked_array, interpolation='nearest', cmap=cmap)
If possible, I'd like to avoid having to use a heavily distorted colour map but accept that that is an option.
You might simply replace values in you array with some fixed value depending on some conditions. For example, if you want to mask elements larger than 1 and smaller than -1:
val1, val2 = 0.5, 1
a[a<-1]= val1
a[a>1] = val2
ax.imshow(a, interpolation='nearest')
val1 and val2 can be modified to obtain colors you wish.
You can also set the colors explicitly, but it requires more work:
import matplotlib.pyplot as plt
from matplotlib import colors, cm
a = np.random.randn(10,10)
norm = colors.normalize()
cmap = cm.hsv
a_colors = cmap(norm(a))
col1 = colors.colorConverter.to_rgba('w')
col2 = colors.colorConverter.to_rgba('k')
a_colors[a<-0.1,:] = col1
a_colors[a>0.1,:] = col2
plt.imshow(a_colors, interpolation='nearest')
plt.show()
For me the simplest way is plotting directly the masks with imshow, passing different colormaps. Max and min of a colormap are used for True and False values:
mask1=np.isnan(a)
mask2=np.logical_not(mask1)
plt.imshow(mask1,cmap='gray')
plt.imshow(mask2,cmap='rainbow')
However this (and other approaches suggested) plot also False values overplotting previous plots. If you want to avoid plotting False values, it can be done by replacing them with np.nan, after converting the array to float (np.nan is of type float and cannot be contained in a boolean mask). nan values are not plotted:
mmm=mask.astype(np.float)
mmm[np.where(mmm==0)]=np.nan
#the substitution can be done also in one line with:
#mmm=np.where(mask,mask.astype(np.float),np.nan)
plt.imshow(mmm,cmap='rainbow',vmin=0,vmax=1)) #will use only the top color: red. vmin and vmax are needed if there are only one value (1.0=True) in the array.
plt.colorbar()
#repeat for other masks...
And i hope I am not going too much off topic, but same technique can be used to plot different part of the data with different colormaps, by replacing the plotting command with:
plt.imshow(mmm*data,cmap='rainbow')
In order to color some pixels red and others green, as appears in the the following image, I used the code below. (See code comments for details.)
import numpy as np #Used for holding and manipulating data
import numpy.random #Used to generate random data
import matplotlib as mpl #Used for controlling color
import matplotlib.colors #Used for controlling color as well
import matplotlib.pyplot as plt #Use for plotting
#Generate random data
a = np.random.random(size=(10,10))
#This 30% of the data will be red
am1 = a<0.3 #Find data to colour special
am1 = np.ma.masked_where(am1 == False, am1) #Mask the data we are not colouring
#This 10% of the data will be green
am2 = np.logical_and(a>=0.3,a<0.4) #Find data to colour special
am2 = np.ma.masked_where(am2 == False, am2) #Mask the data we are not colouring
#Colourmaps for each special colour to place. The left-hand colour (black) is
#not used because all black pixels are masked. The right-hand colour (red or
#green) is used because it represents the highest z-value of the mask matrices
cm1 = mpl.colors.ListedColormap(['black','red'])
cm2 = mpl.colors.ListedColormap(['black','green'])
fig = plt.figure() #Make a new figure
ax = fig.add_subplot(111) #Add subplot to that figure, get ax
#Plot the original data. We'll overlay the specially-coloured data
ax.imshow(a, aspect='auto', cmap='Greys', vmin=0, vmax=1)
#Plot the first mask. Values we wanted to colour (`a<0.3`) are masked, so they
#do not show up. The values that do show up are coloured using the `cm1` colour
#map. Since the range is constrained to `vmin=0, vmax=1` and a value of
#`cm2==True` corresponds to a 1, the top value of `cm1` is applied to all such
#pixels, thereby colouring them red.
ax.imshow(am1, aspect='auto', cmap=cm1, vmin=0, vmax=1);
ax.imshow(am2, aspect='auto', cmap=cm2, vmin=0, vmax=1);
plt.show()
I don't know what the values are in your array, but you could convert the masked areas so that the X values become RGB(A) values (tuples of (R,G,B,A)), in which case the cmap is ignored, according to the documentation at least.
• cmap: [ None | Colormap ]
A matplotlib.colors.Colormap instance, eg. cm.jet. If None, default to
rc image.cmap value. cmap is ignored when X has RGB(A) information

Categories