Basic Imagesc Question -- 2D Matrix

3 views (last 30 days)
I have a 2D matrix. I wish to plot it in a particular way.
The first col, I wish to represent the values on the y-axis. The second col, I wish to use to color code the plotted values.
The matrix changes over time, t. Time runs along the x-axis.
for t = 1: T
c = NaN(2,5);
c(1,:) = rand(1,5); %this vector should be the y-axis
c(2,:) = randi(10,1,5); %this vector should color code c(1,:)
c = sort(c,2);
imagesc(); %??
clear c;
end
I think this is very simple, but just cant get it working! Can anyone help? thank you.

Accepted Answer

mathworks2011
mathworks2011 on 22 Sep 2011
patch.m is the correct function to use.

More Answers (1)

Walter Roberson
Walter Roberson on 9 Sep 2011
I am unclear as to what your x axis is?
Your sort(c,2) sorts each row independently, which destroys any implicit link you might have of the color in c(2,K) corresponding to the y in c(1,K). Is that what you want? Or were you trying to sort by y value but preserve the color mapped to each y ?
I was thinking that perhaps your x axis is implicitly the column index in to the matrix, but if that is the case then it would not seem right to be sorting the y ?
You effectively have a vector of y values and a vector of corresponding colors, but imagesc() is for rendering 2D matrices colored per-pixel. Is a bar graph perhaps a better representation for your data?
  2 Comments
Walter Roberson
Walter Roberson on 9 Sep 2011
If y is a _row_ vector, then bar([y;nan(1,length(y))]) will produce a bar chart in which every bar is colored separately. The color order would, by default, be the color order of the first N entries of the current color map. The bar chart will return an hggroup that has one child patch object per bar (in *this* situation); you could change the FaceColor property or perhaps the CData property in order to set the colors you want.
Note that if you take this approach, you will want to fetch xlim(), subtract 1 from the maximum value, and set that as the new xlim: you would do this because bar() would initially set the xlim to allow a full space for the bar group whose data is all nan.
There are other approaches using bar(), but this way is cleaner.
You could also simulate bar() using line() or plot(), together with fill(), but the way I outline aboe would probably be less effort.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!