Hi there!
Let's say I want to plot this:
contourf(peaks,16)
caxis([-2 2])
colorbar
Now I get:
But I want to show black contour lines only in the caxis limits AND also the color levels in the same limits, i.e. 16 color levels in [-2 2], not just 6 like in the picture.
Any ideas? Thanks

 採用された回答

Cris LaPierre
Cris LaPierre 2019 年 4 月 8 日
編集済み: Cris LaPierre 2023 年 5 月 17 日

1 投票

The problem is your data goes from [-6.5 8]. The 16 contour lines have been drawn for that. Then when you call caxis, you get rid of any color distinction above 2 or below -2 (>2 all have same color, <-2 all have same color). This does not affect where the actual contour lines go, though. Just the range of values used to assign colors.
Instead, specify (in increasing order) the values you want the contour lines at.
Compare the output of these 3 plots
p = peaks;
figure
contourf(p,16)
colorbar
figure
contourf(p,16)
caxis([-2 2])
colorbar
figure
contourf(p,linspace(-2,2,16))
colorbar
I think the last one does what you want.

10 件のコメント

Cris LaPierre
Cris LaPierre 2019 年 4 月 8 日
Getting the white spaces was a little interesting to me. If you don't want that, you could do something like this:
figure
p(p<-2)=-2;
contourf(p,linspace(-2,2,16))
colorbar
Tomas Levy
Tomas Levy 2019 年 4 月 8 日
Brilliant, that's it. Thank you very much.
Benjamin Kraus
Benjamin Kraus 2019 年 4 月 8 日
Another solution is to append -inf to your list of levels, so that everything below the (now) second value is colored:
figure
contourf(peaks,[-inf linspace(-2,2,16)])
caxis([-2 2])
There is a blog post called On The Edge that gives some more details about how contourf determines what areas to fill.
Cris LaPierre
Cris LaPierre 2019 年 4 月 8 日
This is a better approach, since modifying your data just to make it plot as you want will likely have unintended (negative) consequences downstream.
You could use any value less than the overall min of your data.This works for me.
figure
contourf(peaks,[-1000 linspace(-2,2,16)])
caxis([-2 2])
Thibaut Gerson
Thibaut Gerson 2020 年 3 月 20 日
Hello,
I do not understand why, in this case (see code bellow), values above 2 are not "blank" as for the values bellow -2?
Thanks
figure
contourf(p,linspace(-2,2,16))
colorbar
Cris LaPierre
Cris LaPierre 2020 年 3 月 20 日
This is explained in the documentation for contourf:
  • The contourf function uses the current colormap to fill the spaces between the levels in the plot. The first color fills the space between the lowest level and the level above it. The last color corresponds to Z-values that are greater than the highest level in the plot. If Z contains values that are smaller than the lowest level displayed in the plot, the region between the lowest level and the smallest Z-value is white.
Thibaut Gerson
Thibaut Gerson 2020 年 3 月 20 日
Thanks & understood.
Now, is there a way to force "white" also for values which are above the highest level? Based on the function definition, it seems impossible..
Cris LaPierre
Cris LaPierre 2020 年 3 月 20 日
The simplest way is to use a colormap that has white corresponding to the max values.
p = peaks;
figure
contourf(p,[linspace(-2,2,16)])
colormap hot
colorbar
Megha Suswaram
Megha Suswaram 2021 年 2 月 19 日
Thank you! That worked super well for me!
Nabin
Nabin 2023 年 5 月 17 日
Thank you! Worked well for me too!

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

その他の回答 (1 件)

Vahidreza
Vahidreza 2025 年 4 月 1 日
編集済み: Vahidreza 2025 年 4 月 1 日

0 投票

A nice automatic way you can set a similar contour increments and colorbar for two or more figures is:
num_levels = 32;
figure
tiledlayout(1,2, TileSpacing="compact")
nexttile
contourf(x, y, z, num_levels)
colorbar
col_range = clim;
nexttile
contourf(x, y, z, linspace(col_range(1), col_range(2), num_levels))
colorbar

カテゴリ

ヘルプ センター および File ExchangeContour Plots についてさらに検索

製品

リリース

R2017b

タグ

質問済み:

2019 年 4 月 7 日

編集済み:

2025 年 4 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by