- 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.
- Transform the Z-values to their logarithmic counterparts before setting the color data. This way, the color interpolation aligns with the log-transformed data.
Colors of the bar are not same for the same height in 3d bar plot after applying log scale
조회 수: 5 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
Shubham
2024년 8월 19일
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:
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
Shubham
2024년 8월 19일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!