Colormap 'gray' does not work as it should in Matlab r2018

1 visualizzazione (ultimi 30 giorni)
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 Commenti
F. Nucera
F. Nucera il 22 Lug 2019
Dear David,
thanks for answering. Dataplot in my code is not a function. It is just a vector of data. Hence, it should not be this the problem. I think that I am missing some new feature in colormap that it is not available in previous Matlab edition. This would explain why the code works with previous versions of matlab but still I don't understando I should fix the code to get the bars in gray.
Walter Roberson
Walter Roberson il 22 Lug 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.

Accedi per commentare.

Risposta accettata

Star Strider
Star Strider il 22 Lug 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 Commenti
Star Strider
Star Strider il 22 Lug 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
Star Strider il 22 Lug 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!

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by