フィルターのクリア

How can one use a variable to set color of a line plot , and use colorbar?

29 ビュー (過去 30 日間)
Levente Gellért
Levente Gellért 2024 年 5 月 27 日
コメント済み: Levente Gellért 2024 年 5 月 28 日
Dear MAtLab community,
I am creating a simple line plot, and I would like to use a third variable to color the line at every point .
the line comes as:
line(koordmean(:,1),koordmean(:,2))
xlim([1 640])
ylim([1 480])
set(gca,'ydir','reverse')
and the line should be colouread according to the variable 'anx'. It should work similar to the imagesc function combined with the colorbar function.
Any suggestions are highly appreciated! Kind regards
lg

採用された回答

DGM
DGM 2024 年 5 月 27 日
編集済み: DGM 2024 年 5 月 27 日
Here's a simpler way to do it using patch().
% the data as given
load koordmean
load anx
% the coordinate data
x = koordmean(:,1);
y = koordmean(:,2);
% the color scaling data
c = anx;
n = numel(anx);
CT = parula(n);
% pad the vectors to keep the patch from being filled
x(end+1) = NaN;
y(end+1) = NaN;
c(end+1) = NaN;
% create a patch
hp = patch(x,y,c,'EdgeColor','interp');
hp.LineWidth = 2; % if you want fatter lines
colormap(CT)
colorbar
% if you want to set certain axes properties
xlim([1 640])
ylim([1 480])
set(gca,'ydir','reverse')
Doing it this way only creates one high level graphics object, instead of dozens. That means less lag and there's only one object that you need to edit if you want to change properties after the fact.
  2 件のコメント
Levente Gellért
Levente Gellért 2024 年 5 月 28 日
That's very nice, thank you DGM

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2024 年 5 月 27 日
If you'd like the line to have a different color at every point along it, you'll have to plot markers with those colors.
markerSize = 10;
numPoints = 500;
cMap = jet(numPoints); % Make colormap
x = 1:numPoints;
y = x; % Sample data.
for k = 1 : numPoints
plot(x(k), y(k), '.', 'Color', cMap(k, :), 'MarkerSize', markerSize);
hold on;
end
grid on;
hold off;
Adjust colormap and markersize as desired.

Austin M. Weber
Austin M. Weber 2024 年 5 月 27 日
To color the line according the values in the variable anx:
% Your data
load koordmean
load anx
x = koordmean(:,1);
y = koordmean(:,2);
% Normalize the data in anx for color indexing
n = length(x);
anx_normalized = round(rescale(anx, 1, n));
cmap = parula(n);
figure
clim([min(anx) max(anx)])
hold on;
for i = 1 : n-1
% Get color from colormap
color = cmap(anx_normalized(i), :);
% Plot the line
plot([x(i), x(i+1)], [y(i), y(i+1)], 'Color', color, 'LineWidth', 2);
end
hold off
xlim([1 640])
ylim([1 480])
set(gca,'ydir','reverse')
cb=colorbar;
cb.Label.String='anx';
xlabel('koordmean(:,1)')
ylabel('koordmean(:,2)')

カテゴリ

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

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by