rename colorbar Ticks in a script that creates varaible number of ticks

2 ビュー (過去 30 日間)
jan mischke
jan mischke 2015 年 11 月 3 日
回答済み: Star Strider 2015 年 11 月 3 日
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

採用された回答

Star Strider
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 件)

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by