Colormap 'gray' does not work as it should in Matlab r2018
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
F. Nucera
am 22 Jul. 2019
Kommentiert: Star Strider
am 22 Jul. 2019
Hi, I am currently running matlab r2018a in windows and I am facing A BIG problem with the function colormap 'gray'. It does not plot in gray the bar that I produce with the code below when 'nPC0==2'. Note that the code works perfectly in previous versions of matlab. Could you please help in fixing the code or prodiving me with some ideas about why this does not work anymore? I have already tried with colormap(gca,gray) but it does not work either
Thank you. F.
figure('Name', 'Prt R2')
for j = 1:size(LabPrtTmp,1);
iprt = icnt+1;
icnt = icnt + NumPrtTmp(j);
iprt = iprt:icnt;
subplot(nR, nC, j)
if nPC0 == 1
ytmp = dataplot(iprt, nPC0:nPC); % select matrix for bar plot
ytmp(:, 2:end) = ytmp(:, 2:end)-ytmp(:, 1:end-1); % from cum R2 to R2s (as stacked cumulates)
elseif nPC0==2
ytmp = dataplot(iprt, nPC0:nPC)- repmat(dataplot(iprt, 1), 1, nPC-1); % select matrix for bar plot (PC1 excluded)
ytmp(:, 2:end) = ytmp(:, 2:end)-ytmp(:, 1:end-1); % from cum R2 to R2s (as stacked cumulates)
colormap('gray')
end
nprt = length(ytmp);
bar(ytmp,'stacked')
xlim([xtmp(1)-.5 xtmp(end)+.5])
if (nC*nR-j)<nC;
xlab = xlabel('Portfolios');set(xlab,'FontSize',wlab)
end
strprt=strcat('P',num2str([1:size(dataplot, 1)]'));
set(gca,'XTick',1:nprt,'XTickLabel',strprt);
if mod(j-1,nC)==0
ylab = ylabel('R2 (in %)');set(ylab,'FontSize',wlab)
end
tit = title(LabPrtTmp(j, :));
set(tit,'FontSize',wtit,'FontWeight','normal')
grid on
axis tight
end % for j = 1:size(LabPrtTmp,1);
strleg=strcat('PC',num2str([nPC0:nPC]'));
legend(strleg,'Orientation','horizontal');
3 Kommentare
Walter Roberson
am 22 Jul. 2019
When you do not have hold on then when you call colormap before calling a plotting function, the axes might get reset, losing the effect of the colormap call.
But you also run into issues of whether colormap applies to the entire figure or only to the axes.
Akzeptierte Antwort
Star Strider
am 22 Jul. 2019
I am not certain what result you want.
Try this:
m = rand(2,5); % Create Data
cm = colormap(gray(5)) % Matched-Size ‘colormap’
figure
hb = bar(m,'stacked'); % Plot Bars
for k = 1:5
hb(k).FaceColor = cm(k,:) % Color Bars
end
Apparently, if you want something other than the default colormap, you need to colour the bars yourself.
Experiment to get the result you want.
2 Kommentare
Star Strider
am 22 Jul. 2019
F. Nucera’s Answer moved here:
Thanks a lot Stardriver. This is exactly the result that I wanted. The only thing is that it was much easier to do with previous Matlab version. Colormap 'gray' was doing the job very nicely. Now it seems much more complicated to plot bars with a colormap different from default.
F.
Star Strider
am 22 Jul. 2019
My pleasure.
I am not certain what ‘previous MATLAB version’ you are referring to. Much changed beginning with R2014b.
If my Answer helped you solve your problem, please Accept it!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Red finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!