colormap doesn't work with bar plots in 2019a

6 ビュー (過去 30 日間)
Clay Fulcher
Clay Fulcher 2020 年 3 月 27 日
編集済み: Clay Fulcher 2025 年 4 月 30 日
I have been changing the colormap in bar charts for decades. It doesn't seem to work now with bar charts in 2019a.
Previously all I had to do to change the colormap in an existing bar chart to copper: colormap copper
Now it does nothing.

採用された回答

Cris LaPierre
Cris LaPierre 2020 年 3 月 27 日
Not sure if it's changed, but here's an example borrowed from the doc on bar.
y = [1 3 5; 3 2 7; 3 4 2];
b = bar(y,'FaceColor',"flat");
for k = 1:size(y,2)
b(k).CData = k;
end
If I want to use a different colormap, I could do this.
colormap copper
y = [1 3 5; 3 2 7; 3 4 2];
b = bar(y,'FaceColor',"flat");
for k = 1:size(y,2)
b(k).CData = k;
end
  3 件のコメント
KV
KV 2024 年 2 月 9 日
I discovered this issue too today, working through some legacy code! This solution breaks a lot of older code that was used to produce plots.
Kind of silly and quite a pity now that it'll take a for loop with each bar command to change facecolors....
Clay Fulcher
Clay Fulcher 2025 年 4 月 30 日
編集済み: Clay Fulcher 2025 年 4 月 30 日
Cris and KV,
This issue became so annoying that I finally wrote a function to quickly change the colormap of a bar graph. Here it is.
function barcolormap(axesobject,cmap)
%
% This function changes the colormap used with bar graphs.
% USAGE
% barcolormap(axesobject,cmap)
%
% INPUT
% axesobject - the axes object from an existing plot or subplot. Get this
% from the existing axes with axesobject=gca;
% cmap - string containing the chosen colormap. Pre-defined colormaps are:
% parula
% turbo
% hsv
% hot
% cool
% spring
% summer
% autumn
% winter
% gray
% bone
% copper
% pink
% sky (since R2023a)
% abyss (since R2023b)
% jet
% lines
% colorcube
% prism
% flag
% white
%
% EXAMPLE
% bar(rand(6,3));
% axesobject=gca;
% cmap='copper';
% barcolormap(axesobject,cmap); % Change colormap to copper
% barcolormap(axesobject,'winter'); % Change colormap to winter
%
% USAGE
% barcolormap(axesobject,cmap)
%
h=axesobject;
set(h,'colormap',eval(cmap))
hbar=get(h,'children');
Lbar=length(hbar);
for i=1:Lbar
set(hbar(i),'FaceColor','flat');
hbar(i).CData=i;
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeColor and Styling についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by