Plotting 2D points without line in MATLAB - matlab

I have some 2D points and I want to plot them in MATLAB such that every point has a different color and specifier. I have used plot function but it creates line whatever you give. I want to draw these points as discrete points only. How can I do that? Here is what I am trying to achieve in the simplest form (I used TikZ below):
UPDATE:
Thank you for your comments and answers, I have the following code right now:
x = [ 0.56, 0.4526, -0.4324, 0.2749, -0.2993, 0.3404, 0.1959, 0.3363, -0.1706];
y = [0.1999, 0.3939, 0.1999, 0.4414, 0.2000, 0.3931, 0.1999, 0.3966, 0.4056];
figure
plot(x(1),y(1),'rx')
hold on
plot(x(2),y(2),'*','Color','[0 0.9 0]')
hold on
plot(x(3),y(3),'*','Color','[0 0.5 0]')
hold on
plot(x(4),y(4),'o','Color','[0.47 0.52 0.8]','MarkerFaceColor','[0.47 0.52 0.8]')
hold on
plot(x(5),y(5),'o','Color','[0.05 0.28 0.63]','MarkerFaceColor','[0.05 0.28 0.63]')
hold on
plot(x(6),y(6),'s','Color','[1 0.71 0.30]','MarkerFaceColor','[1 0.71 0.30]')
hold on
plot(x(7),y(7),'s','Color','[0.9 0.32 0]','MarkerFaceColor','[0.9 0.32 0]')
%plot(x(7),y(7),'s','Color','[1 0 0.5]','MarkerFaceColor','[1 0 0.5]')
hold on
plot(x(8),y(8),'d','Color','[0.67 0.28 0.73]','MarkerFaceColor','[0.73 0.40 0.78]')
%plot(x(8),y(8),'d','Color','[0.67 0.28 0.73]','MarkerFaceColor','[0.67 0.28 0.73]')
hold on
plot(x(9),y(9),'d','Color','[0.29 0.08 0.55]','MarkerFaceColor','[0.29 0.08 0.55]')
xlabel('X')
ylabel('Y')
h = legend('(^1X,^1Y)','(^2X_1,^2Y_1)','(^2X_2,^2Y_2)','(^3X_1,^3Y_1)','(^3X_2,^3Y_2)','(^4X_1,^4Y_1)','(^4X_2,^4Y_2)','(^5X_1,^5Y_1)','(^5X_2,^5Y_2)');
set(h,'Location','best')
grid
I can now draw the points as dots with different colors and specifiers although this way may not be the best way.

You can simply specify the LineSpec option
http://fr.mathworks.com/help/matlab/ref/plot.html#inputarg_LineSpec
To obtain your example:
plot(xdata, ydata, '.')

Related

How can I plot cumulative plots with specific x values?

I was trying to find out, how to plot a cumulative distribution function (cdf) with specific x values but was not successful.
For example, if the dataset is:
x = [2.50 5.21 7.67 8.43 9.15 11.47 14.59 21.45];
y = [0.20 0.09 0.15 0.13 0.17 0.04 0.7 0.15]; % (total 1)
the graph shape definitely looks wrong, when I use y = cdfplot(x).
I also plotted the graph with cumsum(y) and x to check the shape and it looks fine, but I would like to know, if there is any code which plots cumulative distribution plots.
There's the stairs function for creating "stairstep graphs", which should be exactly what you want, incorporating your cumsum(y) idea.
Please see the following code snippet. I added two additional points for the start and end of some interval, here [0 ... 25]. Also, your values in y sum up to something larger than 1, so I modified these values, too.
x = [0 2.50 5.21 7.67 8.43 9.15 11.47 14.59 21.45 25];
y = [0 0.10 0.09 0.05 0.10 0.14 0.04 0.4 0.08 0];
stairs(x, cumsum(y));
xlim([-1 26]);
ylim([-0.2 1.2]);
That'd be the output (Octave 5.1.0, but also tested with MATLAB Online):
Hope that helps!

5x1 subplots next to each other - why do I always have the first two that are disappearing?

