2D Filled Colour Plot

I have the following 3d equation:
z = (x^2 * y)^((1-1.1)/2)
and I want to plot it, as a colouredcontour plot, under the following conditions, on a 2d x-y plane:
if z>1, shade is one colour; if z<1, plot is another colour.
Having never either peviously worked with three-dimensional plots orcontourf, I was wondering if I could get some guidance because I am a bit lost? :(
Thanks in advance!

 采纳的回答

This works:
z = @(x,y) (x.^2 .* y).^((1-1.1)/2);
x = linspace(0, 2);
[X,Y] = meshgrid(x);
Z = z(X,Y);
figure(1)
contourf(X, Y, Z, [0 1])
colormap([1 0 0; 0 1 0])
See the documentation for colormap for details on how to change the colours.
The plot:

14 个评论

Thank you so much! Excellent.
My pleasure!
Thank you.
But how would I plot something (a line plot) over that?
This works:
z = @(x,y) (x.^2 .* y).^((1-1.1)/2);
x = linspace(0, 2);
[X,Y] = meshgrid(x);
Z = z(X,Y);
s = sin(pi*x/2) .* cos(20*x) + 1;
figure(1)
contourf(X, Y, Z, [0 1])
colormap([1 0 0; 0 1 0])
hold on
plot(x, s, 'b')
hold off
I added ‘s’ as an arbitrary function of ‘x’ out of convenience. The only real constraint is that your line plot has to restrict itself to the limits of the coutourf plot, or it looks strange. In this instance, the contour plot is restricted by the values of ‘x’ that define it, that because of the fractional power, must be restricted to real non-negative numbers.
Applying your idea (badly) I did this:
z = @(x,y) (x.^2 .* y).^((1-1.1)/2);
x = linspace(0, 6);
[X,Y] = meshgrid(x);
Z = z(X,Y);
figure(1)
contourf(X, Y, Z, [0 1])
colormap([1 0 0; 0 1 0])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) -6/2',[0 6 0 6])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) -5/2',[0 6 0 6])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) -4/2',[0 6 0 6])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) -3/2',[0 6 0 6])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) -2/2',[0 6 0 6])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) -1/2',[0 6 0 6])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) -0/2',[0 6 0 6])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) +1/2',[0 6 0 6])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) +2/2',[0 6 0 6])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) +3/2',[0 6 0 6])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) +4/2',[0 6 0 6])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) +5/2',[0 6 0 6])
hold on
ezplot('(y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) +6/2',[0 6 0 6])
hold off
the idea being to overlay an inhomogenous plot for multiple values of a final additive constant C, fluxuating b/w 3 and -3 w/ .5 step increments. My graph, tho, when applying the above, retains the contour line but with an all green background, changing the ezplot plot lines to red instead?
And thanks so much for this help. I really appreciate it.
Before you go any farther, add this after figure(1) and run it to see the result:
figure(2)
meshc(X, Y, Z)
grid on
I’m having a difficult time understanding what you are doing, but your ‘Z’ only go from about 0.75 to about 1.2 (and +Inf at X=0 and Y=0). They don’t extend from -3 to +3. There would only be two visible contour lines, the rest would either be squashed against the axes or off the plot.
I'm sorry. I didn't explain that too well.
I'm plotting two separate things on the same graph. One is the equation contained in the original post, the one you expertly plotted for me, and the other is the following: (y^2)/2 + (((x^2)*y)^(1-1.1))/(1.1-1) - 2/x - 1/(1.1-1) + C = 0
The idea is for the above to overlay a 'background' coloured contour plot, on a 2d x-y plane for multiple values of a constant C. C is the constant that I have spanned from -3 to 3, and doesn't have anything to do with the first graph.
My current graph looks like this:
where the black line, I believe, is the contour plot, but the colours are screwed up :(
SUCCESS!!
Since you already Accepted my Answer (thank you), I will give you my best effort.
Too late last night to think about it, but morning insight inspired a solution:
ff = @(C,x,y) C+1.0./(x.^2.*y).^(1.0./1.0e1).*1.0e1-2.0./x+y.^2.*(1.0./2.0)-1.0e1;
c = -3:0.5:3;
x = linspace(1E-8, 6);
[X,Y] = meshgrid(x);
for k1 = 1:length(c)-1
Zc = ff(c(k1), X, Y);
hc = contour(X, Y, Zc, [0; 0]);
hz{k1} = find(hc(1,:) == 0);
h{k1} = hc;
end
z = @(x,y) (x.^2 .* y).^((1-1.1)/2);
Z = z(X,Y);
figure(1)
contourf(X, Y, Z, [0 1])
colormap([1 0 0; 0 1 0])
hold on
for k1 = 1:length(c)-1
plot(h{k1}(1, hz{k1}(1)+1:hz{k1}(2)-1), h{k1}(2, hz{k1}(1)+1:hz{k1}(2)-1), 'k')
plot(h{k1}(1, hz{k1}(2)+1:end), h{k1}(2, hz{k1}(2)+1:end), 'k')
end
hold off
axis([0 6 0 6])
The code takes advantage of the fact that contour will produce the coordinates of the lines it plots if you ask it to. (That’s the purpose of the ‘h{k1}’ cell array. The lines ‘break’, so to prevent wrap-around, it is necessary to plot the two parts separately. That explains the two lines in the plot loop.) Then all that’s necessary is to use the normal line plot function plot to plot the lines. I don’t know how robust this is, but it works here.
The plot:
This is so brilliant. You've know idea how much you've help. Thanks!
Star Strider
Star Strider 2015-1-8
编辑:Star Strider 2015-1-8
My pleasure!
I guessed you may be under some time restriction, so I e-mailed you. I apologise for the delay, but it took some experimenting to figure out how to do this.
I very much appreciate your compliment! It was quite an interesting learning experience for me, so thank you for asking your Questions.
And one more thing(!). My objective is actually to plot a number of different graphs, with each graph representing the value of some parameter and the parameter's effect on the graph (if that makes sense). The parameter in this case (usually denoted as gamma) is set equal to 1.1, i.e. every value of 1.1 in both equations -- the contour and the multiple line plots -- represents this gamma. And being a relative matlab novice, and not quite understanding your first equation definition, I was wondering how I could make the simple adjustment of changing gamma (this 1.1) into something else (in my case, for e.g., say 1.6, which would be one of a number of values it would assume)?
I'm sorry if this is a stupid question, but you've been a wonderful help it would seem silly not to ask.
I would define gamma manually in the program, put gamma as a specific variable in the equations, and then run the entire program to produce the plot. I would define gamma before the equations that use it, since the equation will pick up the value of gamma from the workspace. You can use the sprintf function to update the title of the plot automatically to reflect the changing value of gamma.
For instance:
gamma = 1.1;
z = @(x,y) (x.^2 .* y).^((1-gamma)/2);
ff = @(C,x,y) (y^2)/2 + (((x^2)*y)^(1-gamma))/(gamma-1) - 2/x - 1/(gamma-1) + C;
figure(1)
. . .
title(sprintf('The Plot With \\gamma = %3.1f', gamma))
That would print the lower-case gamma symbol, \gamma. The upper-case symbol would be \Gamma. (The double ‘\\’ in the sprintf function is necessary to print the single ‘\’ that the TeX interpreter uses to print the appropriate symbol.)
Cool. Cheers. This was the final code I used:
gamma = 5/3;
ff = @(C,x,y) (y.^2)./2 + (((x.^2).*y).^(1-gamma))./(gamma-1) - 2./x - 1/(gamma-1) + C;
c = -3:0.5:3;
x = linspace(1E-8, 6);
[X,Y] = meshgrid(x);
for k1 = 1:length(c)-1
Zc = ff(c(k1), X, Y);
hc = contour(X, Y, Zc, [0; 0]);
hz{k1} = find(hc(1,:) == 0);
h{k1} = hc;
end
z = @(x,y) (x.^2 .* y).^((1-gamma)./2);
Z = z(X,Y);
figure(1)
contourf(X, Y, Z, [0 1])
colormap([1 0 0; 0 1 0])
hold on
for k1 = 1:length(c)-1
plot(h{k1}(1, hz{k1}(1)+1:hz{k1}(2)-1), h{k1}(2, hz{k1}(1)+1:hz{k1}(2)-1), 'k')
plot(h{k1}(1, hz{k1}(2)+1:end), h{k1}(2, hz{k1}(2)+1:end), 'k')
end
hold off
axis([0 6 0 6])
Knowing what the solution is now actually meant to look like (via some reputable-text-print-out material) I realise there's a couple of problems with my code:
1. The contour line and colour shading doesn't actually ever appear to change shape, even with a new value for gamma?
2. The overlaid line graph solution seems to disintegrate beyond gamma = 1.5 -- but I guess this is more my problem
:(
They may not be problems.
1. I don’t know what you’re calculating, but considering that gamma doesn’t really change much, the contour might not change much. You can actually check to see if they’re different with different values of gamma by asking for an output argument in the contourf call, similar to what I did in the contour call. You can then just save and plot them for different values of gamma to see if there’s a change.
2. When I ran my code with the latest value of gamma, it seems that looking at ‘h’ and ‘hz’, there do not appear to be solutions for all values of ‘C’.
Changing only the loop to this version seems to work robustly for all ‘C’ and ‘gamma’:
for k1 = 1:length(c)
Zc = ff(c(k1), X, Y);
hc = contour(X, Y, Zc, [0; 0]);
cz = find(hc(1,:) == 0);
szhz = numel(cz);
if (szhz == 1)
hc = [hc hc];
hz{k1} = find(hc(1,:) == 0);
h{k1} = hc;
elseif (szhz > 1)
hz{k1} = find(hc(1,:) == 0);
h{k1} = hc;
elseif (szhz == 0)
continue
end
end
I won’t go into detail about it, other than to say it now incorporates logic to trap non-existent contours and not include them, and contours with only one contour are simply duplicated (horizontally concatenated with themselves) to make them compatible with the others.
The plotting loop then changes its beginning line only:
for k1 = 1:size(h,2)
plot(h{k1}(1, hz{k1}(1)+1:hz{k1}(2)-1), h{k1}(2, hz{k1}(1)+1:hz{k1}(2)-1), 'k')
plot(h{k1}(1, hz{k1}(2)+1:end), h{k1}(2, hz{k1}(2)+1:end), 'k')
end
That should be reasonably robust. It’s not extremely efficient, but considering it’s likely a one-off for each ‘gamma’ value, it probably doesn’t have to be.
I have one question: What process are you plotting here? I don’t recognise it and I’m just curious.

请先登录,再进行评论。

更多回答(0 个)

类别

产品

Community Treasure Hunt

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

Start Hunting!

Translated by