Contour plot of x and y

7 次查看(过去 30 天)
Salem
Salem 2021-12-12
评论: Salem 2021-12-13
I have x and y data and I want to plot a contour with three colors where each category has its own color. I couldnt figure out a way to plot a contour from x and y data. Note that Im gonna divide y values into three classifications based on three ranges value.
  6 个评论
Star Strider
Star Strider 2021-12-12
What’s a ‘contour bar’?
.
Salem
Salem 2021-12-12
A plot may look like this image, except some colors might apear more than once. So the overall plot will look like a contour bar. where the x-axis is the x value and the color will represent the categories that Im going to divide the y value into.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-12-12
ncopy = 20;
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/832185/Book1.xlsx';
XY = readmatrix(filename);
X = XY(:,1);
Y = 1:ncopy;
Z = XY(:,2*ones(1,ncopy)).';
plot(X, Z)
level = discretize(Z, [0 : .025 : 1]);
imagesc(X, Y, level); colormap(jet);
Your data is too noisy to make for a nice plot like your sample lot.
In the discretize() call, it is not necessary for the bins to be equal size. You could, for example, use [0, 0.1, 0.2, 1]

更多回答(2 个)

John D'Errico
John D'Errico 2021-12-12
编辑:John D'Errico 2021-12-12
I'm not sure what you mean.
A contour plot is a specific type of plot. A contour is a level surface, that is, the set of points in the plane where z(x,y) is equal to some fixed value.
All you have said is you have (x,y) data, scattered in the plane apparently. And if all you have are (x,y) pairs, then there is nothing to denote z.
But then you talk about categories. So, does each point have one of three categories? And now you want to delineate the region of the plane where the points come from category 1, then a separate region where all the points have category, 2, etc? If this is the case, is the bounary between regions a fuzzy one? So some points in the boundary region may belong to different categories? In that case, it becomes more difficult of course.
A simple solution is to just use essentially a scatter plot. So plot each category point with a distinct symbol. You can even use different colored symbols for each category. So a red o might be category 1, a green square category 2, and a blue + category 3. This is trivially done.
If you want better help, then it would help if you actually provide your data. Just attach it in a .mat file, to a COMMENT, or to your original question.
  3 个评论
Salem
Salem 2021-12-12
the output it is going to look like a contour bar. So simply I can present the three categories as colors rather than a scatter plot.
John D'Errico
John D'Errico 2021-12-12
编辑:John D'Errico 2021-12-12
Again, WITHOUT SEEING YOUR DATA, IT IS IMPOSSIBLE TO GIVE BETTER HELP.
Is there a fuzzy region between categories, where points may belong to either category? That is, are you CERTAIN about which group any point belongs? Even if you are certain, is there a fuzzy boundary? So some points may be included islands inside the neighboring category?
Next, while there is a boundary between categories 1 and 2, and between 2 and 3, may categories 1 and 3 ever touch each other? Or must category 2 always lie between 1 and 3?
These are questions you have not answered, but your responses would stringly influence how I might treat the problem.

请先登录,再进行评论。


John D'Errico
John D'Errico 2021-12-12
Since you have not (yet) shown me any data or answered my questions, I'll offer one simple solution, just making guesses about the answers to my questions.
The peaks function is a nice one to use. First, some scattered points in the domain of interest...
n = 500;
x = rand(n,1)*6 - 3; % where the peaks function does something interesting
y = rand(n,1)*6 - 3;
z = peaks(x,y) + randn(size(x))/20;
% Categories
% category 1: 1 <= z
% category 2: 0 <= z < 1
% category 3: z < -1
% Note the addition of noise onto z makes the boundaries essentially fuzzy.
% also nots that category 2 ALWAYS lies between category 1 and 3.
C = 1 + (z >= -1) + (z >= 2);
% we can plot the points in 2-d. (I could probably do this with scatter,
% but I am too lazy)
plot(x(C==1),y(C==1),'ro',x(C==2),y(C==2),'gs',x(C==3),y(C==3),'b+')
As you should see, the boundaries between regions are fuzzy. And while there should always be a green region (cat 2) between categories 1 and 3, it may be difficult to locate the exact location. As bad, this is a relatively lot of data, and there are still many issues.
Can we do anything? Well, we can take a wild shot.
[Xg,Yg] = meshgrid(linspace(-3,3),linspace(-3,3));
fun = scatteredInterpolant([x,y],z,'nearest');
Cg = fun(Xg,Yg);
Now, create a custom color map, that transitions from blue, to green, to red in a specific way.
B = [0 0 1];
G = [0 1 0];
R = [1 0 0];
CM0 = [B;B;G;G;R;R];
CM = interp1([1; 1.75; 1.9 ; 2.1; 2.25; 3],CM0,linspace(1,3,100));
H = pcolor(Xg,Yg,Cg);colormap(CM),H.EdgeColor = 'none';
A problem is there are clearly regions of ambiguity where the interpolation has problems. Call them regions of confusion.
hold on
fimplicit(@(x,y) peaks(x,y) - 1,'k')
fimplicit(@(x,y) peaks(x,y) - -1,'k')
The black lines I've drawn lie at -1 and 1, that is, the theoretical boundaries where the function transitioned between categories, so we can see how it did.
With some effort, you MAY be able to make such a scheme work. That is your problem, not mine. :)
  1 个评论
Salem
Salem 2021-12-12
I have uploaded a sample of the data and the plot. what I want is a contour plot looks like a bar. Where y values will be divided into categories then the color of the contour shows the value of y belongs to which category at specific location of x. Sorry I didnt provide this before as it wasnt available.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by