# How to correctly use contourf with logarithmic color scale?

267 ビュー (過去 30 日間)
Sebastian 2023 年 9 月 5 日
コメント済み: Sebastian 2023 年 9 月 6 日
Dear Matlab Community,
I have a 1372 x 4118 (double) matrix I want to plot using contourf() function. The data entries of the matrix vary from 1 to 1e-9.In order to see changes throughout the whole scale I want to use a log scale fo caxis. The following code sets the Colorbar to log scale but the color representation in the plot is not in agreement with the colorbar: ( Due to memory constraints I reduce the matrix to 600 x 600 )
matrix = matrix.a;
x= linspace(1,600,600);
y=linspace(-1,-600,600);
contourf(x,y,matrix);
set(gca,'ColorScale','log');
c = colorbar;
c.Label.String = 'energy [keV]';
xlim([0 100]);
ylim([-200,0]);
clim([1e-7,1e0]);
Please find attached an image of the wrong plot with the full matrix, a subset of the matrix , and the matrix itself.
Best Regards
sethi
##### 2 件のコメントなしを表示なしを非表示
Walter Roberson 2023 年 9 月 5 日
I do not see any problem. With your sample data, the left edge (near x == 0) has a sharp peak to 1, but most of the rest of the data is around roughly 5e-4
Sebastian 2023 年 9 月 5 日
Dear Mr. Roberson,
thank you for the fast reply. I would expect a more pronounced color gradient than this shown in the attached image (contourf_logscale_2.jpg). In this image, values of 1e-5 have the same greenish color as values of 1e-2. But according to the scale bar there should be a bluish or orange color, respectively
.

サインインしてコメントする。

### 採用された回答

Voss 2023 年 9 月 5 日
The problem is that contourf picks the levels in a linear fashion, so the levels in your contour plot are [0 0.1 0.2 0.3 ... 0.9 1], which obviously don't work very well when the color scale is logarithmic.
One way around this is to use use the log of your data in contourf and then adjust the colorbar tick labels appropriately:
matrix = matrix.a;
x = linspace(1,600,600);
y = linspace(-1,-600,600);
figure
contourf(x,y,log10(matrix));
c = colorbar;
c.Label.String = 'energy [keV]';
c.Ticks = -7:0;
c.TickLabels = compose('10^{%d}',c.Ticks);
xlim([0,100]);
ylim([-200,0]);
clim([-7,0]);
Another way is to specify the contour levels yourself:
figure
n_levels = 8;
contourf(x,y,matrix,logspace(-7,0,n_levels));
set(gca,'ColorScale','log');
c = colorbar;
c.Label.String = 'energy [keV]';
xlim([0 100]);
ylim([-200,0]);
clim([1e-7,1e0]);
Surface plot (no contours), for reference:
figure
p = pcolor(x,y,matrix);
p.EdgeColor = 'none';
set(gca,'ColorScale','log');
c = colorbar;
c.Label.String = 'energy [keV]';
xlim([0,100]);
ylim([-200,0]);
clim([1e-7,1e0]);
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Sebastian 2023 年 9 月 6 日
Hello, thank you so much this is exactly what i wanted! This issue is finally solved :)

サインインしてコメントする。

### その他の回答 (1 件)

Nathan Hardenberg 2023 年 9 月 5 日
There are two ways I know of:
1. Define your own (manual) values on which contours should be drawn, or
2. just plot the logarithmic values instead of the linear ones
matrix = matrix.a;
x = linspace( 1, 600, 600);
y = linspace(-1,-600, 600);
figure(1);
% using contourf with manual lines
contourf(x, y, matrix, [1 0.1 0.01 0.001 0.0001 0.00001 0]);
set(gca,'ColorScale','log');
c = colorbar;
c.Label.String = 'energy [keV]';
xlim([0 100]); ylim([-200,0]);
figure(2);
contourf(x, y, log10(matrix)); % draw logarithmic values
c = colorbar;
c.Label.String = 'log10(energy) [keV]';
xlim([0 100]); ylim([-200,0]);
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Sebastian 2023 年 9 月 6 日
Thank you for your support!

サインインしてコメントする。

### カテゴリ

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

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by