2D Filled Colour Plot
37 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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!
0 commentaires
Réponse acceptée
Star Strider
le 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 commentaires
Star Strider
le 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Contour Plots dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!