rename colorbar Ticks in a script that creates varaible number of ticks
조회 수: 2 (최근 30일)
이전 댓글 표시
I am plotting a contourplot with contourf. If I use the log10(Z) in the contourplot, then my Colormap will be in log sclae too (For example my values in Z are from 1e17 to 1e20, so if I get the log10 I will get values from 17 to 20 and so the colorbar will have values from 17 to 20 as well). What I want though is to have the colorbar with different labels (1E17 1E18 1E19 ...). I know that you can change the labels manually. My problem is that I wrote a script and that the number of ticks depends on my Matrix (Z). So there could be just 2 ticks from 1E14 to 1E15 and there could be like 6 ticks from 1E16 to 1E21. So the renaming of the ticks would have to be variable with number of ticks. I was starting some if cases but I thought that you maybe have a different idea. P.S. I need to plot the values in log10 scale
Thank you very much
댓글 수: 0
채택된 답변
Star Strider
2015년 11월 3일
This is the only robust way I can devise to do what you want. In order to make the colorbar tick labels to look nice, it’s necessary to restrict the contour levels. If you want more contour levels, experiment with the code to get the result you want.
Z = 10.^randi([17 20], 7, 7); % Create Data
extr = [min(floor(Z(:))) max(ceil(Z(:)))]; % Calculate Extremes
clabelvals = 10.^[log10(extr(1)):log10(extr(2))]; % Set Contour Label Levels
contourlvls = 10.^[log10(extr(1)):0.5:log10(extr(2))]; % Set Contour Levels
figure(1)
hcp = contour(log10(Z), log10(contourlvls)); % Contour Plot With Contour Levels
grid on
hcb = colorbar;
cbtk = get(hcb, 'Ticks'); % Get Tick Values
cbtkv = clabelvals; % New Tick Labels To Set
cbtklbls = regexp(sprintf('%4.0E ', cbtkv), ' ', 'split'); % Cell Array Of New Tick Labels
set(hcb, 'Ticks', log10(cbtkv), 'TickLabels', cbtklbls(1:end-1)) % Set New Tick Labels
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Colormaps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!