Colormap 'gray' does not work as it should in Matlab r2018
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
F. Nucera
el 22 de Jul. de 2019
Comentada: Star Strider
el 22 de Jul. de 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 comentarios
Walter Roberson
el 22 de Jul. de 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.
Respuesta aceptada
Star Strider
el 22 de Jul. de 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 comentarios
Star Strider
el 22 de Jul. de 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
el 22 de Jul. de 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!
Más respuestas (0)
Ver también
Categorías
Más información sobre Red en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!