Colorbar - Ticklabel with variables

11 Ansichten (letzte 30 Tage)
Mepe
Mepe am 4 Jun. 2020
Kommentiert: Adam Danz am 4 Jun. 2020
For a contourf plot I would like to adapt the labeling on the colorbar. I want to see the value '0', the min and max values there. These are available as variables (min,max) and should also be shown in the power representation.
c = colorbar(axes2);
Any idea?
  8 Kommentare
Constantino Carlos Reyes-Aldasoro
Two things:
1) Notice that YTick, just Tick the one you need.
c = colorbar(axes2);
c.YTick = [0 P_min P_max];
2) Similarly you need TickLabel, not YTickLabel
c = colorbar(axes2);
c.YTick = [0 P_min P_max];
c.YTickLabel = {'0', P_min, P_max};
I assume that P_min and P_max are the values you want to show, that is fine for Tick but for the names it can be anything, not those values, you can convert the numbers with num2str or you could write
c.TickLabel = {'0', '100','1000'};
Adam Danz
Adam Danz am 4 Jun. 2020
Since the colorscale is logarithmic, setting a tick value of 0 will have no effect becaues log10(0) = -inf.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 4 Jun. 2020
If the colorscale in the axes is logarithmic, you can't set the lower axis limit of the colorbar to 0. You could try, and it wouldn't produce an error, but it would produce a warning and the lower limit still would not be zero. The log of 0 is -inf and a colorbar must be finite.
You could create a tick value at the very bottom of the colorbar and you could change its tick label to 0 but it's inaccurate. The actual lower tick value may be very small (ie, 10^-9) but it's not zero and depending on your colorbar range, that could be misleading.
It's better just to let the lower tick value be some small positive number that actually represents the lower limit of the colorbar.
My recommendation is to merely add the lower tick value using,
cb = colorbar(axes1); % Where axes1 is the handle to your axes.
% cb.Limits = [lower, upper]; % Set the colorbar limits if needed
cb.Ticks = unique([cb.Limits(1), P_min, P_max]);
The result would be a colorbar with tick labels that aren't in exponent format.
If you absolutely must use a tick label of 0 at the very bottom of the colorbar, use this line below. However, this could result in a very misleading representation. If you use this, set the cb.Limits(1) value to some very small number.
cb.TickLabels{1} = '0';
Note that your colorbar will still be in log scale!
  4 Kommentare
Mepe
Mepe am 4 Jun. 2020
Many thanks :-).
Adam Danz
Adam Danz am 4 Jun. 2020
Glad I could help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Colormaps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by