Clear Filters
Clear Filters

Setting color to certain data in 3d plot, keeping rest of data following colormap

6 views (last 30 days)
I am plotting data in a 3d heatmap using the bar3 command. I'd like to set some of the data to a certain color that is not included in the colorscheme of the legend bar, while keeping the rest of the data following the color map. The code I'm currently using is:
b=bar3(error);
set(gca,'XTickLabel',[2.5 5 7.5 10])
set(gca,'YTickLabel',[0 50 100 150 200 250])
ylim([0 20.5]);
colorbar
colormap turbo;
caxis([0 100]);
for k = 1:length(b)
zdata = b(k).ZData;
b(k).CData = zdata;
b(k).FaceColor = 'interp';
end

Answers (1)

Jaswanth
Jaswanth on 29 Apr 2024
Hi Heather,
To achieve the effect of changing the color of some data in a 3D bar plot (bar3) in MATLAB, while keeping the default colormap for the rest of the bars, you can manipulate the properties of the individual bars using FaceColor property. You can set FaceColor to a specific color (using an RGB triplet) to bypass the colormap for that bar.
Kindly go through the following example that changes the color of the first bar to red, while the remaining bars use the default colormap (turbo in this case).
% Generate example data
error = rand(10, 10) * 100;
% Create a 3D bar plot
b = bar3(error);
colorbar
colormap turbo; % Set the default colormap for the plot
% Customize the appearance
for k = 1:length(b)
zdata = b(k).ZData;
b(k).CData = zdata; % Assign ZData to CData for color mapping
b(k).FaceColor = 'interp'; % Use interpolated coloring
if k == 1 % Identify the first bar to change its color
% Use a specific color for the first bar (e.g., red)
b(k).FaceColor = [1, 0, 0]; % Directly set the FaceColor to red
end
end
I hope that the information provided above is helpful in accomplishing your task.

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!