Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Plot 2D Graph with multiple labels

Subject: Plot 2D Graph with multiple labels

From: Norbert Szénási

Date: 11 Dec, 2010 09:52:05

Message: 1 of 4

Hi,
I'd like to ask you if you could help me with a plotting problem. I have an N by 3 matrix, I'd like to plot the data so that on the axis x will be the data from the 1st column of the matrix, on the y axis will be the second column of the matrix and the point in the graph would be represented by "." if the data in the third column is lesser then 50 and "x" if the data in the third column is bigger than 100. If the third column is between 50 and 100, the point doesn't show in the plot.

Data
====
x1 y1 9 -> "."
x2 y2 115 -> "x"
x3 y3 58 -> nothing
x4 y4 1191 -> "x"
...

Sorry to bother you with this question, but I tried to find the solution to this problem for several hours, but I was unsuccesful. Thanks.

Subject: Plot 2D Graph with multiple labels

From: Norbert

Date: 11 Dec, 2010 14:19:05

Message: 2 of 4

I finally found the solution to this problem, however it seems a bit complicated:

My matrix has size N by 3. I wrote 2 functions: return_lower(x, v) and return_higher(x, v).

function result = return_lower(x, v)
result = [];
for index = 1:rows(x)
# if the value in the 3rd vector is bigger then v
if (x(index, 3)>v)
result = [result;x(index, 1:2)];
endif
endfor
endfunction

The second function is very similar.
So, when I have my matrix called M, to show the desired plot I:
1, l = return_lower(M, 50)
2, h = return_higher(M, 100)
3, plot(l(:, 1), (:, 2), ".", h(:, 1), h(:, 2), "x")

Do you know of an easier way to do this? I am asking out curiosity. Thank you.

Subject: Plot 2D Graph with multiple labels

From: Ahmed Fasih

Date: 11 Dec, 2010 14:43:05

Message: 3 of 4


> I'd like to ask you if you could help me with a plotting problem. I have an N by 3 matrix, I'd like to plot the data so that on the axis x will be the data from the 1st column of the matrix, on the y axis will be the second column of the matrix and the point in the graph would be represented by "." if the data in the third column is lesser then 50 and "x" if the data in the third column is bigger than 100. If the third column is between 50 and 100, the point doesn't show in the plot.

The following minimal example shows the use of logical indexing [1] here.

x = linspace(0, 1, 20)';
y = x.^2;
z = rand(size(x))*150; % random numbers between 0 and 150
figure;
plot(x(z<50), y(z<50), '.', x(z>100), y(z>100), 'x')

Note that "mat = [x y z];" <-- this would be your N by 3 matrix.

[1] http://www.mathworks.com/help/techdoc/math/f1-85462.html#bq7egb6-1

Subject: Plot 2D Graph with multiple labels

From: Norbert

Date: 11 Dec, 2010 15:09:05

Message: 4 of 4

"Ahmed Fasih" <fasih.1NOSPAM@osu.edu.nospam> wrote in message <ie02lo$oc8$1@fred.mathworks.com>...
>
> > I'd like to ask you if you could help me with a plotting problem. I have an N by 3 matrix, I'd like to plot the data so that on the axis x will be the data from the 1st column of the matrix, on the y axis will be the second column of the matrix and the point in the graph would be represented by "." if the data in the third column is lesser then 50 and "x" if the data in the third column is bigger than 100. If the third column is between 50 and 100, the point doesn't show in the plot.
>
> The following minimal example shows the use of logical indexing [1] here.
>
> x = linspace(0, 1, 20)';
> y = x.^2;
> z = rand(size(x))*150; % random numbers between 0 and 150
> figure;
> plot(x(z<50), y(z<50), '.', x(z>100), y(z>100), 'x')
>
> Note that "mat = [x y z];" <-- this would be your N by 3 matrix.
>
> [1] http://www.mathworks.com/help/techdoc/math/f1-85462.html#bq7egb6-1

Much sorter, yet clear and simple. Thank you :-)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us