How to re-scale the colorbar without affecting the graph?

41 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Scott MacKenzie
Scott MacKenzie am 19 Mai 2021
Bearbeitet: Scott MacKenzie am 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 Kommentare
Scott MacKenzie
Scott MacKenzie am 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.
Sergii Snegir
Sergii Snegir am 19 Mai 2021
Bearbeitet: Sergii Snegir am 19 Mai 2021
Dear Scott,
you got correctly my idea saying "....but with greater proportions for colors on the bottom."
Short clarificatio. My graph presents set of data. The higest density of this data is colored in read and localised on top of the graph! This part of the data is not interesting compare to the one in the center of the graph.
The interesting set of data, which has to be colored very carefully is colored in blue. Obwiously I would like to stretch this blue color gradient in the color bar and schow how this color scheme related to the data (bin Counts)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sergii Snegir
Sergii Snegir am 19 Mai 2021
Bearbeitet: Sergii Snegir am 21 Mai 2021
Dear Scott,
i think, I have found the answer why your proposition h.Limits = [0 .5] did not work for h=binscatter().
  1. In Matlab 2017 h.Limits shifts only the values, but not the colors positions together, like you showed in the example above.
  2. When I used Matlab 2021a - h.Limits works...but:
  3. When using a binscatter() function the limits are real values.With few lines of code the real values can be transformed either in arbitrary units, or %
Below is the final result:
  3 Kommentare
Scott MacKenzie
Scott MacKenzie am 21 Mai 2021
Bearbeitet: Scott MacKenzie am 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
Adam Danz am 21 Mai 2021
Bearbeitet: Adam Danz am 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Colormaps finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by