I'm trying to plot a few waveforms onto the same plot in Matlab; brand new to it. I've tried plotting them all together with plot() but it doesn't scale them all appropriately. How would I go about scaling them? I read something online that uses hold on, but I'm getting the same issue I believe. What's the simple solution here?
t1 = 0:0.1:1000;
y1 = t1.^5-5*t1.^3+4*t1;
plot(t1, y1)
hold on
t2 = 0:0.0001:0.01;
y2 = -8*exp(-1000*t2) + 3;
plot(t2, y2)
hold on
t3 = 0:0.0001:0.6;
y3 = exp(-10*t3).*cos(100*t3);
plot(t3, y3)
hold on
%plot(t1, y1, t2, y2, t3, y3)
Matlab is doing what you ask it to do: plotting everything to the same axis system (by the way, you only need to use hold on once, it is active until you change the axis or command hold off)
You have three options
define the limits of the axis explicitly, either separately using xlim([xmin xmax]) and ylim([ymin ymax]) or jointly using axis([xmin xmax ymin ymax]) (in your case e.g. axis([0 0.6 0 3.3])
you may want to use separate axis in one plot, see yyaxis yyaxis left / yyaxis right to activate the axes. Note that this only provides two different axes scales
use subplots! (as Cris Luengo already stated)
That would be in your case
% 1st subplot
t1 = 0:0.1:1000;
y1 = t1.^5-5*t1.^3+4*t1;
ax(1) = subplot(3,1,1);
plot(t1, y1)
% 2nd subplot
t2 = 0:0.0001:0.01;
y2 = -8*exp(-1000*t2) + 3;
ax(2) = subplot(3,1,2);
plot(t2, y2)
% 3rd subplot
t3 = 0:0.0001:0.6;
y3 = exp(-10*t3).*cos(100*t3);
ax(3) = subplot(3,1,3);
plot(t3, y3)
% link all axes (but only x direction)
linkaxes(ax,'x')
% your axes limits are so large that you probably don't want to link them
Related
I have multiple sub-plots. When I zoom into the first subplot to view a certain data set Matlab does not zoom the rest of the subplots. How do I make Matlab zoom into all subplots simultaneously?
subplot(2,1,1)
hold on
plot(Tim1.in,IA1.raw32SPC,'b-')
hold off
subplot(2,1,2)
hold on
plot(Tim1.in,IA1.cos,'r-*')
hold off
Since the x variable is the same, It makes sense to just link the x axes in this case. I added some code to create the x and y variables so that anyone can run the code below. You were also using hold unnecessarily.
x = 1:10;
y1 = 3*x;
y2 = x.^2;
ax(1) = subplot(2,1,1);
plot(x, y1, 'b-')
ax(2) = subplot(2,1,2);
plot(x, y2, 'r-*')
linkaxes(ax, 'x')
I can't find any info on how to do this on the Internet other than to use plotyy which only seems to work for two functions.
From Matlab documentation:
Use Right y-Axis for Two Data Sets
Plot three data sets using a graph with two y-axes. Plot one set of
data associated with the left y-axis. Plot two sets of data associated
with the right y-axis by using two-column matrices.
x = linspace(0,10);
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
y3 = 0.2*exp(-0.5*x).*sin(10*x);
plotyy(x,y1,[x',x'],[y2',y3']);
In my opinion, the way to do this that confers the most manual control is to create three overlapping axes with the plots you need, and only display the axis for the topmost one. You could even create 'empty' axes just so you they can serve as the only axis with defined 'limits' in the x and y axes.
Example:
ax1 = axes();
X1 = linspace(0,8*pi, 100); Y1 = sin(X1);
plot(X1, Y1, 'r', 'linewidth', 10);
ax2 = axes();
h = ezplot(#(x) x .* sin(x), [-100, 100]); set(h, 'color', 'w');
ax3 = axes();
image()
%% place them on top of each other by calling them in the order you want
axes(ax3); % bottommost
axes(ax1);
axes(ax2); % topmost
set(ax1, 'visible', 'off');
set(ax2, 'visible', 'off');
set(ax3, 'visible', 'on'); % this is the axes who's limits will show
I have to plot 1 line plot and 3 grouped scatter plots in a single plot window.
The following is the code I tried,
figure;
t1=0:0.1:10;
X = 2*sin(t1);
ts = 0:1:10;
Y1 = randi([0 1],length(ts),1);
Y2 = randi([0 1],length(ts),1);
Y3 = randi([0 1],length(ts),1);
plotyy(t1,X,[ts',ts',ts'],[Y1,Y2,Y3],'plot','scatter');
%plotyy(t1,X,[ts',ts',ts'],[Y1,Y2,Y3],'plot','plot');
The following are my questions,
The above code works if I replace 'scatter' by 'plot' (see commented out line), but 'scatter' works only for 1 data set and not for 3. Why?
How to individually assign colors to the 3 grouped scatter plots or plots?
Read the error message you're given:
Error using scatter (line 44) X and Y must be vectors of the same
length.
If you look at the documentation for scatter you'll see that the inputs must be vectors and you're attempting to pass arrays.
One option is to stack the vectors:
plotyy(t1,X,[ts';ts';ts'],[Y1;Y2;Y3],'plot','scatter');
But I don't know if this is what you're looking for, it certainly doesn't look like the commented line. You'll have to clarify what you want the final plot to look like.
As for the second question, I would honestly recommend not using plotyy. I may be biased but I've found it far to finicky for my tastes. The method I like to use is to stack multiple axes and plot to each one. This gives me full control over all of my graphics objects and plots.
For example:
t1=0:0.1:10;
X = 2*sin(t1);
ts = 0:1:10;
Y1 = randi([0 1],length(ts),1);
Y2 = randi([0 1],length(ts),1);
Y3 = randi([0 1],length(ts),1);
% Create axes & store handles
h.myfig = figure;
h.ax1 = axes('Parent', h.myfig, 'Box', 'off');
h.ax2 = axes('Parent', h.myfig, 'Position', h.ax1.Position, 'Color', 'none', 'YAxisLocation', 'Right');
% Preserve axes formatting
hold(h.ax1, 'on');
hold(h.ax2, 'on');
% Plot data
h.plot(1) = plot(h.ax1, t1, X);
h.scatter(1) = scatter(h.ax2, ts', Y1);
h.scatter(2) = scatter(h.ax2, ts', Y2);
h.scatter(3) = scatter(h.ax2, ts', Y3);
Gives you:
And now you have full control over all of the axes and line properties. Note that this assumes you have R2014b or newer in order to use the dot notation for accessing the Position property of h.ax1. If you are running an older version you can use get(h.ax1, 'Position') instead.
I know this question may sound a bit easy, but I couldn't find what I wanted in the documentation. Basically, I would like to know if a function exists in matlab which allows me to plot data sets y1, y2, ..., yn against the same x-axis in the same scatter diagram.
Any suggestions?
Thanks
You can just use scatter + hold on. For example,
x = rand(1,10);
y1 = rand(1,10);
y2 = rand(1,10);
y3 = rand(1,10);
figure; grid on;
hold on;
scatter(x, y1);
scatter(x, y2);
scatter(x, y3);
Gives:
I have an image and four points (x1,y1),(x2,y2),(x3,y3),(x4,y4). They are pixel coordinates of image. I want to generate one contour from points around image using Matlab. I try to make it but it does not accuracy. Which does implement better?
X=[x1 x2 x3 x4];
Y=[y1 y2 y3 y4];
BW1 = roipoly(I,X,Y); % I is an image
figure,
imagesc(uint8(I),[0 255]),colormap(gray);axis equal;axis off;
hold on,[c1,h1] = contour(BW1,[0 0],'r','linewidth',1.4); hold off
Maybe this is what you are trying to do (I am guessing here…):
X=[x1 x2 x3 x4];
Y=[y1 y2 y3 y4];
BW1 = roipoly(I,X,Y); % I is an image
figure
imagesc(uint8(I),[0 255])
colormap(gray);
axis equal;axis off;
hold on;
plot([X X(1)], [Y Y(1)], 'r', 'linewidth', 1.4); % <<<<< using plot instead of contour
or perhaps you need
[c1,h1] = contour(BW1,[0 0] + 0.5,'r','linewidth',1.4); hold off
This second case uses the fact that a contour of 0.5 will sit right on the boundary between 0 and 1, as needed. But I suspect that your BW1 image will overwrite the original image… sorry can't test right now and you left a lot to be guessed.