Show a discrete color bar in a contourf plot with vector defined contours

26 views (last 30 days)
Hello, I have a difficulties with the colorbar of a contourf plot. I want to have it a maximum of five colors. Sometimes the matrix does not use all of those colors, because the contours depend on avg. and sd. The contourf does not give me problems, but the colorbar does. If I just activate the colorbar, it shows a gradient that I don't want, like in the first image. I changed the colors interactively.
I have a vector v that defines the contours, with values [avg-3sd avg-2sd avg+2sd avg+3sd]. I want everything between avg-2sd and avg+2sd to be white, and the other possible ranges to be filled with solid colors, so I just wrote:
contourf(matrixtoplot,v,'edgecolor','black');
For previous plots, setting the colormap to the corresponding discrete value, and playing with caxis, gave me good results, but it seems that I was lucky, because I can't manage to reproduce that result with other matrices, given that the values of the color boundaries are not evenly distributed. I've tried a lot of options with caxis, unsuccessfully. The following is an example of what I want, with the relevant piece of code:
contourf(othermatrix,otherv,'edgecolor','black');
cb1 = colorbar(ax1);
cb1=colormap(jet(4))
caxis([0 0.2])
ax1=gca;
My strategy was to plot with "full" jet colormap, and the constrain it to the number N of colors used (jet(N)) and adjusting the limits of the color bar. Again, this strategy is not working for every case.

Answers (1)

Cris LaPierre
Cris LaPierre on 17 Dec 2018
I don't have any cases to test. Could you attach a dataset where it is not working? I slightly modified your code, and it works for the test data.
Z = peaks;
contourf(Z)
colormap(jet(4))
colorbar

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!