How to re-scale the colorbar without affecting the graph?
Mostrar comentarios más antiguos
Hallo,
I have a question about how to modify the colorbar. AS you see on the image the data on the graph is colored mainly in blue, dark blue, white. But on the color bar these colors are on the bottom. How to "compress"/hide/etc the color distribution from red to green on the colorbar and show better how blue gradient develops. I dont want that changes in the colorbar affect the graph itself.
I use binscatter() function to plot the data.
Thanks

Respuesta aceptada
Más respuestas (1)
Sergii Snegir
el 19 de Mayo de 2021
Editada: Sergii Snegir
el 21 de Mayo de 2021
0 votos
3 comentarios
We've got a small but important mix-up here.
Binscatter uses the axes' colomap to color code density. The bar plots in Scott's answer use the axes' ColorOrder property which is completely different from the colomap property. The colorbar only defines the colormap and has nothing to do with ColorOrder. That's why the range of colors in Scott's colorbars don't match the range of colors in the bar plots.
His colorbar extends from 0 to 1 because there is no color map data on his example plots and the default range is zero and one in that case. There is no normalization going on in that example.
Scott MacKenzie
el 21 de Mayo de 2021
Editada: Scott MacKenzie
el 21 de Mayo de 2021
OK, Adam, thanks for this clarification. So, if I understand correctly, it makes no sense to use colorbar with bar because the colors in each are independent. I suppose using surf for my example would have been better since the colors in the surf chart are the colors in the colorbar:

To hightlight the colors in the bottom half, the Limits for the right-hand chart were set to [-2 0].
What about using the ColorScale property of the axis? I think this gets closer to what Sergii is after. The chart below on the left is from the documentaion for binscatter:

The chart on the right adds one line of code:
set(gca, 'ColorScale', 'log');
I think this achieves one of Sergii's goal (more information on changes in data at the low end), but not the other (leaving the colors in the scatter plot as is). Perhaps I'm just off on a misguided tangent here, but I thought this might be of some use.
Well, you could use a colorbar with a bar plot if you set the colormap equal to the ColorOrder. For example,
x = randi(5,5,4);
ax = gca;
bar(x,'stacked')
ax.ColorOrder = lines(4);
ax.Colormap = lines(4);
cb = colorbar;
set(cb,'Limits', [0,1], 'Ticks',.125:.25:1,'TickLabels',["A" "B" "C" "D"])
Setting the colorbar's Limits only changes what part of the color spectrum appears on the colorbar as you demonstrated with the surf plot. It's the same as setting an axis limit using xlim|ylim.
Setting the ColorScale affects the colormap and colorbar but that only offers linear|log options.
Lastly, caxis() sets the colormap limits which also affects the colorbar. This is the same as setting the CLim axis property
x = peaks(50);
figure
tiledlayout(1,2)
nexttile
surf(x,'EdgeColor', 'none')
colormap jet
colorbar('Ticks',-6:2:6)
axis square
nexttile
surf(x,'EdgeColor', 'none')
colormap jet
colorbar()
caxis([-2,2])
axis square
Categorías
Más información sobre Color and Styling en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




