Legend in multiple plots Matlab - matlab

How can I put the legend on each graph separately like (figure 2) instead of putting the legend on the side like (figure 1) ?
Figure 1
Figure 2

Probably the easiest thing would be to use the text(x,y,textstring) function to put some text at the final data points of each of your curves (e.g. x, y point of largest x, for each curve). The arguments x, y can be vectors, and the argument textstring can be a cell array of strings.
Create an array of final data points separately for x and y.
xcoords = [x0_final x1_final ...xn_final];
ycoords = [y0_final y1_final ...ym_final];
Create a cell array containing your legend strings of the same length as those xcoord and ycoord arrays
legend_strings = {'T0 = 0.5', 'T0 = 0.7' ...};
Then a call to text(xcoords,ycoords,legend_strings) after your plot should do the labeling you want.

The best way to comment on each line would be to add labels or text instead of legend.
For instance:
1)
labeledge(h,s,t,'T_0=1.5s')
2)
txt = texlabel('T_0=1.5s')
text=text(1,1.00E-04,txt)

Related

No visible points for plot in for loop

I'm struggling with a plot I want to make using a for-loop.
I know it works when I add it after the loop (just a simple plot). But I want to try it in this other way.
fib = ones(1:10);
for k=3:10
hold on
fib(k) = fib(k-1) + fib(k-2);
plot(k,fib(k))
end
hold off
The output is a plot, but there are no points visible.
You need to specify a marker. The documentation says:
If one of X or Y is a scalar and the other is either a scalar or a vector, then the plot function plots discrete points. However, to see the points you must specify a marker symbol, for example, plot(X,Y,'o')
So it will be:
plot(k,fib(k),'o');
Also note that you're creating a 10-dimensional array with fib = ones(1:10);. You most probably meant to write a comma instead of colon in between 1 and 10 to create a row vector. i.e.
fib = ones(1,10);
or a column vector as HansHirse suggested:
fib = ones(10,1);

How to set arbitrary colors for bars in a 3D bar plot?

