Two levels of y-axis tick labels in text on imagesc - matlab

I'm plotting a figure that is a grid of colors/shades, based on the values from a 4x5 matrix.
The x-axis and y-axis tick labels are set using text from a cell array.
The y-axis tick labels exist at 2 levels,
% y ticks, 2 levels
ylabelnames = {'team1','set1';'team1','set2';'team2','set1';'team2','set2'};
I'd like the y-axis tick labels to either
1) span 2 lines, so that they match the 2 lines of text being overlaid in the squares of the plot, i.e. 'team#' on the first line and 'set#' on the second line of each row of the imagesc grid, or
2) rotate the label 'team1' to span across the first 2 rows and rotate label 'team2' to span across the last 2 rows, to not repeat the use of 'team1' 'team2' in the labeling.
My entire code:
%% Plot
Matrix = rand(4,5);
Matrix2 = rand(4,5);
% y ticks, 2 levels
ylabelnames = {'team1','set1';'team1','set2';'team2','set1';'team2','set2'};
xlabelnames = {'group1','group2','group3','group4','group5'};
sigfig = 2;
spacer = '\n';
% Font sizes
plotFontSize = 8; labelFontSize = 8; plotWidth = 500; plotLength = 300;
imagesc(abs(Matrix))
colormap('gray')
colormap(flipud(colormap))
caxis([0 2])
for rows = 1:size(Matrix,1)
for columns = 1:size(Matrix,2)
if abs(Matrix2(rows,columns))<= 0.5 % Show 2nd values less than 0.5
num = Matrix2(rows,columns);
num = vpa(num,sigfig);
num = ['= ' char(num)];
rval = sprintf('%0.2g', Matrix(rows,columns));
message = sprintf([ 'val= ' rval spacer 'val2' num '' ]);
text(columns,rows, message,'HorizontalAlignment','center',...
'FontSize',plotFontSize,'FontName','Times New Roman');
end
end
end
% Put on tick labels
set(gca,'Ticklength', [0 0],'YTick',1:size(Matrix),'XTick',1:length(xlabelnames),...
'YTickLabel',ylabelnames,'XTickLabel',xlabelnames,...
'FontSize',labelFontSize,'FontName','Times New Roman')
If you run this, you'll see that only the first column of the y-labels are used, even though there's plenty of space to fit 2 rows of text per imagesc row.
I did think of a terrible hack way, which is to use
ylabel('team1 team2')
with the huge spaces between labels to spread them out evenly between the y rows, but that's not convenient if I were to increase the size of my matrix, so I'd prefer not doing it that way.
Is there any way to achieve the multi-leveled y tick labeling that I require?
Thanks!

In your example I'd use the text function. For example you can just add:
text(-0.05,1.5,'team1','HorizontalAlignment','center','Rotation',90 );
text(-0.05,3.5,'team2','HorizontalAlignment','center','Rotation',90 );
and see the outcome. The trick is to understand that each "pixel" (or imagesc cell element) is also a unit of the function's 'text' x and y inputs (and also of the ticks if you don't have different scaling), so to go between the first and second blocks use y=1.5 or y=3.5 etc...
to go outside the image on the left use a negative x value (x=-0.05) etc. Read more on text properties here...

Related

Adding colors to lines of a stairstep plot

