Multiple Data Sets on the Same Scatter - matlab

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:

Related

Matlab show two fig files in one plot

I am trying to show two fig. files in one plot but it seems harder then expected. How can this be achieved?
Lets say I have two figs.
fig1 = openfig("someFig1.fig");
fig2 = openfig("someFig2.fig");
If you want to display them in the same plot, you can use copyobj:
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
savefig('test.fig')
y2 = cos(x);
plot(x, y2)
savefig('test2.fig')
fig1 = openfig("test.fig");
fig2 = openfig("test2.fig");
figure
h=subplot(1,1,1); %define subplot of 1x1
%copyobj of each figure on h subplot
copyobj(allchild(get(fig1,'CurrentAxes')),h)
copyobj(allchild(get(fig2,'CurrentAxes')),h)
Output:

Matlab: Plots and Subplots - How do I make Matlab zoom into all the subplots simultaneously?

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')

How to make a matlab legend recognize multiple scatter plots?

I want to place three scatter plots in the same figure window and have a legend that describes them. The scatter plots all load in the same window just fine, but the legend only recognizes the last series. In other words, the legend shows a red marker (the color for the last series) for each of its entries.
How do I make the legend recognize each scatter and not just the last one? I've tried a bunch of different things, and none of them seem to work. Thanks!
The picture is the plot for one of my datasets, note the legend.
s10 = scatter3(x1, y1, z1, 'b'); hold on;
s1 = scatter3(x2, y2, z2, 'g'); hold on;
s01 = scatter3(x3, y3, z3, 'r'); hold on;
legend([s10,s1,s01], {'10ms', '1ms', '0.1ms'})
% Every legend entry is red (pertains to the last series)
I can't reproduce the problem. Using your code above with random data seems to work (I did fix a typo, you need a comma after the first argument to legend):
x1 = rand(10, 1); y1 = rand(10, 1); z1 = rand(10, 1);
x2 = rand(10, 1); y2 = rand(10, 1); z2 = rand(10, 1);
x3 = rand(10, 1); y3 = rand(10, 1); z3 = rand(10, 1);
s10 = scatter3(x1, y1, z1, 'b'); hold on;
s1 = scatter3(x2, y2, z2, 'g'); hold on;
s01 = scatter3(x3, y3, z3, 'r'); hold on;
legend([s10,s1,s01], {'Series 10', 'Series 1', 'Series 01'})
For me this seems to be associated with the recent version of Matlab (R2015b); in this version I get the same problem as you with the legend entries showing only one color (in contrast to the other answers that can't reproduce the problem). But if I roll back to a previous version (R2010b), the problem goes away. I'm not sure if you have that option, but it might help diagnose the precise issue.
If the previous answer doesn't work, you can also exploit dynamic legends. This answer is what is inspiring this post: Dynamic Legend (Updates in every recursion). This is a rather undocumented feature but it does work very well. Basically, after each plot, you are able to dynamically update what the legend looks like without having to make one call to legend that has all of them together.
As such, try something like this. I'll borrow some of the previous answer's code to get me started:
x1 = rand(10, 1); y1 = rand(10, 1); z1 = rand(10, 1);
x2 = rand(10, 1); y2 = rand(10, 1); z2 = rand(10, 1);
x3 = rand(10, 1); y3 = rand(10, 1); z3 = rand(10, 1);
scatter3(x1, y1, z1, 'b', 'DisplayName', '10ms'); hold on;
legend('-DynamicLegend');
scatter3(x2, y2, z2, 'g', 'DisplayName', '1ms'); hold on;
legend('-DynamicLegend');
scatter3(x3, y3, z3, 'r','DisplayName', '0.1ms'); hold on;
legend('-DynamicLegend');
Call scatter3, then make sure that you use the 'DisplayName' flag and place what you would normally put in the appropriate legend spot. After each call to scatter3 after, you use the legend('-DynamicLegend'); command to signal to MATLAB that the legend entries will be forthcoming... you're going to specify them in the 'DisplayName' flag.
When you do that, this is the figure I get:
As a minor note, I can't reproduce your plot either. I get the same plot as the previous answer.
This issue is caused by a Matlab bug affecting version R2015b. It was fixed in R2016a. There is a bugreport here, which contains a patch and 3 alternative workarounds.
Here are the workarounds in case the link goes dead:
If you are unable to install the patch, there are three alternative workarounds:
If the CData of each scatter plot is an RGB triplet, then assign the MarkerEdgeColor or MarkerFaceColor of each scatter plot to the value of the CData:
s1 = scatter(1:10,1:10);
hold on
s2 = scatter(2:11,1:10);
s1.MarkerEdgeColor = s1.CData;
s2.MarkerEdgeColor = s2.CData;
legend('show');
Assign an RGB triplet to the MarkerEdgeColor or MarkerFaceColor of each scatter plot:
s1 = scatter(1:10,1:10);
hold on
s2 = scatter(2:11,1:10);
s1.MarkerEdgeColor = [0 0.4470 0.7410];
s2.MarkerEdgeColor = [0.8500 0.3250 0.0980];
legend('show');
Use this workaround when all the points within each scatter plot are the same color.
Call the legend function with two or more output arguments:
s1 = scatter(1:10,1:10,[],1:10);
hold on
s2 = scatter(2:11,1:10,[],26:35);
[h, ~] = legend('show');
Use this workaround when the points within each scatter plot are different colors.

How to plot contour from points coordinate in matlab

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.

formatting plotyy with two vectors for each y axis

I have a plotyy figure in MATLAB with 2 vectors in on each y axis.
plotyy(x1,[y1(:),y2(:)], x1,[y3(:),y4(:)])
I need to format each of the lines separately, but can not find the documentation about how to do this. Can someone please me show an example?
Does the following example code help?
%# Generate some data
N = 20;
X = (1:N)';
Y1 = randn(N, 1);
Y2 = randn(N, 1);
Y3 = randn(N, 1) - 50;
Y4 = randn(N, 1) - 50;
%# Perform the plotyy, returning an axes handle, and a handle for both figures
[Axes, fig1, fig2] = plotyy(X, [Y1 Y2], X, [Y3 Y4]);
%# Change the format of Y1 and Y2 (separately)
set(fig1(1), 'LineStyle', ':');
set(fig1(2), 'LineStyle', '--');
%# Change the format of Y3
set(fig2(1), 'LineStyle', '-.');
In the above code, the figure handle fig1 corresponds to the first y-plot, ie Y1 and Y2, and I can access the individual lines by indexing fig1 with 1 and 2.
Similarly, the figure handle fig2 corresponds to the second y-plot, ie Y3 and Y4, and I access Y3 by indexing this handle with 1. I could also access Y4 with fig2(2), if I so desired.