Say that I have a matrix Z with some values, and I want to illustrate it by a plotting the values in Z by height. The first solution comes to mind is a surface, but using surf and similar functions with small matrices doesn't look good.
So I thought about using something like a 3D bar plot with bar3. But the problem is that this function always sets the color by the group and not by height, and I can't get it to do so.
Here is an example:
Z = peaks(5);
subplot 121
surf(Z)
title('Surface look bad')
subplot 122
bar3(Z)
title('The color is not by height')
I tried to look for the color properties in the handles returned by bar3 (like CData and FaceColor) but got lost with all the values and how they relate to the bars themselves.
Ultimately, I would like to have a general solution that for 2 matrices Z and C I can create a 3D bar plot with bars in height given by Z and color given by C.
How can I do so?
The function bar3 returns a surface object, one for each group (i.e. one for each color), so all the bars in one group are essentially plotted as one 'broken' surface. This is explained very good in this answer, so I won't repeat it here.
Instead, I'll get to the solution for this specific problem. The relevant property of the surface is CData. When we create the bar plot, each surface's CData is assigned with a matrix in some size (we'll get to this) that is all equal one value. A different value for each surface. This is how the figure as a whole translates its color map to the color of the groups.
As written above (and elaborated in the linked answer), each group represented by a surface, so it takes a whole matrix to define the color at each point of the surface. The first thing we want to do is to get this matrix size:
Z = peaks(5);
bar_h = bar3(Z);
% we take only the first one, but they are all the same size:
cdata_sz = size(bar_h(1).CData)
cdata_sz =
30 4
CData has always 4 columns (see here why), and the number of rows is always 6*number of groups. This is because it takes 5 vertices to create one closed rectangle with an area object (the last vertex is like the first one) and one line is for spacing between the bars with NaNs, so they will look separated.
Next, we need to enlarge our original colormap (which is the same size of Z) to fit CData in the right way. Essentially, we just want to repeat the same value for all vertices that belong to the same bar. Assuming Z is also our color data (i.e. we color by height) we do:
z_color = repelem(Z,6,4)
Now we need to split our z_color to different cells in the number of our groups. Each cell will contain the coloring data for one surface object:
z_color = mat2cell(z_color,cdata_sz(1),ones(1,size(Z,2))*cdata_sz(2));
And finally, we apply the new color data to the bar plot:
set(bar_h,{'CData'},z_color.')
As a bonus, if we want to remove all zero values from our bar, it can be done easily by setting them to NaN:
Z(abs(Z)<eps) = nan;
C(isnan(Z)) = nan; % if we use a colormap C different from Z
All the above could be boiled down to this handy function:
function bar_h = Cbar3(Z,C,b,y)
% Z - The data
% C - CData (if other then Z values)
% b - Minimum absolute value to keep colored
% y - y-axis values to order the data by
if nargin<2, C = Z; end
if nargin<3 || isempty(b), b = 0; end
Z(abs(Z)<b) = nan;
C(isnan(Z)) = nan;
if nargin<4
bar_h = bar3(Z);
else
bar_h = bar3(y,Z);
end
cdata_sz = size(bar_h(1).CData);
z_color = repelem(C,6,4);
z_color = mat2cell(z_color,...
cdata_sz(1),ones(1,size(Z,2))*cdata_sz(2));
set(bar_h,{'CData'},z_color.')
end
Example of usage:
subplot 121
Z = peaks(30);
Cbar3(Z,Z,0.5);
pbaspect auto
shading flat % just to get a cleaner look
title('Cbar3 using height as color')
subplot 122
Cbar3(Z,rand(size(Z)),0.5);
pbaspect auto
shading flat % just to get a cleaner look
title('Cbar3 using random as color')
Result:
This is a partial answer.
The case of using the bar height as color is covered by the official MATLAB documentation. Essentially the example code boils down to:
function q45423394
hB = bar3(peaks(25)); colorbar;
for indE = 1:numel(hB)
hB(indE).CData = hB(indE).ZData;
end
All you need to do afterwards is make sure that the colormap is the one you want.
While I find EBH's solution aesthetically more pleasing, here there is a simpler solution: interpolation
z = peaks(5);
[x,y]=meshgrid(1:0.1:size(z,1),1:0.1:size(z,2));
zn=interp2(z,x,y,'nearest');
% plot it
surf(zn,'edgecolor','none','facecolor','interp')

How to get length of YTickLabels in MATLAB?

I have a MATLAB subplot figure. I need the YLabels to left align justify. To do this I am setting the Position property for each ylabel. My problem is the subplots are being created programmatically and therefore I don't know what to set the position as.
In MATLAB I want to use the longest/widest YTickLabel as a reference point for positioning. To do that I want to get the length of each label. I am able to get the YTickLabels by doing:
% Set Label format as string
set(gca, 'YTickLabel', num2str(transpose(get(gca, 'YTick'))))
% Get axis YTickLabels
ax = gca;
labels = get(ax, 'YTickLabel');
% Print labels to console
disp(labels)
I would like to iterate through the labels and find the length of the longest label. I've tried accessing them as a cell array but get 'Cell contents reference from a non-cell array object error.' And when I try matrix indexing nothing prints.
Does anyone know if it is possible to get the length of each individual YTickLabel value?
Useful info:
MATLAB R2014b
By "length of each individual YTickLabel value" I understand that you wish to get the number of characters forming each label.
It's quite easy using the numel function, which outputs the number of elements in a cell for an example. Since labels are stored in a cell array, we can use the fancy function cellfun to apply numel to each cell, then convert to a numeric array with cell2mat
In short you can use this:
LabelLength = cell2mat(cellfun(#(x) numel(x),labels,'uni',0))
here is some sample code to illustrate:
clear
clc
close all
x = 1:5;
y = rand(size(x));
scatter(x,y,40,'r','filled')
set(gca,'YTick',[1 3 5],'YTickLabel',{'One';'ThisIsThree';'AndFive'})
grid on
labels = get(gca,'YTickLabel')
LabelLength = cell2mat(cellfun(#(x) numel(x),labels,'uni',0))
and output:
LabelLength =
3
11
7
You could replace cellfun with this equivalent for-loop:
LabelLength = zeros(numel(labels),1);
for k = 1:numel(labels)
LabelLength(k) = numel(labels{k});
end
LabelLength
Note that as a workaround offering quite a lot of flexibility, you could replace the YTickLabels by text objects, for which you can set the HorizontalAlignment property to left for the text to be left-justified.
Hope that helps!

How to mark co-ordinates of points in Octave using "plot" command?

I am using the plot command in Octave to plot y versus x values. Is there a way such that the graph also shows the (x,y) value of every coordinate plotted on the graph itself?
I've tried using the help command but I couldn't find any such option.
Also, if it isn't possible, is there a way to do this using any other plotting function?
Have you tried displaying a text box at each coordinate?
Assuming x and y are co-ordinates already stored in MATLAB, you could do something like this:
plot(x, y, 'b.');
for i = 1 : numel(x) %// x and y are the same lengths
text(x(i), y(i), ['(' num2str(x(i)) ',' num2str(y(i)) ')']);
end
The above code will take each point in your graph and place a text box (with no borders) in the format of (x,y) where x and y are the coordinates for all of the points.
Note: You may have to play around with the position of the text boxes, because the above code will place each text box right on top of each pair of co-ordinates. You can play around by adding/subtracting appropriate constants in the first and second parameters of the text function. (i.e. text(x(i) + 1, y(i) - 1, .......); However, if you want something quick and for illustrative purposes, then the above code will be just fine.
Here's a method I've used to do something similar. Generating the strings for the labels is the most awkward part, really (I only had a single number per label, which is a lot simpler)
x = rand(1, 10);
y = rand(1, 10);
scatter(x, y);
% create a text object for each point
t = text(x, y, '');
% generate a cell array of labels - x and y must be row vectors in this case
c = strsplit(sprintf('%.2g,%.2g\n',[x;y]), '\n');
c(end) = []; % the final \n gives us an extra empty cell, remove it
c = c'; % transpose to match the dimensions of t
% assign each label to each text object
set(t, {'String'}, c);
You may want to play around with various properties like 'HorizontalAlignment', 'VerticalAlignment' and 'Margin' to tweak the label positions to your liking.
After a little more thought, here's an alternative, more robust way to generate a suitable cell array of coordinate labels:
c = num2cell([x(:) y(:)], 2);
c = cellfun(#(x) sprintf('%.2g,%.2g',x), c, 'UniformOutput', false);
There is a very useful package labelpoints
in MathWorks File Exchange that does exactly that.
It has a lot of convenient features.

Extract information from a 3-D graph

I used the plot3 method for creating a 3-D graph. Now I want to extract all the points with z > 0.
How can I do this?
First you need to have the data used to make the plot; if you have them directly, that is the simple case. If not - for example, if you have a plot from some other script, or a figure file saved by someone else that you're just loading - you can get the data from the plot like this:
%# make sure the plot is the current axes object by clicking on it
%# or else use the actual axes handle instead of gca
X = get(gca,'xdata');
Y = get(gca,'ydata');
Z = get(gca,'zdata');
Next, use logical indexing:
index = Z > 0;
X_of_interest = X(index);
Y_of_interest = Y(index);
Z_of_interest = Z(index);
The new variables contain the X,Y,Z values of all points where the condition Z>0 is true.