Contour plot with different levels for negative and positive values
23 views (last 30 days)
Show older comments
Hi everyone,
I need to build a contour plot to display both positive and negative values of contours. Specifically, I want the contour lines with positive values to be colored in red, while those with negative values should be in blue. The desired appearance of the contour plot is depicted in the figure.
To create the contour plot, I am using a certain code. However, in its current form, this code does not allow for the correct visualization of contour lines with negative values, resulting in gaps. I am unable to adjust the number of contour lines for positive and negative values. I would appreciate your help.
load('fdata.mat')
z=plane(:,2); % axis Z
y=plane(:,1); % axis Y
fdata=plane(:,3); % function
[X,Y]=meshgrid(min(z):0.01:max(z),min(y):0.01:max(y)); % grid
Z=griddata(z,y,fdata,X,Y);
h_plot = figure('InvertHardcopy','on','Color',[1 1 1],'Colormap',[0 0 1;0.00892857182770967 0.00892857182770967 1;...
0.0178571436554193 0.0178571436554193 1;0.0267857145518064 0.0267857145518064 1;0.0357142873108387 0.0357142873108387 1;...
0.0446428582072258 0.0446428582072258 1;0.0535714291036129 0.0535714291036129 1;0.0625 0.0625 1;0.0714285746216774 0.0714285746216774 1;...
0.0803571417927742 0.0803571417927742 1;0.0892857164144516 0.0892857164144516 1;0.0982142835855484 0.0982142835855484 1;...
0.107142858207226 0.107142858207226 1;0.116071425378323 0.116071425378323 1;0.125 0.125 1;0.133928567171097 0.133928567171097 1;...
0.142857149243355 0.142857149243355 1;0.151785716414452 0.151785716414452 1;0.160714283585548 0.160714283585548 1;0.169642850756645 0.169642850756645 1;...
0.178571432828903 0.178571432828903 1;0.1875 0.1875 1;0.196428567171097 0.196428567171097 1;0.205357149243355 0.205357149243355 1;0.214285716414452 0.214285716414452 1;...
0.223214283585548 0.223214283585548 1;0.232142850756645 0.232142850756645 1;0.241071432828903 0.241071432828903 1;0.25 0.25 1;0.258928567171097 0.258928567171097 1;...
0.267857134342194 0.267857134342194 1;0.27678570151329 0.27678570151329 1;0.28571429848671 0.28571429848671 1;0.294642865657806 0.294642865657806 1;0.303571432828903 0.303571432828903 1;...
0.3125 0.3125 1;0.321428567171097 0.321428567171097 1;0.330357134342194 0.330357134342194 1;0.33928570151329 0.33928570151329 1;0.34821429848671 0.34821429848671 1;0.357142865657806 0.357142865657806 1;...
0.366071432828903 0.366071432828903 1;0.375 0.375 1;0.383928567171097 0.383928567171097 1;0.392857134342194 0.392857134342194 1;0.40178570151329 0.40178570151329 1;0.41071429848671 0.41071429848671 1;...
0.419642865657806 0.419642865657806 1;0.428571432828903 0.428571432828903 1;0.4375 0.4375 1;0.446428567171097 0.446428567171097 1;0.455357134342194 0.455357134342194 1;0.46428570151329 0.46428570151329 1;...
0.47321429848671 0.47321429848671 1;0.482142865657806 0.482142865657806 1;0.491071432828903 0.491071432828903 1;0.5 0.5 1;0.508928596973419 0.508928596973419 1;0.517857134342194 0.517857134342194 1;...
0.526785731315613 0.526785731315613 1;0.535714268684387 0.535714268684387 1;0.544642865657806 0.544642865657806 1;0.553571403026581 0.553571403026581 1;0.5625 0.5625 1;0.571428596973419 0.571428596973419 1;...
0.580357134342194 0.580357134342194 1;0.589285731315613 0.589285731315613 1;0.598214268684387 0.598214268684387 1;0.607142865657806 0.607142865657806 1;0.616071403026581 0.616071403026581 1;0.625 0.625 1;...
0.633928596973419 0.633928596973419 1;0.642857134342194 0.642857134342194 1;0.651785731315613 0.651785731315613 1;0.660714268684387 0.660714268684387 1;0.669642865657806 0.669642865657806 1;...
0.678571403026581 0.678571403026581 1;0.6875 0.6875 1;0.696428596973419 0.696428596973419 1;0.705357134342194 0.705357134342194 1;0.714285731315613 0.714285731315613 1;0.723214268684387 0.723214268684387 1;...
0.732142865657806 0.732142865657806 1;0.741071403026581 0.741071403026581 1;0.75 0.75 1;0.758928596973419 0.758928596973419 1;0.767857134342194 0.767857134342194 1;0.776785731315613 0.776785731315613 1;...
0.785714268684387 0.785714268684387 1;0.794642865657806 0.794642865657806 1;0.803571403026581 0.803571403026581 1;0.8125 0.8125 1;0.821428596973419 0.821428596973419 1;0.830357134342194 0.830357134342194 1;...
0.839285731315613 0.839285731315613 1;0.848214268684387 0.848214268684387 1;0.857142865657806 0.857142865657806 1;0.866071403026581 0.866071403026581 1;0.875 0.875 1;0.883928596973419 0.883928596973419 1;...
0.892857134342194 0.892857134342194 1;0.901785731315613 0.901785731315613 1;0.910714268684387 0.910714268684387 1;0.919642865657806 0.919642865657806 1;0.928571403026581 0.928571403026581 1;0.9375 0.9375 1;...
0.946428596973419 0.946428596973419 1;0.955357134342194 0.955357134342194 1;0.964285731315613 0.964285731315613 1;0.973214268684387 0.973214268684387 1;0.982142865657806 0.982142865657806 1;...
0.991071403026581 0.991071403026581 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;1 1 1;...
1 1 1;1 0.991452991962433 0.991452991962433;1 0.982905983924866 0.982905983924866;1 0.974358975887299 0.974358975887299;1 0.965811967849731 0.965811967849731;1 0.957264959812164 0.957264959812164;...
1 0.948717951774597 0.948717951774597;1 0.94017094373703 0.94017094373703;1 0.931623935699463 0.931623935699463;1 0.923076927661896 0.923076927661896;1 0.914529919624329 0.914529919624329;...
1 0.905982911586761 0.905982911586761;1 0.897435903549194 0.897435903549194;1 0.888888895511627 0.888888895511627;1 0.88034188747406 0.88034188747406;1 0.871794879436493 0.871794879436493;...
1 0.863247871398926 0.863247871398926;1 0.854700863361359 0.854700863361359;1 0.846153855323792 0.846153855323792;1 0.837606847286224 0.837606847286224;1 0.829059839248657 0.829059839248657;...
1 0.82051283121109 0.82051283121109;1 0.811965823173523 0.811965823173523;1 0.803418815135956 0.803418815135956;1 0.794871807098389 0.794871807098389;1 0.786324799060822 0.786324799060822;...
1 0.777777791023254 0.777777791023254;1 0.769230782985687 0.769230782985687;1 0.76068377494812 0.76068377494812;1 0.752136766910553 0.752136766910553;1 0.743589758872986 0.743589758872986;...
1 0.735042750835419 0.735042750835419;1 0.726495742797852 0.726495742797852;1 0.717948734760284 0.717948734760284;1 0.709401726722717 0.709401726722717;1 0.70085471868515 0.70085471868515;1 0.692307710647583 0.692307710647583;1 0.683760702610016 0.683760702610016;1 0.675213694572449 0.675213694572449;1 0.666666686534882 0.666666686534882;1 0.658119678497314 0.658119678497314;1 0.649572670459747 0.649572670459747;1 0.64102566242218 0.64102566242218;1 0.632478654384613 0.632478654384613;1 0.623931646347046 0.623931646347046;1 0.615384638309479 0.615384638309479;1 0.606837630271912 0.606837630271912;1 0.598290622234344 0.598290622234344;1 0.589743614196777 0.589743614196777;1 0.58119660615921 0.58119660615921;1 0.572649598121643 0.572649598121643;1 0.564102590084076 0.564102590084076;1 0.555555582046509 0.555555582046509;1 0.547008574008942 0.547008574008942;1 0.538461565971375 0.538461565971375;1 0.529914557933807 0.529914557933807;1 0.52136754989624 0.52136754989624;1 0.512820541858673 0.512820541858673;1 0.504273533821106 0.504273533821106;1 0.495726495981216 0.495726495981216;1 0.487179487943649 0.487179487943649;1 0.478632479906082 0.478632479906082;1 0.470085471868515 0.470085471868515;1 0.461538463830948 0.461538463830948;1 0.452991455793381 0.452991455793381;1 0.444444447755814 0.444444447755814;1 0.435897439718246 0.435897439718246;1 0.427350431680679 0.427350431680679;1 0.418803423643112 0.418803423643112;1 0.410256415605545 0.410256415605545;1 0.401709407567978 0.401709407567978;1 0.393162399530411 0.393162399530411;1 0.384615391492844 0.384615391492844;1 0.376068383455276 0.376068383455276;1 0.367521375417709 0.367521375417709;1 0.358974367380142 0.358974367380142;1 0.350427359342575 0.350427359342575;1 0.341880351305008 0.341880351305008;1 0.333333343267441 0.333333343267441;1 0.324786335229874 0.324786335229874;1 0.316239327192307 0.316239327192307;1 0.307692319154739 0.307692319154739;1 0.299145311117172 0.299145311117172;1 0.290598303079605 0.290598303079605;1 0.282051295042038 0.282051295042038;1 0.273504287004471 0.273504287004471;1 0.264957278966904 0.264957278966904;1 0.256410270929337 0.256410270929337;1 0.247863247990608 0.247863247990608;1 0.239316239953041 0.239316239953041;1 0.230769231915474 0.230769231915474;1 0.222222223877907 0.222222223877907;1 0.21367521584034 0.21367521584034;1 0.205128207802773 0.205128207802773;1 0.196581199765205 0.196581199765205;1 0.188034191727638 0.188034191727638;1 0.179487183690071 0.179487183690071;1 0.170940175652504 0.170940175652504;1 0.162393167614937 0.162393167614937;1 0.15384615957737 0.15384615957737;1 0.145299151539803 0.145299151539803;1 0.136752143502235 0.136752143502235;1 0.128205135464668 0.128205135464668;1 0.119658119976521 0.119658119976521;1 0.111111111938953 0.111111111938953;1 0.102564103901386 0.102564103901386;1 0.0940170958638191 0.0940170958638191;1 0.085470087826252 0.085470087826252;1 0.0769230797886848 0.0769230797886848;1 0.0683760717511177 0.0683760717511177;1 0.0598290599882603 0.0598290599882603;1 0.0512820519506931 0.0512820519506931;1 0.042735043913126 0.042735043913126;1 0.0341880358755589 0.0341880358755589;1 0.0256410259753466 0.0256410259753466;1 0.0170940179377794 0.0170940179377794;1 0.00854700896888971 0.00854700896888971;1 0 0],...
'Color',[1 1 1],'units','normalized','outerposition',[0 0 1 1]);
axes1 = axes('Parent',h_plot);
hold on
zmin = floor(min(z(:)));
zmax = ceil(max(z(:)));
zinc = (zmax - zmin) / 100
zlevs = zmin:zinc:zmax
[L,cL]=contour(X,Y,Z,zlevs);
0 Comments
Accepted Answer
Cris LaPierre
on 20 Apr 2024
I'll share my thoughts.
First, in a contour plot, the rows of your matrix correspond to the Y axis, and the columns correspond to X. Therefore, to duplicate the plot you shared, flip y and z.
load fdata.mat
z=plane(:,2); % axis Z
y=plane(:,1); % axis Y
fdata=plane(:,3); % function
% flip y and z in the next 2 lines of code
[X,Y]=meshgrid(min(y):0.01:max(y),min(z):0.01:max(z)); % grid
Z=griddata(y,z,fdata,X,Y);
Next, it does not make sense to me to set your contour line values using anything but your Z value. That is Z here, not z. Perhaps it makes sense for your data. You'd know better than I. However, the range of Z values is so large that I think you want to use logspace to create them. I arrived at these settings through trial and error.
zlevs = logspace(-3,5,26);
Then just call contour twice, once for positive zlevs with solid green lines for the contours, and once with -zlevs with dashed blue contour lines.
contour(X,Y,Z,zlevs,'g-')
hold on
contour(X,Y,Z,-zlevs,'b--')
hold off
More Answers (0)
See Also
Categories
Find more on Contour Plots 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!