In a contour plot, manipulate colors of contours
Show older comments
In a contour plot, how can I set for example all negative values red, all pooitive values blue, zero line black but a bit thicker? In addtion I would like to use different increments, like between the values 0 and 50 do increment of 5 and everything higher do increment of 10?
Answers (2)
That's a few questions in one, but I'll try to answer them all even if some are less satisfactory than others.
The colour of lines is taken from the colourmap so you can manipulate this e.g. using the example in the help:
[X,Y,Z] = peaks;
figure; hAxes = gca; [~,h] = contour(hAxes, X,Y,Z,20);
cmap = [1 0 0; 0 0 1];
colormap( hAxes, cmap )
caxis( [-1 1] ) % Or [-6 6] - it's all the same with this colourmap
You can also change the colourmap to have 0 as black also, but it is more fiddly to avoid having the contours closest to 0 also being black - e.g. you need a colourmap with 100 red values, 1 black, 100 blue.
I'm not aware that you can change the line thickness individually.
Likewise the increments are set when you call 'contour', but you could split your data in two and create two contour plots on the same axes for each half of your data with different settings.
2 Comments
Michael Marcinkowski
on 23 Mar 2017
Adam
on 23 Mar 2017
Well, Z is what determines the contour height so you can split your matrices based on this. e.g. again using the example in the documentation:
[X,Y,Z] = peaks;
Z1 = Z;
Z1( Z >= 0 ) = 0;
Z2 = Z;
Z2( Z < 0 ) = 0;
figure; hAxes = gca;
contour( hAxes, X, Y, Z1, 50 )
hold( hAxes, 'on' )
contour( hAxes, X, Y, Z1, 10 )
This allows you to supply different numbers of contours for positive and negative data.
Michael Marcinkowski
on 30 Mar 2017
1 Comment
Adam
on 30 Mar 2017
The first one is what I answered originally. The zero contour line can be included with either negative or positive (in my answer I included it with positive. Or you could just add it as a 3rd contour set I guess. I'm not sure what you want increments of, but you can manipulate the absolute number of contour lines based on your own calculated increment. Contour lines with different increments seems to just be what I answered as the final comment to my answer above.
Categories
Find more on Purple in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!