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

1 回表示 (過去 30 日間)
F. Nucera
F. Nucera 2019 年 7 月 22 日
コメント済み: Star Strider 2019 年 7 月 22 日
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 件のコメント
F. Nucera
F. Nucera 2019 年 7 月 22 日
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 2019 年 7 月 22 日
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.

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2019 年 7 月 22 日
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 件のコメント
Star Strider
Star Strider 2019 年 7 月 22 日
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 2019 年 7 月 22 日
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!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRed についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by