In a contour plot, manipulate colors of contours

9 vues (au cours des 30 derniers jours)
Michael Marcinkowski
Michael Marcinkowski le 23 Mar 2017
Commenté : Adam le 30 Mar 2017
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?

Réponses (2)

Adam
Adam le 23 Mar 2017
Modifié(e) : Adam le 23 Mar 2017
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 commentaires
Michael Marcinkowski
Michael Marcinkowski le 23 Mar 2017
Thank you for your fast reply. I am completely new to matlab so please forgive me that I don't really understand how to split data with matlab. I have attached the code for plotting the contour plot and the mat file which has to be loaded first.
load('D:\Programme\MATLAB\R2016b\bin\tippch3.mat')
Rad=Angle.*pi/180;
[xx yy ]=pol2cart(Rad,R);
tix=-2:.01:2;
tiy=-2:.01:2;
figure(1)
set(figure(1),'units','normalized','outerposition',[0 0 1 1]);
cla;
[X,Y] = meshgrid(tix,tiy);
Z = griddata(xx,yy,ERS2,X,Y,'v4');
%zmin=floor(min(Z(:)));
%zmax=ceil(max(Z(:)));
zmin=-90;
zmax=500;
zinc=10;
zlev=zmin:zinc:zmax;
colormap(hsv);
[C,h]=contour(X,Y,Z,zlev);
c = colorbar;
c.Label.String = 'Energie [kJ/mol]';
ix=find(Z==min(Z(:)));
line(X(ix),Y(ix),...
'marker','o',...
'markersize',10,...
'markerfacecolor',[1,0,0],...
'color',[1,0,0]);
text(X(ix),Y(ix)-.5,'',...
'horizontalalignment','center');
%viscircles([0 0],2,'Color',[0 .5 .5]);
%circles(0,0,2,'FaceColor','white')
axis equal;
Adam
Adam le 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.

Connectez-vous pour commenter.


Michael Marcinkowski
Michael Marcinkowski le 30 Mar 2017
Hello,
thank you again for your quick reply. It does the job now in the sense that it creates 50 contour lines for values below 0 and 10 contour lines for values greater than 0 right? My questions are now: 1) How can I now give these lines red and blue colors? 2) How can I put in zero value contour line? 3) How can I manipulate not to have an absolute number of contour lines but increments of it (like it was before)? 4) How can I insert contour lines with different increments? (e.g. from 0 to 100 I would like to have contour lines with increment of 10, and for values above I would like to have increments of 50)
load('D:\Programme\MATLAB\R2016b\bin\tippch3.mat')
Rad=Angle.*pi/180; % angle between a plane holding fragment 1 and approaching fragment 2
[xx yy ]=pol2cart(Rad,R); % r is a distance between fragments
tix=-2:.5:2; % allowed x distances
tiy=-2:.5:2; % allowed y distances
% figure(1)
% set(figure(1),'units','normalized','outerposition',[0 0 1 1]);
cla;
[X,Y] = meshgrid(tix,tiy); % here we create a X-Y grid
Z = griddata(xx,yy,ERS2,X,Y,'v4'); % here we interpolate our energies E for x,y positions into grid data
% zmin=floor(min(Z(:))); % this can be used to control which contour lines to plot
% zmax=ceil(max(Z(:)));
zmin=-90;
zmax=500;
zinc=10; % distance between contours
zlev=zmin:zinc:zmax;
%colormap(hsv);
%[C,h]=contour(X,Y,Z,zlev); % and finally plot !
Z1 = Z;
Z1( Z >= 0 ) = 0;
Z2 = Z;
Z2( Z < 0 ) = 0;
figure(1); hAxes = gca;
set(figure(1),'units','normalized','outerposition',[0 0 1 1]);
contour( hAxes, X, Y, Z1, 50 )
hold( hAxes, 'on' )
contour( hAxes, X, Y, Z2, 10 )
c = colorbar;
c.Label.String = 'Energie [kJ/mol]';
%ix=find(Z==min(Z(:)));
%line(X(ix),Y(ix),...
%'marker','o',...
%'markersize',10,...
%'markerfacecolor',[1,0,0],...
%'color',[1,0,0]);
% text(X(ix),Y(ix)-.5,'',...
% 'horizontalalignment','center');
%viscircles([0 0],2,'Color',[0 .5 .5]);
%circles(0,0,2,'FaceColor','white')
axis equal;
%saveas(gcf, 'test.png');
export_fig neu -pdf -eps -png -jpg -tiff -native -q101 -r600 -transparent
Sorry for this probably annoying questions, but I just start to learn how to use matlab.
Best,
Michael
  1 commentaire
Adam
Adam le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Contour Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by