add error bar to a plot matlab [duplicate] - matlab

I am very new to MATLAB and expect a step-by-step solution. I have data, series(y), which I have to plot against (x). Also I have the standard deviation values for each data point of (y). Now I have to plot these series highlighting the error bars. How can I do that?
The data is in a text file sorted in columns as:
X = -50, -49, -48, -47....0....1, 2, 3, 4, 5....till 50
Y = 1.2, 1.0, 1.1, 1.9, 1.3.....
Standard deviation = 0.6, 0.5, 0.3, 0.6, 0.6.....
Also, how do I control the ticks and appearance property for these kinds of graphs?

x = 1:0.1:10;
y = sin(x);
e = 0.1 * randn(length(x), 1);
errorbar(x,y,e)
set(gca, 'Xlim', [4 10])
set(gca, 'XTick', 4:2:10)
See also get(gca) and get(gcf) for other properties to change.
For help on any of these functions, do, for example, help errorbar.

Related

How can I create a rectangle with a hole in MATLAB/OCTAVE?

I would like to plot/draw exactly this shape in MATLAB or OCTAVE. Of course I do know how to plot, and how to create rectangles, using either the plot, the line or the rectangle functions. But I have not yet managed to add this "hole" on the top side of the rectangle. I figure, it's a (half-)circle of radius 0.5 and center point (1.5|2). In OCTAVE, there is a drawCircleArc function, but I don't want to only draw that thing, but also to have the necessary coordinates defining the whole shape for further manipulation.
Here is one way (matlab/octave compatible):
% Specify all polygon points, excluding the semi-circle outline
X = [1, 0, 0, 3, 3, 2];
Y = [2, 2, 0, 0, 2, 2];
% Add semi-circle outline to array of polygon points
t = 0 : -0.01 : -pi;
X = [X, 1.5 + 0.5 * cos(t)];
Y = [Y, 2 + 0.5 * sin(t)];
% Use fill to plot the filled polygon, with desired settings
fill( X, Y, [0.8, 0.8, 0.8], 'linewidth', 1.5 );
axis( [-2, 4, -2, 4] ); axis equal;
As of 2017b you can also use polyshapes and boolean operators.
rect = polyshape([0 3 3 0], [0 0 2 2]);
t = linspace(0, 2*pi, 32);
circ = polyshape(1.5+.5*cos(t), 2+.5*sin(t));
subplot 121, hold on
plot(rect)
plot(circ)
axis equal
shape = subtract(rect, circ);
subplot 122
plot(shape)
axis equal

Add errorbar to gscatter Matlab

