Describe a colormap in the legend of a plot

37 ビュー (過去 30 日間)
Markus Reichel
Markus Reichel 2022 年 6 月 24 日
コメント済み: Voss 2022 年 6 月 24 日
I am plotting a spacecraft trajectory and use a colormap to visualize the acceleration Vector of the spacecraft in the first part of the trajectory.
The code looks like follow:
%setting color map for Acceleration Vector
Acc_min = floor(min(ab(ab>0))*10000000);
Acc_max = ceil(max(ab(ab>0))*10000000);
Acc_min = min(Acc_min,[],'all');
Acc_max = max(Acc_max,[],'all');
Delta_Acc = Acc_max - Acc_min;
Acc_vals = ceil(Delta_Acc);
cmap = colormap(jet(Acc_vals)); % colormap should be 'parula' or 'jet'
colval = round(abs(nor)*10000000-Acc_min); % number of different temperature color values
for i=1:nval
if colval(i) < 1
colval(i) = 1;
elseif colval(i) > Acc_vals
colval(i) = Acc_vals;
end
end
% plot accelearation vector
for i=1:2:nval-1
Acc_col = cmap(colval(i),:);
quiver3(pos(i,1),pos(i,2),pos(i,3),arrowlength*Acc(i,1),arrowlength*Acc(i,2),arrowlength*Acc(i,3),'Color', Acc_col)
end
Now I would like to describe the color map in the legend of the of the plot. I imagine a bar with the same color range and underneath, the actual acceleration values, for example 0.2 m/s^2 on the red end and 0.05 m/s^2 on the blue end. This is just an idea and any other recommendations how to properly label this are welcome!
Any help would be much appreciated!
  4 件のコメント
Markus Reichel
Markus Reichel 2022 年 6 月 24 日
Don´t the Ticks just set the position for the TicksLabels? I want a TicksLabel on the red end, the middle where currently the value is displayed, and on the blue end. Not only in the middle like its´s currently displayed.
Also I found the problem with the labeling for the color bar.
colbar1.Label.String = 'Acceleration'; colbar1.Label.FontSize = 20; colbar1.Label.VerticalAlignment = 'top';
Now the label is on the right side of the bar. I tried using VerticalAlignment = 'bottom' but it will still display the label on the right hand side. Is there a way to show it on the left side of the color bar?
Markus Reichel
Markus Reichel 2022 年 6 月 24 日
You were right. the Ticks values were not within the caxis.
With these values it´s working but I don´t know why:
colbar1.Ticks = linspace(-0.0061, 0.0061, 3) ;
Do you know how I can shift the label of the colorbar from the right to the left?
And thank you very much for your help!

サインインしてコメントする。

採用された回答

Voss
Voss 2022 年 6 月 24 日
values = 1000*rand(10);
surface(1:10,1:10,values,'FaceColor','interp')
box on
grid on
view(3);
min_max = [min(values(:)) max(values(:))];
cmap = get(gca(),'Colormap');
n_colors = size(cmap,1);
legend_ax = axes(gcf(), ...
'Units','normalized', ...
'Position',[0.69 0.89 0.3 0.1], ...
'Box','on', ...
'Layer','top', ...
'XTick',[], ...
'YTick',[], ...
'XLim',[0 1], ...
'YLim',[0 2], ...
'YDir','reverse');
surface(legend_ax, ...
linspace(0,1,n_colors+1), ...
[0 1], ...
[0:n_colors; 0:n_colors], ...
'EdgeColor','none');
t = [ ...
text(legend_ax,[0 1],[1 1],sprintfc('%.2f',min_max)); ...
text(legend_ax,0.5,1,'m/s^2') ...
];
set(t(1),'HorizontalAlignment','left');
set(t(2),'HorizontalAlignment','right');
set(t(3),'HorizontalAlignment','center');
set(t,'VerticalAlignment','top');
  2 件のコメント
Markus Reichel
Markus Reichel 2022 年 6 月 24 日
Amazing! Thank you!!
Voss
Voss 2022 年 6 月 24 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
John D'Errico 2022 年 6 月 24 日
Hard to put it into a legend, since legend is not designed to do that. Far easier (one line of code) to just put a title above the plot. Have it say something like "red = x (m/s^2) , blue = y (m/s^2)", where x and y are numbers of your choosing. Why make extra work for yourself when there is no gain?
  1 件のコメント
Markus Reichel
Markus Reichel 2022 年 6 月 24 日
I would like to visualize this information for better interpretation. I now use colorbar() but have some problems with the labeling.
But thanks for the input!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeColorbar についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by