In Matlab, when I want to plot each row of a n x m matrix A as a line, I do
plot(A');
One problem for me is the x-labels which are indices from 1 to number of variables.
I want to change those labels to more meaningful values from, say, a vector B.
So I tried following statement
plot(repmat(B,1,size(A,1)),A');
but the chart looks totally different. I know I can use 'XTickLabel' but it does not work with line plot of matrix, meaning no effect of 'XTickLabel'. Any idea how I can put labels correctly?
You could use something along the lines of:
>>
A = [
1 2 3 4
5 6 7 8
9 8 7 6
5 4 3 2
];
>>
B = [
15 30 45 60
];
>> plot(A')
>> set(gca, 'XTick', 1:numel(B))
>> set(gca, 'XTickLabel', cellstr(num2str(B'))')
This would give you:
You could try this as well
x = 0:0.1:1;
A = [ x.*x ; exp(-x) ]
plot( x, A' )
Related
I want to plot vertical lines corresponding to values obtained at specific time points.
Example:
a = [0 5 7 9 ] at 0 seconds,
b = [0.5 6 6.5 11] at 2 seconds,
c = [0 4 2 10] at 4 seconds
Each time point will be a vertical line between the maximum and minimum of the vectors. I also need to mark the start and end points of a, b and c, for instance a should have a circle (or star etc.) at 0 and 9.
Here is an example output:
You can use line with end markers.
% Your data
a = [0 5 7 9 ];
b = [0.5 6 6.5 11];
c = [0 4 2 10];
% Combine to get min/max values
data = [a; b; c].';
mins = min(data);
maxs = max(data);
% Plot using line, nice flexible method which plots vertical lines at points 2:2:n
line(repmat(2*(0:numel(mins)-1), 2, 1), [mins; maxs], 'color', 'k', 'marker', 'o')
Output:
If you want different markers on each end, or different colours, please see this answer which gives more detailed examples.
I'm trying to plot some data on Matlab using the following code:
x = [1 2 5 6 7 9]
y1 = [1 2 3 2 1 2]
y2 = [2 2 2 1 3 3]
y3 = [1 1 2 3 1 1]
plot(x,y1,'--.','markersize',20); hold on;
plot(x,y2,'--.','markersize',20); hold on;
plot(x,y3,'--.','markersize',20); hold off;
legend('y1','y2','y3');
xlim([1 9]);
ylim([0 4]);
And I'm getting the following result:
Note that I have no Y values for the X positions 3, 4 and 8, but the X axis is still showing these X values in the graph.
There is some way that I can ignore the positions 3, 4 and 8 in the X-axis, and show only the Y values for the X positions 1, 2, 5, 6, 7 and 9?
I can use the following command to 'hide' these positions:
set(gca, 'XTick', x);
But the gaps related to these positions are still there.
Update:
This is the graph I'm trying to create (it was created on paint):
Note: in my case, the X-axis just represents the IDs of some images, and because of that I just need to show the numbers.
You can get the plot you want by first leaving x out in the calls to plot (so it plots against the array index) then altering the XTick and XTickLabel properties of the axes (and adjusting the x limit slightly):
plot(y1,'--.','markersize',20); hold on;
plot(y2,'--.','markersize',20);
plot(y3,'--.','markersize',20);
legend('y1','y2','y3');
xlim([1 numel(x)]); % Note numel is used here
ylim([0 4]);
set(gca, 'XTick', 1:numel(x), 'XTickLabel', cellstr(num2str(x(:))));
I am not sure if you need gaps between plot lines. If you wish no values to be plotted there, try replacing the values by nan. For example,
x = [1 2 3 4 5 6 7 8 9]
y1 = [1 2 nan nan 3 2 1 nan 2]
y2 = [2 2 nan nan 2 1 3 nan 3]
y3 = [1 1 nan nan 2 3 1 nan 1]
plot(x,y1,'--.','markersize',20); hold on;
plot(x,y2,'--.','markersize',20); hold on;
plot(x,y3,'--.','markersize',20); hold off;
legend('y1','y2','y3');
xlim([1 9]);
ylim([0 4]);
This will give a plot like this:
Sounds like you need stem.
stem(x, y1);
legend('y1');
d=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
bar_widh=0.2;
figure;
h = bar(d,bar_widh);
for i=1:2:4
for j=1:4
x=d(i,j)
y=d(i+1,j)
figure,plot(x,y);
end
i=i+1;
end
In this code I had plotted a bar chart and I want to extract values from bar chart. (Values are correctly extracted but only one point is plotted; not a line) But i'm getting wrong results.
My aim is to plot a line between
d(1,1) and d(2,1);d(3,1) and d(4,1);
d(1,2) and d(2,2);d(3,2) and d(4,2);
d(1,3) and d(2,3);d(3,3) and d(4,3);
d(1,4) and d(2,4);d(3,4) and d(4,4);
In first figure I need 2 lines(from 1 column); in second figure I need 2 lines(from 2 column); in third figure I need 2 lines(from 3 column) and in fourth figure I need 2 lines(from 4 column).
no.of figures=no.of columns
Version 2
I tried another version
d=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
bar_widh=0.2;
figure;
h = bar(d,bar_widh);
saveas(h,'testfigure.fig');
clear
close all
h=hgload('testfigure.fig');
ch=get(h,'Children');
l=get(ch,'Children');
x=get(l,'Xdata');
y=get(l,'Ydata');
and i'm getting error as
Error using get
Conversion to double from cell is not possible.
Error in Untitled5 (line 10)
l=get(ch,'Children');
d=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16];
bar_widh=0.2;
figure;
h = bar(d,bar_widh);
figure; hold on;
for i = 1:size(d,2)
x = [d(1,i) d(2,i)];
y = [d(3,i) d(4,i)];
plot(x,y);
end
To plot a line, you must make sure the parameters x and y in plot(x,y) are vectors than scalars. For example, to plot a line between P1 = [P1x, P1y] and P2 = [P2x, P2y], the paramenters should be:
x = [P1x, P2x]; y = [P1y, P2y];
I have two matrix as
A = [1 2 3; 4 6 7; 3 6 7]
B = [2 5 6; 2 8 7; 2 8 5]
I want to plot a graph between this two matrix, I mean in such as way that A(1,1) as x coordinate and B(1,1) as Y coordinate of 1st point. Similarly, for 2nd point A(1,2) as x and B(1,2) as Y and so on. At last I should get straight line connecting this point for each row.
And then I have the measure the length of the line connecting all the points for each row, so that I can know which row have greater length
I tried this
for i=1:1:3
plot(A(i,:),B(i,:)), hold on;
end
Is it correct because I can't able to interpret and how to measure the length also??
Your way of plotting seems correct.
To calculate the length of each line I would use this code:
for i=1:1:3
len(i) = sum(sqrt(diff(A(i,:),1).^2+diff(B(i,:)).^2));
end
You don't need for loop to plot. Just do.
A = [1 2 3; 4 6 7; 3 6 7];
B= [2 5 6; 2 8 7; 2 8 5];
% Plot lines
plot(A.',B.');
% Calculate length of lines
length=sum(sqrt((diff(A,1,2).^2)+(diff(B,1,2).^2)),2);
I have a vector of integers that vary from 1 to 4.
A=[1 2 3 4 2 3 2 1 2 3 4 4]
I would like to plot A with different colors for each value...
the vertical line that links A(1) to A(2) should have the color of the first value (in this case 1).
Is that possible?
and how to handle the case of NaN present in the vector?
I should plot A against a time vector
A = [1 1 1 NaN 4 4 4 Nan 2 2 3 3];
time = [1 2 3 4 5 6 7 8 9 10 11 12];
Suppose you have the following set of colors:
col = hsv(4);
You set the order of the colors based on the values of A:
figure();
set(gca, 'ColorOrder', col(A,:), 'NextPlot', 'replacechildren');
Then, you can plot each line in the desired color:
n = numel(A);
plot(hankel(0:1,1:n-1),hankel(A(1:2),A(2:n)))
This results in:
Edit:
The hankel approach is a bit like shooting with a bazooka to kill a mosquito, as we say in the Netherlands. Anyway, I learned about it a few questions ago - so I liked to use it. See the post of Dan for a simpler alternative for the plotting. Still, setting the correct colors can be done as in the above.
You can do it using just a touch of trickery:
A=[1 2 3 4 2 3 2 1 2 3 4 4]
x = [1:numel(A)-1; 2:numel(A)];
y = A(x);
plot(x,y)