This should be simple - but it appears not. I just would like to have a figure with five subplots next to each other - without spaces in between. Also, the first tricky part perhaps is that I want to have a first subplot that uses floatAxisX.
Let's say I have four variables:
x1 = salinity
x2 = temperature
x3 = density
y = depth
So my code looks like this:
figure;
hfig = figure('Name','xxx');
set(gcf,'Position',get(0,'ScreenSize'))
set(hfig,'color','w');
subplot(151);set(subplot(151),'Position',[0.15 0.15 0.15 0.75]);
% plot salinity vs depth
hl1=plot(x1,y,'k-');
% assign current axis handle to variable for later reference if needed
ax1=gca;
% set properties of the axes
set(ax1,'XMinorTick','on','ydir','reverse', 'ytick',[0:25:150],'box','on','xcolor',get(hl1,'color'))
% add 1st floating axis for the second parameter (temperature) plotted
[hl2,ax2,ax3] = floatAxisX(x2,y,'r:','Temperature (C)',[5 15 0 150]);
set(ax2,'ydir','reverse','ytick',[0:25:150])
% add 2nd floating axis for the third parameter (density) plotted
[hl3,ax4,ax5] = floatAxisX(x3,y,'b--','Density (Kg m^-^3)',[24 27 0 150]);
set(ax4,'ydir','reverse','ytick',[0:25:150]);
subplot(152);set(subplot(152),'Position',[0.31 0.35 0.15 0.55]);
For example, I have distinct plots put together using hold on:
plot(x1,y);axis ij;
subplot(153);set(subplot(153),'Position',[0.46 0.35 0.15 0.55]);
plot(x1,y);axis ij;
subplot(154);set(subplot(154),'Position',[0.61 0.35 0.15 0.55]);
plot(x1,y);axis ij;
subplot(155);set(subplot(155),'Position',[0.76 0.35 0.15 0.55]);
plot(x1,y);axis ij;
Note that just doing the following
subplot(151);set(subplot(151),'Position',[0.15 0.15 0.15 0.75]);
subplot(152);set(subplot(152),'Position',[0.31 0.35 0.15 0.55]);
subplot(153);set(subplot(153),'Position',[0.46 0.35 0.15 0.55]);
subplot(154);set(subplot(154),'Position',[0.61 0.35 0.15 0.55]);
subplot(155);set(subplot(155),'Position',[0.76 0.35 0.15 0.55]);
only gives me a figure with the last three subplots next to each other. I would be grateful to get some explanation why this happens and how to solve it.
From the help for subplot:
If a subplot specification causes a new axes to overlap anexisting axes, the existing axes is deleted - unless the positionof the new and existing axes are identical.
Your code is doing this, though you may not realize it. You call subplot(151), which places an axis in the default location and then you position it manually. Because you have manually positioned the first axis, calling subplot(152) is the same as calling subplot(151) and the new axis ends up on top of the first one causing the latter to be deleted. And so on, until you move axes away from the the area where the default axis is placed.
There are several ways around this. You can create all of your subplots and then go back and position them. You can create your subplots starting from the right of your figure (subplot(155)) and move left. Or you can create the subplots in the desired positions directly via:
subplot('Position',[0.15 0.15 0.15 0.75]);
subplot('Position',[0.31 0.35 0.15 0.55]);
subplot('Position',[0.46 0.35 0.15 0.55]);
subplot('Position',[0.61 0.35 0.15 0.55]);
subplot('Position',[0.76 0.35 0.15 0.55]);
You can have these return handles if you need to plot to these axes separately at a later time via plot(AX,...) or subplot(AX); plot(...), where AX is an axis handle.
Also note that the help for subplot contains special comments for the subplot(151) style, particularly:
This syntax does not return a handle, so it is an error to specify a return argument.
I get a handle back myself, but I don't know if it's to be trusted. Still, even changing the style to subplot(1,5,1) the issue of only showing the last three persists.

Plot an eye diagram of a signal in MatLab

