Colormap - contourf scaling and color distribution

5 views (last 30 days)
Hi,
I want to plot the matrix M_2f1 with the contourf command (logarithmic scaling). So far this works quite well.
But two points bother me:
1. I would like to specify a defined area in the colorbar (e.g. 0 to 10). Here, logarithmic scaling is certainly a hindrance. Is there another option than choosing a very small, positive value?
2. I don't like the color distribution in the colorbar. I would prefer an even distribution (detached from the logarithmic scaling)
M_2f1=[0.0149766610154867,0.0148727404855169,0.0149659744686933,0.0197882778625291,0.0184313013563117,0.0176794194615834,0.0196108762871584,0.0258284889924015,0.0354233801633032,0.171924195188659,0.0281396699959042,0.0141469884838954,0.0334375924402216,0.0314806724042401,0.172885272977384,0.0883818321056840,0.106217236988698,0.0782668761035103,0.122073669629712,0.0274013525228684,0.0485163365474854;0.0250969531529632,0.0139275587788499,0.0130272875683284,0.0198185099934251,0.0258483008607248,0.0216474128550250,0.0260883732887959,0.0329091753710079,0.0268783125653000,1.91538463836732,0.195449696327352,0.0381036869089853,0.0588836876081499,0.171757614634871,0.0231260787399225,0.170935395044788,0.120483617869003,0.600468182457506,0.166605755383242,0.318424884533997,0.0370178336952820;0.0127170974716373,0.0147177357124663,0.0142949618281963,0.0141321097009308,0.0135950246696883,0.0155180804843015,0.0128687984781713,0.0162524560391073,0.0149971936741940,0.0137489964554028,0.0328922058840716,0.0135826966044079,0.0147869975187494,0.0136696060929059,0.0133113468539191,0.0684702636256040,0.0644115870564511,0.359497603819481,0.0386968843910783,0.0148698983598170,0.0194361839183835]
f = figure(1);
axes1 = axes('Parent',f);
hold(axes1,'on');
[C,h]=contourf(log(M_2f1),15);
h.LineWidth = 0.0001;
box(axes1,'on');
axis(axes1,'tight');
set(axes1,'BoxStyle','full','Layer','top');
set(gca,'colorscale','log')
colorbar(axes1);
c = colorbar;
colormap(parula(15));
c.Label.String = 'Nonlinearity parameter';
set(c, 'ylim', [0 10])
xticks([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21])
xlim([1 21])
xticklabels({'','',5,'',10,'',15,'',20,'',25,'',30,'',35,'',40,'',45,'',50})
yticks([1 2 3])
ylim([1 3])
yticklabels({'0','5','10'})
xlabel('X-direction [mm]')
ylabel('Y-direction [mm]')
Any Idea?

Accepted Answer

Adam Danz
Adam Danz on 30 May 2020
Edited: Adam Danz on 30 May 2020
Here are the changes I suggest you make. Lines that have been commented-out are lines you don't seem to need.
The key changes are setting the x and y values in contourf() instead of setting tick labels and removing the log scale for the colorscale.
I've left additional comments in all caps with further explanation.
M_2f1=[0.0149766610154867,0.0148727404855169,0.0149659744686933,0.0197882778625291,0.0184313013563117,0.0176794194615834,0.0196108762871584,0.0258284889924015,0.0354233801633032,0.171924195188659,0.0281396699959042,0.0141469884838954,0.0334375924402216,0.0314806724042401,0.172885272977384,0.0883818321056840,0.106217236988698,0.0782668761035103,0.122073669629712,0.0274013525228684,0.0485163365474854;0.0250969531529632,0.0139275587788499,0.0130272875683284,0.0198185099934251,0.0258483008607248,0.0216474128550250,0.0260883732887959,0.0329091753710079,0.0268783125653000,1.91538463836732,0.195449696327352,0.0381036869089853,0.0588836876081499,0.171757614634871,0.0231260787399225,0.170935395044788,0.120483617869003,0.600468182457506,0.166605755383242,0.318424884533997,0.0370178336952820;0.0127170974716373,0.0147177357124663,0.0142949618281963,0.0141321097009308,0.0135950246696883,0.0155180804843015,0.0128687984781713,0.0162524560391073,0.0149971936741940,0.0137489964554028,0.0328922058840716,0.0135826966044079,0.0147869975187494,0.0136696060929059,0.0133113468539191,0.0684702636256040,0.0644115870564511,0.359497603819481,0.0386968843910783,0.0148698983598170,0.0194361839183835]
f = figure(1);
axes1 = axes('Parent',f);
hold(axes1,'on');
% [C,h]=contourf(log(M_2f1),15)
[C,h]=contourf(0:2.5:50, 1:3, log(M_2f1),15); % SET THE X AND Y VALUES HERE
h.LineWidth = 0.0001;
box(axes1,'on');
% axis(axes1,'tight'); % THE PLOT SHOULD ALREADY BE 'TIGHT', BUT YOU COULD KEEP THIS IF YOU WANT TO
set(axes1,'BoxStyle','full','Layer','top');
% set(gca,'colorscale','log') % IT'S NOT CLEAR WHY YOU NEED THIS
c = colorbar(axes1); % GET THE COLORBAR HANDLE HERE
% c = colorbar; % REDUNDANT
% colormap(parula(15));
c.Label.String = 'Nonlinearity parameter';
% set(c, 'ylim', [0 10]) % NO NEED FOR THIS
% xticks([1:21]) % NO NEED FOR THIS
% xlim([1 21]) % NO NEED FOR THIS
% xticklabels({'','',5,'',10,'',15,'',20,'',25,'',30,'',35,'',40,'',45,'',50}) % NO NEED FOR THIS
% yticks([1 2 3]) % YOU COULD KEEP THIS ONE IF YOU WANT TO
% ylim([1 3]) % NO NEED FOR THIS
% yticklabels({'0','5','10'}) % NO NEED FOR THIS
xlabel('X-direction [mm]')
ylabel('Y-direction [mm]')
One reason the image seems stretched vertically is because of the scaling difference between the x and y axes. That could be addressed by equating the aspect ratio using axis(axes1, 'equal') but then your axis height will be narrow.
  2 Comments
Mepe
Mepe on 30 May 2020
Thank you for optimizing the code :-).
How could I now set the display area of the colorbar? I want to keep the same for different evaluations.
I would also like to scale the color bar logarithmically. So far, this had been converted using ... log (M_2f1). Is there a more elegant way?
Thanks a lot!!!
Adam Danz
Adam Danz on 30 May 2020
Do you mean something like this?
c.TickLabels = compose('10^{%.2g}',c.YTick);

Sign in to comment.

More Answers (0)

Categories

Find more on Colormaps 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!