This may be an obviously simple question, but i'm not sure how to do this.
I have 4 calculated values stored in 4 variables, each representing a condition. I want to simply display each of these in a graph with the condition/variable on the X axis and the values on the Y axis. I have tried the code below, but it just gives me a blank figure with values but no line.
figure(1)
T = TA;
S = SA;
U = UA;
O = OA;
plot(T,S,U,O, '--o')
shg
Thanks in advance.
Try this
figure(1)
T = 12;
S = 7;
U = 5;
O = 10;
plot([T,S,U,O], '--o');
set(gca,'XTick',[1,2,3,4]);
set(gca,'XTickLabel',{'T','S','U','O'})
shg
for me this gave
Check matlab help on plot and specifically linespecs to define the format you would like the data plotted in. For example, code below plots each variable in different color with line and symbol.
figure;
hold on;
plot(T,'-bo');
plot(S,'-g.');
plot(U,'-rd');
plot(O,'-mx');
Related
I want to plot something like this http://www.nature.com/nprot/journal/v9/n6/fig_tab/nprot.2014.090_F7.html or like this one http://file.scirp.org/Html/11-2200285/ff6819f9-5db9-4121-852d-a8d5c302a5a4.jpg I have a 2D matrix.
I tried to plot it like this but did not work
figure;
hold on
for i = 1:size(X,1)
plot(X(i,:)+10)
end
Since you do not specify what exactly does not work, I am having some difficulties to answer your question directly (I do not have enough reputation for a comment).
The following code results in an image like the ones you link. Maybe you forgot to multiply the y-shift with the iteration number i.
N = 200;
x = 1:N;
M = 5;
X = sin(kron(x,ones(M,1)));
figure; hold all;
for i=1:N
plot(X(i,:)+i*5);
end
I would like make a plot that looks like this:
Namely a scatter plot with series of replicates connected via lines to the centroid.
I am not sure whether I saw it somewhere or whether I am simply getting glyphplot confused. I do not know what it is called, so Google has failed me —"cracked glass plot" is what I would personally call it.
In Matlab there is no native one line way to show a scatterplot with both x and y errorbars —yet Excel can. This makes me think there is a better way, such as this cracked glass plot.
Does it exist or do I need to write it myself?
I don't know of any native function to do this, but you can quite easily make use of line to plot something to fit your purposes, e.g.
function scatterPlotToCentroid(scatterPoints)
numScatters = size(scatterPoints,2);
scatterX = scatterPoints(1,:);
scatterY = scatterPoints(2,:);
centroid = mean(scatterPoints,2);
c1 = centroid(1);
c2 = centroid(2);
X = [repmat(c1,1,numScatters); scatterX];
Y = [repmat(c2,1,numScatters); scatterY];
hold on
line(X,Y,'Color','k');
scatter(scatterX, scatterY, 'r*');
end
Example call
% example: scatter sample (two groups)
numPoints = 10;
scatterDiff = 5;
scatterPointsA = 4+scatterDiff*(rand(2,numPoints)-0.5);
scatterPointsB = 8+scatterDiff*(rand(2,numPoints)-0.5);
% for each scatter sample group, plot scatter points
% and lines to centroid
hold on, box on
scatterPlotToCentroid(scatterPointsA)
scatterPlotToCentroid(scatterPointsB)
axis([0 12 0 12])
Example plot
Below follows the initial version of this answer, that left out the "centroid" part of the question (missed...), and instead generated random scatters around a given center-point; drawing lines from the latter to these scatter points.
function scatterPlotAt(centerPoint, numScatters, maxScatterSideLength)
c1 = centerPoint(1);
c2 = centerPoint(2);
scatterX = c1-maxScatterSideLength + ...
randi(2*maxScatterSideLength,1,numScatters);
scatterY = c2-maxScatterSideLength + ...
randi(2*maxScatterSideLength,1,numScatters);
X = [repmat(c1,1,numScatters); scatterX];
Y = [repmat(c2,1,numScatters); scatterY];
hold on
line(X,Y,'Color','k');
scatter(scatterX, scatterY, 'r*');
end
Example call
hold on, box on
scatterPlotAt([4; 4], 6, 3)
scatterPlotAt([8; 8], 6, 3)
axis([0 12 0 12])
Example result
This is a continuation of my previous problem, so now i want to know how to change array size and how to use size(out) = [m n].
so basically if you have 10x10 array and you want to plot column 9 vs. column 10 in scatter plot, and you want column 1-column 8 to be the labels of your scatter plot. how can i use size(out) = [10 10]?
for someone who want examples:
Auto-Label in scatter plot using matlab
and what if your array is m x n? is there a general code for this? please enlighten me, thanks.
To make things more general you can make use of the end keyword, which refers to the last row/column or an array/cell array/anything in Matlab (actually "last array of index").
Revisiting your example, you could use num2str (alternatively to sprintf) and use the following:
scatter(out(:,end-1), out(:,end));
for k = 1:size(out,1)
T{k} = num2str(out(k,1:end-2));
end
xshift = 0.03; yshift = 0.03;
text(out(:,3)+xshift, out(:,4)+yshift, T);
grid on
Which gives this:
I want to make a plot in Matlab like this.
How could I do something like this in Matlab?
Thank you all!
Make some data:
x = 1:0.1:100;
y = 1:5;
for i = y
z(i,:) = sin(i*x);
end
Plot it:
figure
hold on
for i = y
plot3(x,i*ones(size(x)),z(i,:))
end
Modify the plot aspect and view:
daspect([100,2,2])
view(45,60)
Does that do roughly what you need?
You can use the command plot3(X,Y,Z).
You have to build three matrices each one containing a number of column equal to the number of series you need. (6 in the figure you sent)
For example
X = repmat([-200:200]',1,6);
Z = rand(401,6)*10;
Y = ones(401,1)*[1:20:120];
plot3(X,Y,Z)
axis image
So i have this graph with these two gaussians created in matlab
This Graph have been created using the following bit of matlab code
TimeTakenWDriver = textread('TimeTakenWDriver.txt');
TimeTakenWODriver = textread('TimeTakenWODriver.txt');
fig1 = figure;
h1 = histfit(TimeTakenWDriver);
std1 = std(TimeTakenWDriver);
std2 = std(TimeTakenWODriver);
mean1 = mean(TimeTakenWDriver)
mean2 = mean(TimeTakenWODriver)
delete(h1(1));
set(h1(2),'color','b');
hold on;
h2 = histfit(TimeTakenWODriver);
delete(h2(1));
set(h2(2),'color','r');
Now i want to find the two y coordinates that corresponds to the two means "the two top-points" ive searched around but cant get any of the solutions i find to work because of the way i create my graphs. Any ideas?
You can try getting the data directly from the plot:
C = get(get(gca, 'Children'), 'YData');
and then find the peak value using max:
max(C(:))
Note that if you have two or more plots on the same axes (as in your example), C would be a cell array, so you need to access each cell separately to get the peaks:
cellfun(#(x)max(x(:)), C)