I am trying to plot a hypnogram (graph that shows sleep cycles) and am currently using stairstep function to plot it. Below is a sample data since the one I am working with is huge:
X = linspace(0,4*pi,10);
Y = sin(X);
stairs(X,Y)
How do I make the lines of every ticks/score on the y-axis have a unique color? Which looks something like this:
One way to do it would be to segregate your data into as many dataset as your have flat levels, then plot all these data sets with the required properties.
There is however a way to keep the original dataset into one piece. If we consider your initial example data:
X = linspace(0,4*pi,10);
Y = sin(X);
step 1: recreate a "stair" like data set
Then by recombining the elements of X and Y we can obtain the exact same output than with the stairs function:
x = reshape( [X;X], 1,[] ); x(1) = [] ; % duplicate each element, remove the first one
y = reshape( [Y;Y], 1,[] ); y(end) = [] ; % duplicate each element, remove the lastone
hp = plot(x,y) ;
step 2: Use patch to be able to specify level colors
The patch object has many option for colouring faces, vertex and edges. The default patch object will try to close any profile given in coordinate by joining the first and last point. To override this behaviour, you just need to add a NaN element to the end of the coordinate set and patch will produce a simple line (but all the colouring options remain !).
To determine how many levels and how many colors we will need, we use the function unique. This will tell us how many unique levels exist in the data, and also we can associate each level with an index which will point to the color map.
%% Basic level colored line patch
% create profile for patch object
x = reshape([X;X],1,[]); x(1) = [] ; % same as above to get a "stairs" shape
y = reshape([Y;Y],1,[]); y(end) = [] ; % idem
xp = [x,NaN] ; % add NaN in last position so the patch does not close the profile
yp = [y,NaN]; % idem
% prepare colour informations
[uy,~,colidx] = unique(Y) ;
ncolor = length(uy) ; % Number of unique level
colormap(hsv(ncolor)) % assign a colormap with this number of color
% create the color matrix wich will be sent to the patch object
% same method of interleaving than for the X and Y coordinates
cd = reshape([colidx.';colidx.'],1,[]);
hp = patch(xp,yp,cd,'EdgeColor','interp','LineWidth',2) ;
colorbar
Yes! ... now our flat levels have a colour corresponding to them ... but wait, those pesky vertical lines are still there and polluting the graph. Could we colour them in a different way? Unfortunately no. No worries however, there is still a way to make them completely disappear ...
step 3: Use NaN to disable some segments
Those NaN will come to the rescue again. Any segment defined with a NaN will not be plotted by graphic functions (be it plot, patch, surf or any other ...). So what we can do is again interleave some NaN in the original coordinate set so only the horizontal lines will be rendered. Once the patch is created, we can build a second, "opposite", coordinate set where only the vertical lines are visible. For this second set, since we do not need fancy colouring, we can simply render them with plot (but you could also build a specific patch for that too if you wanted to colour them differently).
%% invisible vertical line patch + dashed vertical lines
% prepare profile points, interleaving NaN between each pair
vnan = NaN(size(X)) ;
xp = reshape([X;vnan;X],1,[]); xp([1:2 end]) = [] ;
yp = reshape([Y;Y;vnan],1,[]); yp(end-2:end) = [] ;
% prepare the vertical lines, same method but we interleave the NaN at one
% element offset
xv = reshape([X;X;vnan],1,[]); xv([1:3 end]) = [] ;
yv = reshape([Y;vnan;Y],1,[]); yv([1:2 end-1:end]) = [] ;
% prepare colormap and color matrix (same method than above)
[uy,~,colidx] = unique(Y) ;
ncolor = length(uy) ; % Number of unique level
colormap(hsv(ncolor)) % assign a colormap with this number of color
% create the color matrix wich will be sent to the patch object
% same method of interleaving than for the X and Y coordinates
cd = reshape([colidx.';colidx.';vnan],1,[]); cd(end-2:end) = [] ;
% draw the patch (without vertical lines)
hp = patch(xp,yp,cd,'EdgeColor','flat','LineWidth',2) ;
% add the vertical dotted lines
hold on
hv = plot(xv,yv,':k') ;
% add a label centered colorbar
colorbar('Ticks',((1:ncolor)+.5)*ncolor/(ncolor+1),'TickLabels',sprintf('level %02d\n',1:ncolor))
I have used the hsv colormap in the last example because your example seems to indicate that you do not need gradually progressing colors. You could also define a custom colormap with the exact color you want for each level (but that would be another topic, already covered many time if you search for it on Stack Overflow).
Happy R.E.M. sleeping !
Below code is not that efficient, but works well.
Basically, it draws line by line from left to right.
Firstly, generate sample data
num_stage = 6;
% generate sample point
x = linspace(0,1,1000)';
% generate its stage
y = round((sin(pi*x)+1)*(num_stage-1)/2)/(num_stage-1);
stage = unique(y); % find value of each stage
color_sample = rand(num_stage,3); % set color sample
Then we can draw like this
idx = find([1;diff(y)]); % find stage change
idx(end+1) = length(x)+1; % add last point
% display routine
figure;
% left end stage
k = 1;
% find current stage level
c = find(stage == y(idx(k)));
% plot bold line
plot(x([idx(k),idx(k+1)-1]),y(idx(k))*ones(2,1),'color',color_sample(c,:),'linewidth',5);
hold on;
for k = 2 : length(idx)-1
% find current stage level
c = find(stage == y(idx(k)));
% plot dashed line from left stage to current stage
plot(x([idx(k)-1,idx(k)]),[y(idx(k-1));y(idx(k))],'--','color',[0.7,0.7,0.7]);
% plot bold line for current stage with specified color
plot(x([idx(k),idx(k+1)-1]),y(idx(k))*ones(2,1),'color',color_sample(c,:),'linewidth',5);
end
% set x-axis
set(gca,'xlim',[x(1),x(end)]);
Following is result
Use if statement and divide it blocks. Check the criteria of Y-axis to be in a certain range and if it falls in that range, plot it there using the colors you want. For example if (y>1) plot(x,y,'r') else if (y some range) plot(x,y,'b'). Hope it helps

