0

I'm using the LINES(M) Matlab function which returns an M-by-3 matrix containing a colormap. The problem is that sometimes it return the same colour twice. As an example I used LINES(8) in the results as shown below the first and last row are the same, so I'm asking how can the returned matrix has unique set of colours, if anyone could advise?

0   0   1
0   0.500000000000000   0
1   0   0
0   0.750000000000000   0.750000000000000
0.750000000000000   0   0.750000000000000
0.750000000000000   0.750000000000000   0
0.250000000000000   0.250000000000000   0.250000000000000
0   0   1
Tak
  • 3,536
  • 11
  • 51
  • 93
  • How are you planning to use the colormap? If you're using it to set line colors for repeat invocations of plot on the same figure then have you considered using 'hold all'? – grantnz Aug 05 '13 at 04:05
  • @grantnz thanks for your comment. Actually what I want to do is get unique colors using the LINES method, do you have any suggestions regarding this? – Tak Aug 05 '13 at 05:07

2 Answers2

1

The lines color map has only 7 unique colors (as you have already spotted).
If you need more than 7 unique colors, you'll have to create a map by yourself.

One option is using rand:

>> rCmap = rand( n, 3 ); % create a random map with n colors - usually unique.
Shai
  • 111,146
  • 38
  • 238
  • 371
1

The lines colormap has a maximum of 7 unique colors after which they start to repeat.

>> lines(8)
ans =
            0            0            1
            0          0.5            0
            1            0            0
            0         0.75         0.75
         0.75            0         0.75
         0.75         0.75            0
         0.25         0.25         0.25
            0            0            1    % <---- starts to repeat

You can always select one of the other colormaps which use interpolation to build as many colors as you want. See doc colormap for a list of supported ones.

Similarly you can build you own colormap using the same linear interpolation technique between a specified number of stop-points.

For example, the jet colormap is constructed by passing through a series of 9 endpoints as shown here, using linear interpolation in between. The hsv colormap is built in a similar manner, only it interpolates across the hue space rather than in RGB. Here is yet another example showing how to build a custom divergent colormap with red-white-blue endpoints.

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454