I want to add a known error bar (vertical) for each x data for the gscatter function. I have plotted the grouped scatter (to specify the colour) with calculated mean. How should I do?
This is my current code
Mydata = readable ('D:\Download\Book1.xlsv);
y = Mydata.Y;
x = Mydata.X;
g = Mydata.Category
size = 10
h = gcatter (x,y,g,'rkgb','X',size);
I don't think that Matlab's scatterplot supports errorbars from within the scatter function itself. I think that some more manual work should be done. Here is a working example with 2 categories, made simple with a loop (you can apply this to much more than just 2 categories)
Y = [4,3,4,2,10,9,11]; % some invented Y data
X = [1,2,3,7,6,9,8]; % some invented X data
groups = [0, 1]; % 2 groups/categories
G = [0,0,0,1,1,1,1]; % categories of data
E = [0.1, 0.4, 0.2, 0.5, 0.9, 0.7, 1]; % errors
colors = {'r', 'k'};
figure, gscatter (X,Y,G,'rk','X',10);
hold on
for i = 1:length(groups)
errorbar(X(G==groups(i)),Y(G==groups(i)),E(G==groups(i)),'LineStyle','None','Color',colors{i})
end

How can I add vertical lines to my confusion matrix generated with matlab?

I have used the following code to generate my confusion matrix, which I have found it on internet :
confmat = C;
labels = {'0', '1', '2', '3', '11' };
numlabels = size(confmat, 1); % number of labels
confpercent = 100*confmat./repmat(sum(confmat, 1),numlabels,1);
imagesc(confpercent);
Mycolors=[0 0.7 0.4; 1 0.9 0.9 ]
colormap(flipud(Mycolors));
textStrings = num2str([confpercent(:)], '%.1f%%\n');
textStrings = strtrim(cellstr(textStrings));
[x,y] = meshgrid(1:numlabels);
hStrings = text(x(:),y(:),textStrings(:), ...
'HorizontalAlignment','center');
midValue = mean(get(gca,'CLim'));
textColors = repmat(confpercent(:) > midValue,1,3);
set(hStrings,{'Color'},num2cell(textColors,2));
set(gca,'XTick',1:numlabels,... 'XTickLabel',labels,... 'YTick',1:numlabels,... 'YTickLabel',labels,... 'TickLength',[0 0]);
I have gotten the next matrix
While I want to add vertical lines to my matrix to separate between values so I can get a similar one to the next :
I could get those vertical lines using pcolor(confusion_matrix) but the percentages are shiffted to the corner of each grid and I have got the next picture :
Use another axes object
The classic MATLAB trick when having to deal with different axes properties.
Basically we are going to create a new axes object, place it on top of the precious one, then make it transparent (no background color) so we can see what's behind, but we'll keep the grid lines well visible right where we want them.
So immediately after your sample code, add:
ax1 = gca ; % get handle of initial axes
ax2 = axes ; % create new axe and retrieve handle
lim = [0 numlabels] ; % Prepare X and Y properties
tks = 0:numlabels ;
% superpose the new axe on top, at the same position
set(ax2,'Position', get(ax1,'Position') );
% make it transparent (no color)
set(ax2,'Color','none') ;
% set the X and Y properties
set(ax2, ...
'XLim',lim,'XTick',tks,'XTickLabel','' ,...
'YLim',lim,'YTick',tks,'YTickLabel','' ) ;
% now set your grid properties
set(ax2,'GridColor','k','GridAlpha',1)
This will get you (data differ a bit because I randomly generated the confusion matrix):
Of course now you have full control over your grid lines, so you can also refine how they appear through the grid properties of the axes. A few interesting properties are:
GridLineStyle — Line style for grid lines
GridColor — Color of grid lines
GridAlpha — Grid-line transparency
LineWidth — Line width
For more details, look at the documentation for Axes Properties
Well !
I have found the requested thing to get the vertical and horizantal line, which is adding the lines simply using plot and hold on :
I have used the next code at the end of the mentioned one in my question :
hold on
plot ([2 2],get(gca, 'YLim'), 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot ([1 1],get(gca, 'YLim'), 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot ([3 3],get(gca, 'YLim'), 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot ([4 4],get(gca, 'YLim'), 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot (get(gca, 'XLim'), [1 1], 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot (get(gca, 'XLim'), [2 2], 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot (get(gca, 'XLim'), [3 3], 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot (get(gca, 'XLim'), [4 4], 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
I have used 1, 2, 3 and 4 because I have four classes and at the end of each class prediction results I need to plot the line.
Hope that will be useful

MATLAB: What is the difference between pzmap and pzplot?

What is the difference between pzmap and pzplot ?? Both are used to plot the poles and zeros of the LTI system.
For example:
Lets say I define my transfer function via tf command:
t = tf([2 5],[1 3 2])
Transfer Function
Continuous-time transfer function.
But when I try to plot the zero-pole-map via pzmap, I got:
pzmap(t)
And with pzplot, I got the same plot:
pzplot(t)
Plot with pzmap and pzplot
Both are the same plots. What is the difference then between pzmap and pzplot ??
pzplot allows you to customize your figure. For example, the marker size or line width. An example of how to customize your figure:
close all;clear;clc;
Ts = 1;
num = [1, 0.5, 0, 1]; % b
den = [0, 3, 2, 0]; % a
HZ = tf(num, den, Ts, 'variable', 'z^-1');
pzplot(HZ)
h = findobj(gca, 'type', 'line');
set(h, 'markersize', 9)
text(real(roots(num)) - 0.1, imag(roots(num)) + 0.1, 'Zero')
text(real(roots(den)) - 0.1, imag(roots(den)) + 0.1, 'Pole')
axis equal
However; pzmap command only maps the locations of zeros and poles.
Notice here that marker sizes are larger than default size set by MATLAB.

How to display two overlapping lines in matlab

I have to plot 5 lines that overlap in some regions, and I need to be able to see all the lines.
I can think of shifting the lines a bit to allow them to be displayed, but this doesn't seem a very elegante solution. Even so, how could I code such a thing?
Is there any other way to plot multiple overlapping lines while being able to distinguish them at every point?
For exemple, here is one exemple with 3 overlapping lines:
Thank you in advance!
Another way is to use transparency.
Unfortunatelly, line objects do not obey
transparency commands :(
A workaround is to:
1. download patchline (<-- link to Matlab Central)
2. use it to plot patchline with transparency
Once you have patchline, you can try something like:
% create some lines:
l1 = [1, 1, 1, 0, 0, 0.25, 1, 1, 0, 0, 0, 0, 1 1];
l2 = [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1];
l3 = [1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0];
% plot with patchline (notice the use of 'EdgeAlpha'):
figure;
patchline(1:length(l1), l1, [], 'EdgeColor', [0.8, 0.2, 0.35],...
'LineWidth', 5, 'EdgeAlpha', 0.5 );
hold on;
patchline(1:length(l1), l2, 'EdgeColor', [0.2, 0.7, 0.55],...
'LineWidth', 5, 'EdgeAlpha', 0.5 );
patchline(1:length(l1), l3, 'EdgeColor', [0.1, 0.2, 0.95],...
'LineWidth', 5, 'EdgeAlpha', 0.5);
% change y limits to see line overlap clearly
set(gca, 'YLim', [-0.5, 1.5]);
Not an ideal way to do it - the rough 'cracks' will stay this way,
but you can experiment with different line widths or moving the
lines in y axis by a value that would correspond to an image with each
line covering only half of it closest neighbour...
You may play with EraseMode property of the plot line. The following code example shows how to shift the lines and EraseMode effect:
% First we generate some data
NLines = 2;
NPoints = 50;
LineWidth = 3;
ShiftStep = 1.1;
XData = linspace(0,1,NPoints);
YData = rand(NPoints,NLines);
for k=1:NLines
YData(:,k) = YData(:,k) > (k/(NLines+1));
end
% Then we create plot
figure('color','w');
subplot(3,1,1); plot(XData,YData, 'LineWidth',LineWidth);
ylim([-0.1 1.1]);
title('simple')
subplot(3,1,2); plot(XData,YData+repmat((0:NLines-1)*ShiftStep,NPoints,1), 'LineWidth',LineWidth, 'EraseMode','xor');
ylim([-0.1 1.1+ShiftStep*(NLines-1)]);
title('Shifted vertically')
subplot(3,1,3); plot(XData,YData, 'LineWidth',LineWidth, 'EraseMode','xor');
ylim([-0.1 1.1]);
title('EraseMode = xor')
In my opinion if you have more than three lines similar to your plot, shifting is visually more attractive. Also you may create several axes (Like I did) and plot each line in its own axes, so they will have y-labels set accordingly, but their X-labels will be essentially the same.
You can use plot3 and assign different Z values to different overlapping lines. However, it'll look more like you expect (Z being the "up" direction) if you swap the Y and Z axes:
Example:
Y1 = randn(1,100);
Y2 = randn(1,100);
X = 1:100;
Z1 = 1*ones(size(X));
Z2 = 2*ones(size(X));
plot3(X,Z1,Y1);
hold on;
plot3(X,Z2,Y2);