Matlab: (1) Plotting multiple canvases, (2) holding them separately, (3) montage all - matlab

I've got 10 grayscale images. I'd like to plot a simple YELLOW line over each image separately, then show them all over one plot (montage style).
I tried to draw all images first, but that made plotting lines very tricky (X,Y axes weren't standard for plotting over each separate image).
I thought about burning the line over the image, but I don't have the computer vision toolkit (easy functions to do this), otherwise it seemed complicated to both convert the grayscale to color and get it to burn the image.
I thought I might be able to use the function newplot to create a temporary plot space for each image, draw the line with a simple plot(...) call, then save it and just montage(...) all the individual plots at the end.
Is this possible? I've never played with the function newplot or tried to loop through individual plots, saving them up for a call to montage(...) this way, but it seems like a logical/simple approach.

I finally worked it out with subplot, subimage, and plot, using subplot with the position arguments does what I want easily enough. Using subplot kept the axis relative to the subplot I was on so I could plot the line with a standard fplot/plot call. The trick was normalizing the position to percentages vs. thinking of it in terms of pixels.
here's some code demoing it:
% Loop through this code, each time moving the subplot by position
LOOP {
% calculate left & bottom position as percentages (0..1)
subplot( 'Position', [ left bottom (1/cols) (1/rows) ] );
hold on
% (1) Draw the image
subimage(tmpImg, [0 255]);
axis off;
% (2) Plot the line over the original image
F = #(x) polyval(p, x);
fplot(F, [1 dimX 1 dimY], '-y');
}

Related

Plotting Additional Line w/o adjusting Plot Window around Original Data in Matlab

I have a scatterplot of data in Matlab, along with a horizontal linegraph which divides two sub groups of this data - all on the same plot. I have plotted these two entities separately using the hold on command.
Ideally, I want the plot window to automatically adjust to just the scatterplot data, and would prefer the horizontal line I plotted to simply extend off the screen in all cases. Is there a simple way to do this?
At the moment when I change the limits of the horizontal line, the graph window shifts to accommodate these points, skewing the view of the scatterplot data I'm actually interested in.
Example:
% central line segment
boundary_line = plot(csv_results.data(:,9),csv_results.data(:,10));
% negative extension of line segment off screen
line_negext = plot([-10,csv_results.data(1,9)],[csv_results.data(1,10),csv_results.data(1,10)]);
% positive extension of line segment off screen
line_posext = plot([10,csv_results.data(6,9)],[csv_results.data(6,10),csv_results.data(6,10)]);
% scatterplot data of interest
scatt_data = plot(csv_results.data(:,3),csv_results.data(:,4));
UPDATE: My problem is that, as seen in my code above, I need to plot two line segments at different y values that continue to positive and negative infinity, which link up to an existing plot in the middle. If I use yline I can simply draw one horizontal line - if I use xlim I risk cropping out data for subsequent runs..
If you want to adjust the axis to more restrictive portion (reduce), then xlim() and ylim() get the job done. Horizontal lines drawn by yline() will persist, as will vertical lines drawn by xline(). Note that xline() and yline() should work for releases including R2018b and later.
% MATLAB R2019a
% Sample Data
n = 10;
X1 = 5*rand(n,1);
Y1 = 5*rand(n,1);
X2 = 5 + 5*rand(n,1);
Y2 = 5 + 5*rand(n,1);
figure, hold on
yline(5)
scatter(X1,Y1,'bo')
scatter(X2,Y2,'rd')
scatter(X1,Y2,'ks')
xlim([0 5])
Notice that by calling xlim() for a larger x-axis range also expands the horizontal line.
xlim([-1,12])
If you plot new data after xlim() outside the range, the plot won't automatically adjust. However, if you do so before calling xlim(), then the horizontal line will expand. Try the example below.
figure, hold on
yline(5)
scatter(X1,Y1,'bo')
scatter(X2,Y2,'rd')
scatter(X1,Y2,'ks')
Then immediately execute
scatter(100*rand(n,1),Y1)
and see that the horizontal line has expanded to cover the new, much longer x-axis.
After posting this answer, I found: How to draw horizontal and vertical lines in MATLAB?

How to update a scatter3 plot (in a loop) in Matlab

Quite a simple question but just couldn't find the answer online... I want to visualise a point cloud gathered from a lidar. I can plot the individual frames but wanted to loop them to create a "animation". I know how to do it for normal plots with drawnow but can't get it working with a scatter3. If I simply call scatter3 again like I have done in the commented code then the frame that I am viewing in the scatter plot jumps around with every update (Very uncomfortable). How do i get the scatter3 plot to update to the new points without changing the UI of the scatter ie. Still be able to pan and zoom around the visualised point cloud while it loops through.
EDIT: The file is a rosbag file, I cannot attach it because it is 170MB. The problem doesn't happen when using scatter3 in a loop with a normal array seems to be something with using scatter3 to call a PointCloud2 type file using frame = readMessages(rawBag, i).
EDIT: The problem does not seem to be with the axis limits but rather with the view of the axis within the figure window. When the scatter is initialised it is viewed with the positive x to the right side, positive y out of the screen and positive z upwards, as shown in view 1. Then after a short while it jumps to the second view, where the axis have changed, positive x is now out of the screen, positive y to the right and positive z upwards (both views shown in figures). This makes it not possible to view in a loop as it is constantly switching. So basically how to update the plot without calling scatter3(pointCloudData)?
rawBag = rosbag('jackwalking.bag');
frame = readMessages(rawBag, 1);
scatter3(frame{1});
hold on
for i = 1:length(readMessages(rawBag))
disp(i)
frame = readMessages(rawBag, i);
% UPDATE the 3D Scatter %
% drawnow does not work?
% Currently using:
scatter3(frame{1})
pause(.01)
end
The trick is to not use functions such as scatter or plot in an animation, but instead modify the data in the plot that is already there. These functions always reset axes properties, which is why you see the view reset. When modifying the existing plot, the axes are not affected.
The function scatter3 (as do all plotting functions) returns a handle to the graphics object that renders the plot. In the case of scatter3, this handle has three properties of interest here: XData, YData, and ZData. You can update these properties to change the location of the points:
N = 100;
data = randn(N,3) * 40;
h = scatter3(data(:,1),data(:,2),data(:,3));
for ii = 1:500
data = data + randn(N,3);
set(h,'XData',data(:,1),'YData',data(:,2),'ZData',data(:,3));
drawnow
pause(1/5)
end
The new data can be totally different too, it doesn't even need to contain the same number of points.
But when modifying these three properties, you will see the XLim, YLim and ZLim properties of the axes change. That is, the axes will rescale to accommodate all the data. If you need to prevent this, set the axes' XLimMode, YLimMode and ZLimMode to 'manual':
set(gca,'XLimMode','manual','YLimMode','manual','ZLimMode','manual')
When manually setting the limits, the limit mode is always set to manual.
As far as I understood what you describe as "plots jumpying around", the reason for this are the automatically adjusted x,y,z limits of the scatter3 plot. You can change the XLimMode, YLimMode, ZLimMode behaviour to manual to force the axis to stay fixed. You have to provide initial axes limits, though.
% Mock data, since you haven't provided a data sample
x = randn(200,50);
y = randn(200,50);
z = randn(200,50);
% Plot first frame before loop
HS = scatter3(x(:,1), y(:,1), z(:,1));
hold on
% Provide initial axes limits (adjust to your data)
xlim([-5,5])
ylim([-5,5])
zlim([-5,5])
% Set 'LimModes' to 'manual' to prevent auto resaling of the plot
set(gca, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual')
for i=2:len(x,2)
scatter3(x(:,i), y(:,i), z(:,i))
pause(1)
end
This yields an "animation" of plots, where you can pan and zoom into the data while continuous points are added in the loop

How to print a reversed colorbar in matlab?

I want to reverse a colorbar on a figure saved as a pdf from Matlab. I can reverse the colorbar on my screen, but when I print my figure as a pdf the colorbar is flipped back to the normal direction again.
Minimum example:
figure(1)
colormap(parula(100))
c = colorbar('direction','reverse');
print(1,'-dpdf','graphs\test_colorbar.pdf',sprintf('-r%d',150))
Using export_fig does not help - when export_fig prints a reversed colorbar it flips the colormap and reverses the label order, which does not work if the ticks are not symmetric around the middle value of the colorbar.
Minimum example showing why flipping the colorbar and labels does not help:
tick_array = 0:0.3:1;
figure(1)
colormap(parula(100))
colorbar('direction','reverse','Ticks',tick_array)
export_fig('graphs\test_colorbar.pdf','-pdf')
tick_labels = strtrim(cellstr(num2str(flip(tick_array)')));
figure(2)
colormap(flipud(parula(100)))
colorbar('Ticks',tick_array,'Ticklabels',tick_labels)
The figure saved by export_fig looks like figure 2 (which is incorrect).
Try reversing the map itself, instead of the parameter that control its direction:
figure(1)
c = parula(100);
colormap(flipud(c));
colorbar;
print(1,'-dpdf','graphs\test_colorbar.pdf',sprintf('-r%d',150))
I also really suggest export_fig for saving images in MATLAB, especially for publication quality figures

How do I crop a 2-d surface plot in MATLAB, to remove axes and white margins?

I have written a program to process and print a 81x81 2D surface plot, which needs to be used as an input. Saving the plot made by Matlab includes side axes as well as white margins on the sides.
How do I crop this image to get just the (81pixels)x(81pixels) output as an image?
Try placing this after your figure code, it will remove the margins around your figure.
set(gca,'units','pixels') % set the axes units to pixels
xx = get(gca,'position'); % get the position of the axes;
set(gcf,'units','pixels') % set the figure units to pixels
yy = get(gcf,'position'); % get the figure position;
set(gcf,'position',[yy(1) yy(2) xx(3) xx(4)]) % set the position of the figure to the length and width of the axes
set(gca,'units','normalized','position',[0 0 1 1]) % set the axes units to pixels
You can avoid using surf plot and just save your array as image. So, you have 81x81 array. I'll use this one for the example:
a = magic(81);
Now you need to normalize image in order to have values from 0 to 255:
a = (a - min(min(a)))/(max(max(a))-min(min(a)))*255;
Finally you use imwrite to save your array as image.
imwrite(a,jet(256),'SO_4.png')
The first argument is your 81x81 array. The second argument is colormap. You can specify any colormap you want, but I like the heatmap jet(256). You can skip this argument, in this case the image would be grayscale. From here you can find what colormaps are available and what they look like. The last argument is image name. The result is shown below:
Hope that helps, good luck.

Matlab scatter plot with straight lines connecting the points

Is there an easy command to have a plot like the blue line in the picture (excel)? Matlab defaults to produce something like the line in red. The only way I know to do this is to issue a plot command for each segment of the line:
for i=2:n-1
plot([data(i-1,1) data(i,1)],[data(i-1,2) data(i,2)],'-b'); hold on;
end
You can just plot the entire array and let plot automatically draw straight line segments between each of the points. This is the default behaviour when plotting things in MATLAB. MATLAB plotting smooth lines is not the default behaviour when the plot is produced, so I'm not sure where you're getting that information.
You would need to perform some sort of spline interpolation to get the red line, but you desire the blue curve and so plotting the entire array in a single plot command should suffice.
It's as simple as:
plot(data(:,1), data(:,2), '-b');
Just to be sure that we're on the same page, I'm going to reproduce your data then use the above command to plot the data so you can see for yourself that the behaviour you desire is achieved:
data = [0 0; 1 1; 2 4; 3 6; 4 4]; %// Your data reconstructed
plot(data(:,1), data(:,2), '-b'); %// Main plotting code
%// Some extras
xlim([0 4.5]);
ylim([0 7]);
grid;
I've added in some extra code to get the plot to look like your example. I've made the x-axis limits go up to 4.5 and the y-axis limits go up to 7. I've also placed a grid in the plot.
We get: