How to re-scale the colorbar without affecting the graph?
40 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sergii Snegir
le 19 Mai 2021
Modifié(e) : Adam Danz
le 21 Mai 2021
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
0 commentaires
Réponse acceptée
Scott MacKenzie
le 19 Mai 2021
Modifié(e) : Scott MacKenzie
le 19 Mai 2021
Just set the Limits property of the colorbar. Here's an example for bar charts ...
tiledlayout('flow');
nexttile;
x = 1:5;
y = rand(5,5);
bar(x,y);
colorbar;
nexttile;
x = 1:5;
y = rand(5,5);
bar(x,y);
h = colorbar;
h.Limits = [0 .5];
3 commentaires
Scott MacKenzie
le 19 Mai 2021
Hmm, in that case, I don't understand your goal. In the bottom colorbar in my example, the color that was in the middle of the bar is repositioned to the top of the bar. And the colors that were on the top half of the bar are hidden in the bottom bar. Are you looking for something akin to a log scale where all the colors still appear, but with greater proportions for colors on the bottom? I'm only asking as a matter for clarification -- not sure how to do that. Good luck.
Plus de réponses (1)
Sergii Snegir
le 19 Mai 2021
Modifié(e) : Sergii Snegir
le 21 Mai 2021
3 commentaires
Scott MacKenzie
le 21 Mai 2021
Modifié(e) : Scott MacKenzie
le 21 Mai 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.
Adam Danz
le 21 Mai 2021
Modifié(e) : Adam Danz
le 21 Mai 2021
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
Voir également
Catégories
En savoir plus sur Colormaps dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!