MATLAB : bar graph_ modify parameters on axis

I have a bar graph with 2 different data (each with a different color as you can see on the picture). I would like to move the x-axis so it crosses at y=-100 (for example). So if a data = -40, I would like to have a bar from -100 to -40.
An other question : is it possible to write each value of the x-axis vertically (because with all the values, we can't see anything).
The last question : is it possible to have 2 different scales for the x_axis ?
Thank you in advance,
Best regards,
Here is some code to get you going. Everything is commented so that should be easy to follow:
clear
clc
close all
%// Generate dummy data
y = -90*rand(1,20);
NumY = numel(y);
HalfData = round(NumY/2);
%// Loop to color half in pink and half in blue
hold all
for k = 1:NumY
hBar = bar(k,y(k));
if k <= HalfData
set(hBar,'FaceColor',[1 0 1])
else
set(hBar,'FaceColor',[0 0 1])
end
end
hold off
%// Get xtick labels and position for future use
xtLabels = cellstr(get(gca,'XTickLabel')).';
xtPos = get(gca,'XTick');
%// Change baseline value
set(hBar,'BaseValue',-40)
%// Get baseline to change its properties if you want
hBaseL = get(hBar,'Baseline');
set(hBaseL,'LineStyle','--','Color','k','LineWidth',3)
%// Adjust axis limits. Remove labels to place them vertically
set(gca,'XLim',[0 NumY],'XTickLabel',{''})
%// Get correct position for xlabel text
YLimPoArrays = min(get(gca,'YLim'));
YLimPoArrays = repmat(YLimPoArrays,numel(xtPos),1);
%// Place text positioned vertically with a small y offset
text(xtPos,YLimPoArrays-3,xtLabels,'HorizontalAlignment','center','Rotation',90,'FontSize',15)
And the output:
Hope that helps!

How can I set subplot size in MATLAB figure?

I often need to plot 10 images together, but using this code results in small images :
img = rand(400,600);
for i=1:10
subplot(2,5,i);
imshow(img);
title(['Image ' int2str(i)]);
end
As you can see, the images do not use all available space in the screen. How can I increase the size, or decrease the padding/margin between them?
Thanks for any help.
I don't believe there is an easy way to do it. There are two options:
First, use the position part of the subplot:
>> subplot(2,5, i, [l, b, w, h])
and calculate the left, bottom, width, height.
Or, get the handle of the returned axis:
>> h(i) = subplot(2,5,i);
and then modify the axis afterward.
>> set(h(1), 'position', [l, b, w, h] );
There are a number of pages that will give more detail, e.g., http://www.briandalessandro.com/blog/how-to-make-a-borderless-subplot-of-images-in-matlab/
[update]
The code below gives a little more detail on who you can do something like what you are looking for. It is a tad tedious. The 0.95 and 0.02 are just to give a little padding. They are nothing magical. :-)
One other thing to note is I would really encourage you to use "ii" as your index variable (or something else) as "i" is defined as sqrt(-1). It is a good convention not to use "i" and "j" as index variables (especially in Matlab).
img = rand(400,600);
figure(1);
clf();
hold on;
% Get the width and height of the figure
lbwh = get(1, 'position');
figw = lbwh(3);
figh = lbwh(4);
% Number of rows and columns of axes
ncols = 5;
nrows = 2;
% w and h of each axis in normalized units
axisw = (1 / ncols) * 0.95
axish = (1 / nrows) * 0.95
for ii=1:10
% calculate the row and column of the subplot
row = floor( ii/(ncols+1) ) + 1
col = mod( ii-1, ncols ) + 1
% calculate the left, bottom coordinate of this subplot
axisl = (axisw+0.02) * (col-1)
axisb = (axish+0.02) * (row-1)
% plot the subplot
h= subplot('position', [axisl, axisb, axisw, axish] );
imshow(img);
title(['Image ' int2str(ii)]);
pause
end
You will have to play with it to make it do exactly what you want. And "help" is your friend.
I have this requirement often and the most efficient way for me to achieve it is using the third party subplot_tight function, which is a more-or-less slot-in replacement for subplot. At its simplest you can do
figure(1); clf
subplot_tight(1,2,1, [0.05 0.05])
%normal plot stuff
where the two parameters in the fourth argument control the fraction of visible space around the image.
Based on the answer of #brechmos, when your subplot number is more than 10 subplot, then his code will trigger a error.
% calculate the row and column of the subplot
row = floor( ii/(ncols+1) ) + 1
col = mod( ii-1, ncols ) + 1
e.g. 4X5 cells, then subplot 11 will be wrongly interpreted as (2, 1), but not (3,1).
Replace it with the code below can fix it:
% calculate current row and column of the subplot
row = floor( (i-0.5)/ncols ) + 1;
col = mod(i-(row-1)*ncols, ncols+1);
You can use figure properties option once you generate the plot. Click on the subplot which you want to resize. From property editor select 'more properties' option. There if you scroll you will see 'Position' tab. You can change those values to see how the subplot moves and thus adjust subplot according to your preference.

