How to set your own choice colors in a contour plot?

1,078 views
Skip to first unread message

Bernd Becker

unread,
Dec 11, 2012, 5:33:31 AM12/11/12
to scitoo...@googlegroups.com
# plotting your own contour levels and above and below ranges:

levels=[240,250,260,270,280,285,290,295,300,305]

# choose your own colors from a predefined Brewer color table                 <=== How do you do that?

sketch of idea:
  -- have color chart hanging on the wall in your office, choose colours by reference number from table:

colors=[ 2, 5, 8, 12,  15, 0 , 21 , 24, 27 , 19, 32 ]

# use user defined colors and contours in filled contour plot:

qplt.contourf(t0+273.15,levels=levels,cmap=brewer_cmap,extend='both')
plt.title('near surface temperature')

Thanks for all your suggestions!
Bernd

Ian Edwards

unread,
Dec 11, 2012, 5:52:23 AM12/11/12
to scitoo...@googlegroups.com
Hi Bernd,

I've written a small function to extract colours from a palette given the indices that you are interested in.  There's also a small example that creates a plot with a Brewer colour palette (i.e. the color chart hanging on the office wall) and then a version where only a subset of the colours have been chosen.  Beware there is no error checking in the script...


import matplotlib.colors as mpl_colors
import matplotlib.cm as mpl_cm
import matplotlib.pyplot as plt
import iris
import iris.quickplot as qplt
 
def subset_colormap(name, colormap, indices):
    colors
= []
   
for i in indices:
        rgba
= colormap(i/float(colormap.N))
        colors
.append(rgba)
   
return mpl_colors.LinearSegmentedColormap.from_list(name, colors, N=len(colors))
 
def example():
    brewer_cmap
= mpl_cm.get_cmap('brewer_RdBu_11')
    my_cmap
= subset_colormap('mycmap',brewer_cmap, [0,3,10])
 
    cube
= iris.load_cube(iris.sample_data_path('air_temp.pp'))
    plt
.subplot(121)
    qplt
.contourf(cube, brewer_cmap.N, cmap=brewer_cmap)
    plt
.gca().coastlines()
    plt
.subplot(122)
    qplt
.contourf(cube, my_cmap.N-1, cmap=my_cmap)
    plt
.ion()
    plt
.show()
 
if __name__ == "__main__":
    example
()



Bernd Becker

unread,
Dec 11, 2012, 11:28:30 AM12/11/12
to scitoo...@googlegroups.com
Ian,


Without having seen the plot you cannot know which colours have been chosen from the brewer palette. To make an informed guess on how the plot may look and to ascertain that the next 256 plots use the same colour bar: Is there no  way to address the colours from the brewer and other pre-defined  palettes right at the beginning?

How could I recreate the image displayed here:

Best regards,
Bernd.

Bernd Becker

unread,
Dec 11, 2012, 11:30:05 AM12/11/12
to scitoo...@googlegroups.com

Carwyn Pelley

unread,
Jan 9, 2013, 6:20:07 AM1/9/13
to scitoo...@googlegroups.com
You can have a look at your colour map by plotting it.

something like:
   
a = np.linspace(0, 1, 256).reshape(1,-1)
a
= np.vstack((a,a))
fig
= plt.figure(figsize=(5,5))
plt
.axis('off')
plt
.imshow(a, aspect='auto', cmap=plt.get_cmap(cmapname))
fig
.text(0.5,0.95,cmapname,fontsize=10,verticalalignment='center',horizontalalignment='center')


You could make your axis so it represents the colour indices to help you choose them also, if that helps.
Reply all
Reply to author
Forward
0 new messages