Colors of the bar are not same for the same height in 3d bar plot after applying log scale

5 visualizaciones (últimos 30 días)
I'm trying to plot 3D graph with bars, in which I'm using colormap. I have found a work around to apply colormap on bar3:
b = [...] % my data
for k = 1:length(b)
zdata = b(k).ZData;
b(k).CData = zdata;
b(k).FaceColor = 'interp';
end
colormap('jet')
I'm also applying log scale in Z axis. But it was messing up my plot and I found a work around for this also.
% Z log fix
llim = .1;
h = get(gca,'Children');
for i = 1:length(h)
ZData = get(h(i), 'ZData');
ZData(ZData==0) = llim;
set(h(i), 'ZData', ZData);
end
But I'm getting the following result after the log fix where the bars don't have same color at the same Z value (height).
I'm trying to get results like the following plot.
Anyone know the solution?

Respuesta aceptada

Shubham
Shubham el 19 de Ag. de 2024
Hi Kazi,
To achieve consistent coloring of bars at the same height in a 3D bar plot with a logarithmic scale on the Z-axis, you need to ensure that the color mapping corresponds correctly to the logarithmic values. Here's how you can address this issue:
  1. When you apply a logarithmic transformation, the color mapping needs to reflect the transformed values. This ensures that bars at the same height have the same color.
  2. Transform the Z-values to their logarithmic counterparts before setting the color data. This way, the color interpolation aligns with the log-transformed data.
Here's a revised version of your code with these adjustments:
% Your data
b = bar3(yourData); % Replace 'yourData' with your actual data matrix
% Apply colormap with correct mapping
colormap('jet');
clim([min(yourData(:)) max(yourData(:))]); % Set color axis limits
% Log scale transformation
llim = 0.1; % Lower limit for log transformation
for k = 1:length(b)
% Get ZData and apply log transformation
zdata = b(k).ZData;
zdata(zdata == 0) = llim; % Avoid log(0) by setting a lower limit
logZData = log10(zdata); % Apply log transformation
% Set CData for color mapping
b(k).CData = logZData;
b(k).FaceColor = 'interp';
end
% Set the Z-axis to log scale
set(gca, 'ZScale', 'log');
% Adjust the Z-axis limits and ticks if necessary
zlim([llim, max(yourData(:))]);
zticks = logspace(log10(llim), log10(max(yourData(:))), 5); % Adjust number of ticks as needed
set(gca, 'ZTick', zticks);
This approach should ensure consistent coloring across bars at the same height, even with a logarithmic scale applied. Adjust the llim and tick settings as needed for your specific data range.
  3 comentarios
Shubham
Shubham el 19 de Ag. de 2024
Hi Kazi,
I've made some changes in the code. Try this code:
% Sample data
[X, Y] = meshgrid(1:10, 1:10);
Z = randi([1, 1000], size(X));
% Create 3D bar plot
figure;
b = bar3(Z);
% Apply colormap to bar3
for k = 1:length(b)
zdata = b(k).ZData;
b(k).CData = zdata;
b(k).FaceColor = 'interp';
end
colormap('jet')
% Apply log scale in Z axis and normalize colors
llim = .1;
h = get(gca, 'Children');
for i = 1:length(h)
ZData = get(h(i), 'ZData');
ZData(ZData == 0) = llim;
set(h(i), 'ZData', ZData);
% Normalize ZData for consistent coloring
ZDataNorm = log10(ZData);
set(h(i), 'CData', ZDataNorm);
end
% Set Z-axis to log scale
set(gca, 'ZScale', 'log')
% Add labels and title
xlabel('X-axis')
ylabel('Y-axis')
zlabel('Counts')
title('3D Bar Plot with Logarithmic Z-axis and Colormap')
In this code snippet, I've used some random data. You need to replace your data to achieve the final plot.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Discrete Data Plots en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by