Interpolate/fill in missing cells in truth-value grid in MATLAB

Consider the 37x101 matrix below:
Each black cell has value 1, the rest of the cells value 0. I would like to fill in the "gaps" by means of cubic spline interpolation, as well as scaling the y axis from 37 to 181. The latter can be done by using the interp1 function, as in:
interp1(37,matrix,181,'pchip')
However, the outcome is now interpolated along the y-axis, but the gaps remain:
I don't want to interpolate along the x-axis, because I want the final matrix to have dimension 181 x 101. I just want to fill in the gaps using the existing cells (of the 181 x 101 matrix).
How can the original matrix (top), be scaled from 37 x 101 to 181 x 101 (without the "smoothing" in the second image), as well as filling in the gaps using some kind of spline interpolation as if this was a proper function?
It appears that your truth value grid has a single one where the true value is in each row. If the true/1 values do in fact create a line through the image, I would recommend parametrize the line with respect to t so that y = fy(t) and x = fx(t). If you're not familiar with this you can find some parametrization info on youtube tutorials or google. The main idea is that if you have , say a truth table that looks like this:
Then you could plot the the location of each pixel with respect to another variable, t and then use interp1(...) on each of these individually. In my case I defined the x and y values as follows:
n = 32;
rand('seed', 1982);
y_orig = 1:n;
x_orig = ceil(n*sin(y_orig/n*pi));
So I can plot as:
t1 = linspace(0,1, n);
plot(t1,x_orig, 'r', 'linewidth', 3);
hold all
plot(t1,y_orig, 'b', 'linewidth', 3);
legend('X', 'Y')
Note that I can get any truth value I want just by using interp1 like this (if you wanted to find the value half way between the 5th and 6th row):
desiredY = 5.5;
t= 1:n;
truthValue= interp1(t, x_orig, desiredY, 'cubic')
But we are looking to make a new image so I chose a more convenient parametrization of t between zero and one. Unfortunately, you may not have x and y off hand, so we need to pull them out of the image. Assuming you have a single true/1 value in each row we can yank out the values with max(...):
[maxVals, x1] = max(data,[],2);
x1(maxVals == 0) = [];
y1 = find(maxVals ~= 0);
Some form of find on each row would also work. If you have a truth value in each row then y1 should equal 1:n. The max function returns the index of the max in dimension 2 in the second return value. I use the next two lines to remove any entries where there truth table was empty (max is zero) and then y1 = 1:n minus those entries that were empty.
A quick and dirty way to get lots of points along this line is:
t2 = linspace(0,1,1024);
x2 = interp1(t1, x1, t2, 'cubic');
y2 = interp1(t1, y1, t2, 'cubic');
I can then plot the original points/image and this newly discovered finer line together like this:
imagesc(data);
hold all;
plot(x2,y2, 'linewidth', 2);
axis image
colormap(flipud(colormap(gray)));
To get this:
Finally, you can quickly turn this into a new image by scaling the parametrization up. My method is not particularly efficient for clarity:
y2_scaled = floor((y2(:)-1)*scaleValue + 1);
x2_scaled = floor((x2(:)-1)*scaleValue + 1);
scaleValue = 2;
data2 = zeros(n*scaleValue);
for ind = 1:length(x2_scaled)
data2(y2_scaled(ind),x2_scaled(ind)) = 1;
end
Which results in:
Note that this table has connected all the points and you now have multiple true/1's in each row. This is because I chose a very small step size for t2. You could fix this by either choosing t2 smarter, skipping multiple values in each row, or average the location of each indices in each row. Or ignoring this issue.
To fix t2 with the scaling value, you could use
t2 = linspace(0,1,n*scaleValue);
to get only one true/1 per row in the above code.
Also, if you want to only scale one dimension, you could do it like this:
y2_scaled = floor((y2(:)-1)*scaleValue + 1);
x2_scaled = floor((x2(:)-1) + 1);
scaleValue = 2;
data2 = zeros(n*scaleValue,n);
for ind = 1:length(x2_scaled)
data2(y2_scaled(ind),x2_scaled(ind)) = 1;
end
I see this as a bitmap, so why not a clamped blur?
I=yourmatrixhere
%gaussian blur
% it looks like bump-to-bump distance is 3 empties
% therefore hsize should be about 7
% it looks like bump vertical size is about 4
% therefore simga should be about 10
hsize=[3 3];
sigma = 10;
h=fspecial('gaussian',hsize,sigma)
I2=imfilter(I,h,'replicate');
At this point you have spread information to adjacent columns, but you need to "tidy up" from continuous to binary.
%threshold
th = 0.25;
I3=zeros(size(I));
ind=find(I>=th);
I3(ind)=1;
At this point, I3 is your matrix of interest to do the "erode" or interpolation.

