How to get color bar for 2-D plot?

조회 수: 3 (최근 30일)
Saneesh Ali
Saneesh Ali 2019년 1월 14일
답변: Ayush 2024년 8월 20일
I am ploting a number of ellipse in a single plot.First i created the ellipse, then I rotate the ellipse through an angle theta by multiplying with a rotation matrix, then i plot it. I want to give a color bar according to the orientation of the ellipse. Any help will be highly appreciated.
Thank you
for ut=1:length(PMAX1)
a=PMAX1(ut,1);
b=PMIN1(ut,1);
uh=.15;
xc=5;
yc=frr(ut,1);
alpha=the1(ut,1);
m = 1000;
x = zeros(m,1);
y = zeros(m,1);
theta = linspace(0,2*pi,m);
for k = 1:m
x(k) = .2 * cos(theta(k));
y(k) = uh * sin(theta(k));
end
R = [cos(alpha) -sin(alpha); ...
sin(alpha) cos(alpha)];
rCoords = R*[x' ; y']; % we use the [] to create a 2xm array
xr = rCoords(1,:)'; % extract the rotated x coordinates
yr = rCoords(2,:)'; % extract the rotated y coordinates
plot(xr+xc,yr+yc,'b')
hold on
end

답변 (1개)

Ayush
Ayush 2024년 8월 20일
Hey Saneesh,
I understand that you are trying to plot multiple ellipses on a single plot and want to add a color bar that reflects the orientation of each ellipse. This involves using the orientation angle to determine the color of each ellipse, and then displaying a color bar to represent these angles visually.
To achieve this, you can:
  1. Normalize the orientation angles to map them to a colormap.
  2. Use MATLAB's "colormap" to assign colors to each ellipse based on its orientation.
  3. Add a color bar to the plot to indicate the mapping from angles to colors.
% Example data
PMAX1 = rand(10, 1) * 5; % Replace with your data
PMIN1 = rand(10, 1) * 3; % Replace with your data
frr = rand(10, 1) * 10; % Replace with your data
the1 = linspace(0, 2*pi, 10)'; % Replace with your data
% Prepare the figure
figure;
hold on;
colormap(jet); % Use the jet colormap for color representation
% Get the range of angles for normalization
min_alpha = min(the1);
max_alpha = max(the1);
for ut = 1:length(PMAX1)
a = PMAX1(ut, 1);
b = PMIN1(ut, 1);
uh = .15;
xc = 5;
yc = frr(ut, 1);
alpha = the1(ut, 1);
m = 1000;
x = zeros(m, 1);
y = zeros(m, 1);
theta = linspace(0, 2*pi, m);
for k = 1:m
x(k) = .2 * cos(theta(k));
y(k) = uh * sin(theta(k));
end
R = [cos(alpha) -sin(alpha); sin(alpha) cos(alpha)];
rCoords = R * [x'; y'];
xr = rCoords(1, :)';
yr = rCoords(2, :)';
% Normalize alpha to a value between 0 and 1
normalized_alpha = (alpha - min_alpha) / (max_alpha - min_alpha);
% Map the normalized alpha to a color
color = colormap(jet);
colorIndex = round(normalized_alpha * (size(color, 1) - 1)) + 1;
ellipseColor = color(colorIndex, :);
% Plot the ellipse with the assigned color
plot(xr + xc, yr + yc, 'Color', ellipseColor, 'LineWidth', 1.5);
end
% Add a color bar
colorbar;
caxis([min_alpha max_alpha]); % Set the limits of the color bar to match the angle range
xlabel('X-axis');
ylabel('Y-axis');
title('Ellipses with Orientation-Based Color');
hold off;
Explanation:
  • Colormap: The "jet" colormap is used here, but you can choose any colormap you prefer.
  • Normalization: The angle is normalized between 0 and 1 to map it to the colormap index.
  • Color Mapping: For each ellipse, a color is selected based on its orientation.
  • Color Bar: A color bar is added to the plot to indicate the orientation corresponding to the colors.
To learn more about "colormap" and "colorbar" , refer to the following MathWorks documentation:
  1. colormap : https://www.mathworks.com/help/matlab/ref/colormap.html
  2. colorbar : https://www.mathworks.com/help/matlab/ref/colorbar.html
Hope this helps!
Regards

카테고리

Help CenterFile Exchange에서 Polar Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by