2D Filled Colour Plot

20 views (last 30 days)
J
J on 7 Jan 2015
Commented: Star Strider on 9 Jan 2015
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!

Accepted Answer

Star Strider
Star Strider on 7 Jan 2015
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 Comments
J
J on 9 Jan 2015
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
:(
Star Strider
Star Strider on 9 Jan 2015
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!