Post Image
By Matt GaidicaMarch 9, 2018In Uncategorized

Making your own colormap (cmap) in MATLAB

This function will use the first row of an image and allow you to use that as a colormap, or as individual colors for your own application. It just extracts colors and puts them into an array. For instance, I designed this “traffic light” color scheme and want to apply it to a plot where each color represents some reaction time.


You can use any image, but here is how to design the gradient in Adobe Photoshop:

  1. Open a new document with a width greater or equal to the amount of elements you will be coloring in MATLAB (for instance, width: 1500 pixels, height: 200 pixels). The height is only so you can visualize the color scheme; the function will only require a single row.

  2. Use the Shape tool to draw a rectangle the size of the canvas.

  3. Double click the rectangle layer and apply a Gradient Overlay. Use the color tools to make your gradient.

  4. Save your document as a Photoshop file first if you want to archive the original, but then export it to a JPG of the original size either using Save As, or Save for Web.

Screen Shot 2018-03-09 at 9.09.24 AM.png

The mycmap function has one mandatory input: the image filename. There is one optional input: the number of elements to be returned. For instance, if you are using the colors to set cmap, you probably don’t need to set the number of elements (it will just return a colors array the width of your image). On the other hand, if you are using the colors to set the color of say, 55 different lines on a plot, you will pass that number as the second input to the function, and the returned array colors will have 55 rows.


Below are phase plots from an electrophysiology experiment where each line represents a single trial. Each line has been colored based on reaction time (low-to-high) using the colors from the mycmap function.


svgFinding Consecutive Numbers that Exceed a Threshold in MATLAB
svgSimpler Subplots: Converting Grid Coordinates to Axes Position