How can thin horizontal lines be added between each row in a grayscale image?

I need to create an nth-order Hadamard matrix, row double it, within each row randomly permute the elements of the matrix, and then display it. So far, I have accomplished all of these things. What I end up with when I imshow(matrix) is a nice picture of black and white boxes. But I haven't figured out how to insert a fine line to divide each row. I can create something like the first image on the left, but not the image on the right (these are Figures 1 and 2 from this paper)
Any help or comments would be thoroughly appreciated.
I've found using vector approaches (e.g., patch and rectangle) for this sort of problem unnecessarily challenging. I think that it's more straightforward to build a new image. This avoids floating-point rounding issues and other things that crop up with vector graphics. My solution below relies on some functions in the Image Processing Toolbox, but is simple and fast:
% Create data similarly to #TryHard
H = hadamard(48);
C = (1+[H;-H])/2;
rng(0); % Set seed
C(:) = C(randperm(numel(C))); % For demo, just permute all values, not rows
% Scale image and lines
scl = 10; % Amount to vertically scale each row
pad = 2; % Number of pixels to add between each row
C = imresize(C,scl,'nearest');
C = blockproc(C,[scl size(C,2)],#(x)[x.data;zeros(pad,size(C,2))]);
C = C(1:end-pad,:); % Remove last line added
% Dispay image
imshow(C)
This results in an image like this
The scl and pad parameters can be easily adjusted to obtain different sizes and relative sizes. You can call imresize(...,'nearest') again after adding the lines to further scale the image if desired. The blocproc line could potentially be made more efficient with various options (see the help). It could also be replaced by calls to im2col and col2im, which possibly could be faster, if messier.
I did not try the code, but I think that something like that should work:
sizeOfACube = 6;
numberOfRows = 47;
RGB = imread('image.png');
RGB = imresize(A, [(numRows+numberOfRows) numCols]);
for i=1:1:NumberOfRows
RGB(i*6,:,:) = 0;
end
imagesc(RGB);
imwrite(RGB,'newImage.png');
with:
sizeOfAcube the size of one cube on the QRcode.
numRows and numCols the number of Rows and Column of the original image.
One solution is to use patches, for instance as follows:
% set up example array
xl = 24; yl = xl;
[X Y] = find(hadamard(xl)==1);
% generate figure
figure, hold on
for ii=1:length(X)
patch(X(ii) + [0 0 1 1],Y(ii) + [0.1 0.9 0.9 0.1],[1 1 1],'Edgecolor',[1 1 1])
end
axis([0 xl+1 0 yl+1])
axis('square')
The patch command patch(x,y, color) accepts the vertices of the polygon element as x and y. In this example you can modify the term [0.1 0.9 0.9 0.1] to set the thickness of the bounding black line.
This generates
Edited
For the particular instance provided by the OP:
H=Hadamard(48); %# now to row-double the matrix
A=(1+H)/2;
B=(1-H)/2;
C=[A; B]; %# the code below randomly permutes elements within the rows of the matrix
[nRows,nCols] = size(C);
[junk,idx] = sort(rand(nRows,nCols),2); %# convert column indices into linear indices
idx = (idx-1)*nRows + ndgrid(1:nRows,1:nCols); %# rearrange whatever matrix
E = C;
E(:) = E(idx);
[X Y] = find(logical(E));
xl = length(X);
yl = length(Y);
figure, hold on
for ii=1:xl
rectangle('Position',[X(ii) Y(ii)+.2 1 0.8],'facecolor',[1 1 1],'edgecolor',[1 1 1])
end
axis([0 max(X)+1 0 max(Y)+1])
axis('square')
set(gca,'color',[0 0 0])
set(gca,'XTickLabel',[],'YTickLabel',[],'XTick',[],'YTick',[])
This example uses rectangle instead of patch to generate sharp corners.
The image: