MATLAB Plot function of two variables on 2D Grid

Click For Summary

Discussion Overview

The discussion revolves around plotting a function of two variables using MATLAB, specifically how to visualize outputs from a function that returns binary values based on pairs of input variables. Participants explore methods for creating a 2D grid plot where each pairing of independent variables is represented by a colored dot.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant seeks assistance in plotting a function that outputs binary values for combinations of two independent variables, x and y, and questions the feasibility of visualizing this in 2D.
  • Another participant suggests using the scatter function in MATLAB, providing an example code snippet to illustrate how to create a scatter plot with colored dots based on a color vector.
  • A follow-up post raises a concern regarding the requirement for the color vector C to match the lengths of X and Y, noting that this poses a challenge when plotting multiple points for each combination of x and y.
  • Another participant clarifies that the lengths of X, Y, and C should correspond to the total number of points to be plotted and provides a method to generate the necessary vectors using MATLAB functions like kron and repmat.

Areas of Agreement / Disagreement

Participants generally agree on the use of scatter plots for visualizing the data, but there is a lack of consensus on how to handle the dimensionality of the input vectors and the corresponding color vector for the scatter function.

Contextual Notes

Participants discuss specific MATLAB functions and their requirements, highlighting potential limitations in vector lengths and the need for generating appropriate input data structures for plotting.

etothex
Messages
2
Reaction score
0
Hey all - this is my first post on the forum but I've browsed for help for a really long time on other topics. Any help would be greatly appreciated!

So I have a function, called stability.m that takes in two independent variables, say x and y, as input and outputs either a 1 or a 0 for each pairing of x and y. I would like to set up a plot as x versus y that plots each pairing as a colored dot (simply all 1's = black and all 0's = red, for instance). I would more or less rather like a grid/matrix so I can easily see what each possible pairing would represent for broad ranges of x and y (IE, x and y = 1x100 vectors).

I was wondering if this is even possible. I have read through tons of tutorials regarding mesh plots, scatter plots, etc and have found most to say that functions of two variables can only be plotted on a 3d graph. Is this true?

Thanks for the help
- etothex
 
Last edited:
Physics news on Phys.org
Last edited by a moderator:
MisterX said:
You can do that using http://www.mathworks.com/help/techdoc/ref/scatter.html", with C being vector with the same size as X and Y.


Code:
X = 0:7;
Y = 0:7;

colormap(hsv(256));
C = (0:7)/8;

scatter(X, Y, 4, C, 'filled');

You can also display 2D matrices using http://www.mathworks.com/help/techdoc/ref/image.html".

Okay got a little further with the image function. however, with scatter, the constraint that C needs to be of same length as X, Y won't be good here because if X and Y are length 4, then there will be 16 points that need to be plotted on the graph. (X=1, Y=1:4 then X=2, Y=1:4 and so on). Is there a way around this?
 
Last edited by a moderator:
The length of X and the length of Y and the length of C should all be equal to the number of points (since you want a different color for each point). By X, Y, and C, I mean the parameters to the scatter function.

So, using the example you gave, one might think of the probem as, given the two length 4 vectors, how does one use MATLAB to generate the two 16 length vectors. Here is one way to do it:

Code:
x = 1:4;
y = 1:4;

X = kron(x,ones(size(y))); %repeats each element (a bit like interpolation)
Y = repmat(y, size(x)); %repeats the whole vector

colormap(hsv(length(X)));

C = (0:(length(X) - 1))/length(X);
scatter(X, Y, 4, C, 'filled');
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K