MATLAB Answers

0

Multiple line plots of matrix with color depending on other vector

Asked by Elisaveta Dombrovski on 26 Jun 2018
Latest activity Answered by Ameer Hamza
on 26 Jun 2018
Hi,
I am pretty inexperienced with matlab, so sorry if I cannot express myself clearly.
I have a matrix A = 78x31 and I want to make a line plot of the rows, so 78 lines in total.
Additionally I want the range of the line colors to be dependent on the values of a vector B = 78x1.
So for example:
for row 1: B(1) -> 3.3 -> medium color
for row 2: B(2) -> 5.5 -> dark color
for row 3: B(3) -> 2.5 -> bright color etc..
I used the plot(A) before, which works fine, but gives me very random colors. scatter(A,B) does not do the job either.
Thank you in advance!

  0 Comments

Sign in to comment.

1 Answer

Answer by Ameer Hamza
on 26 Jun 2018

The order of plot colors is decided by the ColorOrder property of the axes object. You can access this property as
ax = gca;
ax.ColorOrder
for R2014a and older
ax = gca;
get(ax, 'ColorOrder')
So if you have a colorMatrix corresponding to B vales i.e. [78 x 3] matrix, each row is an RGB color. i.e. B(1) = 3.3 -> [r g b]??? is the first row. You need to have RGB values corresponding to each value in B. Then you can use the colorMatrix to change the value of ColorOrder property.
ax.ColorOrder = colorMatrix;
For older versions
set(ax, 'ColorOrder', colorMatrix);
So the main task here is to generate such a colorMatrix based on values in vector B. Although it is possible to get such a colorMatrix using colormap() function, it is difficult to suggest an exact solution unless you can provide with a sample dataset.

  0 Comments

Sign in to comment.