How to re-scale the colorbar without affecting the graph?
조회 수: 112 (최근 30일)
이전 댓글 표시
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
채택된 답변
Scott MacKenzie
2021년 5월 19일
편집: Scott MacKenzie
2021년 5월 19일
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
Scott MacKenzie
2021년 5월 19일
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.
추가 답변 (1개)
Sergii Snegir
2021년 5월 19일
편집: Sergii Snegir
2021년 5월 21일
댓글 수: 3
Scott MacKenzie
2021년 5월 21일
편집: Scott MacKenzie
2021년 5월 21일
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
2021년 5월 21일
편집: Adam Danz
2021년 5월 21일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Colormaps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!