The matrix X contained in a .mat file represents the acquired signal. The element of place (i, j) of the matrix is the i-th sample of the j-th screen. The sampling frequency is equal to 4 GS/s.
How do I plot the eye diagram relative to the signal contained in X using MatLab?
I tried but I could not draw the eye diagram from the matrix X (see http://ge.tt/8Xq5SYh/v/1?c). Here is the link to the matrix X that I used:
http://ge.tt/8Xq5SYh/v/0
and my MatLab code:
%sampling frequency fs=4 GS/s
rows=4000; %4000 rows (samples) |__ in matrix X
columns=10; %1000 columns (screens) |
%for plot all the graphics in the same window (overlapping)
hold on;
%index of the single row (column for the single column)
row=1:1:100;
t=1:1:100;
for column=1:columns,
%plot
plot(t,X(row, column),'-bo','LineWidth',1, 'MarkerEdgeColor','b', 'MarkerFaceColor','b', 'MarkerSize',2);
end
%axis properties
set(gca,'YTick', [-0.5 -0.45 -0.4 -0.35 -0.3 -0.25 -0.2 -0.15 -0.1 -0.05 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5]); %soli valori di ascisse da visualizzare
grid on;
set(gca,'GridLineStyle','-');
axis([0 10 -0.5 0.5]);
Someone could try to show me how to do?
maybe the matrix is not correct?
Thanks in advance to anyone who answers
You can simply plot(x,'b'). The plot command will draw a line for every column of x, which corresponds to all the samples of each "screen". The 'b' in the command is just to make every line the same color like a typical eye diagram.

Anti-alias lines vs markers in MATLAB

Hi I have a image in MATLAB
and I want the line to be smooth - look at the line from 0.4 to 0.8... it's horrible.
When using 'LineSmoothing','on' operator in plot I get this
I does a good job on lines but it smooths markers also and they are horrible!!
How can I get MATLAB to smooth only lines and not the markers??
Here is the code:
clear all;
close all;
bpp = [0.8 0.4 0.2 0.1 0.05];
bpp_j = [0.8 0.4 0.2 0.1];
AAE_JPEG = [1.65 2.91 6.20 10.96];
AAE_JPEG_2000 = [1.39 2.29 3.78 6.75 12.52];
AAE_EEDC = [2.08 2.67 3.80 5.94 9.31];
hold on;
plot(bpp_j, AAE_JPEG, 'k','LineWidth',1.5,'MarkerSize',9,'MarkerEdgeColor','k','LineSmoothing','on');
plot(bpp, AAE_JPEG_2000, 'k', 'LineWidth',1.5,'MarkerSize',6,'MarkerEdgeColor','k','LineSmoothing','on');
plot(bpp, AAE_EEDC, 'k', 'LineWidth',1.5,'MarkerSize',6,'MarkerEdgeColor','k','LineSmoothing','on');
plot(bpp_j, AAE_JPEG, 'x','LineWidth',1.5,'MarkerSize',8,'MarkerEdgeColor','k');
plot(bpp, AAE_JPEG_2000, 'o', 'LineWidth',1.5,'MarkerSize',6,'MarkerEdgeColor','k');
plot(bpp, AAE_EEDC, 'v', 'LineWidth',1.5,'MarkerSize',6,'MarkerEdgeColor','k');
LL = plot(rand(1,2),rand(1,2),'k-x','visible','off','LineWidth',1.5,'MarkerSize',8);
LK = plot(rand(1,2),rand(1,2),'k-o','visible','off','LineWidth',1.5,'MarkerSize',6);
LI = plot(rand(1,2),rand(1,2),'k-v','visible','off','LineWidth',1.5,'MarkerSize',6);
legend([LL,LK, LI],'JPEG','JPEG 2000','EEDC')
axis([0 0.9 0 14])
xlabel('bpp');
ylabel('AAE');
grid on;
and while I'm still here... how can I only display 0.05 0.1 0.2 0.4 and 0.8 on x-axis?
I'd just try using export_fig without even linesmoothing the lines...
I don't have a MATLAB here so I can't test but does it work if you plot the smoothed lines without markers
plot(bpp_j, AAE_JPEG, 'k','LineWidth',1.5,'LineSmoothing','on');
then another plot of the markers with no lines?
plot(bpp_j, AAE_JPEG, 'x','MarkerSize',8,'MarkerEdgeColor','k');
As for the x-axis ticks see matlab x axis label set as a vector

How to keep the subplot sizes unchanged after putting a colorbar

Let us say we have a 1-by-2 subplot and we plot some graphics inside as follows:
subplot(1,2,1)
surf(peaks(20))
subplot(1,2,2)
surf(peaks(20))
And then we want to put a colorbar:
colorbar
I don't want the right figure squezzed as in the result. How can we put the colorbar out of the rightmost figure in a row of subplots and keep the sizes of them unchanged?
Note: Actually, I need it for plotting images where the colorbar is common and I want to put it on the right. I used this toy example for simplicity.
You could just extract the position of the first plot and use on the second. MATLAB automatically moves the colorbar to the right when rescaling.
f1=figure(1);clf;
s1=subplot(1,2,1);
surf(peaks(20));
s2=subplot(1,2,2);
surf(peaks(20));
hb = colorbar('location','eastoutside');
%% # Solution:
s1Pos = get(s1,'position');
s2Pos = get(s2,'position');
s2Pos(3:4) = [s1Pos(3:4)];
set(s2,'position',s2Pos);
%% # Alternative method. Brute force placement
set(s1,'Units','normalized', 'position', [0.1 0.2 0.3 0.6]);
set(s2,'Units','normalized', 'position', [0.5 0.2 0.3 0.6]);
set(hb,'Units','normalized', 'position', [0.9 0.2 0.05 0.6]);
This is just what I was looking for. After implementing Vidar's automatic solution I came up with a simplification. Get the position of the far right axes BEFORE adding the colorbar, and then just reset the squeezed position to the original:
f1=figure(1);clf;
s1=subplot(1,2,1);
surf(peaks(20));
s2=subplot(1,2,2);
surf(peaks(20));
s2Pos = get(s2,'position');
hb = colorbar('location','eastoutside');
set(s2,'position',s2Pos);