Shifting plotted points from green to red

17 vues (au cours des 30 derniers jours)
Jared Dube
Jared Dube le 22 Fév 2023
Hi, I'm not sure how to go about this, but I'm currently trying to create a figure in which x and y position are plotted from a 2177x30 table. I want to make it so the first point is green and the last point is red. I'm trying to shift the points from green to red as they near the end of the plot so you can clearly follow the position as time goes by. For example say your plotting 100 points, then point 1 would be green, point 51 would be yellow, and point 100 would be red. Is there a way to do that? Currently I have this matlab code below as well as attatched my current output:
clc; clear;
data_HAND = readtable ('JARED_HAND.xlsx');
figure
plot(data_HAND.HAND1X,data_HAND.HAND1Y)
hold on
plot(data_HAND.HAND2X,data_HAND.HAND2Y)
hold on
plot(data_HAND.HAND3X,data_HAND.HAND3Y)
hold on
plot(data_HAND.HAND4X,data_HAND.HAND4Y)
hold on
plot(data_HAND.HAND5X,data_HAND.HAND5Y)
hold on
plot(data_HAND.HAND6X,data_HAND.HAND6Y)
hold on
plot(data_HAND.HAND7X,data_HAND.HAND7Y)
hold on
plot(data_HAND.HAND8X,data_HAND.HAND8Y)
hold on
plot(data_HAND.HAND9X,data_HAND.HAND9Y)
hold on
plot(data_HAND.HAND10X,data_HAND.HAND10Y)
hold on
plot(data_HAND.HAND11X,data_HAND.HAND11Y)
hold on
plot(data_HAND.HAND12X,data_HAND.HAND12Y)
hold on
plot(data_HAND.HAND13X,data_HAND.HAND13Y)
hold on
plot(data_HAND.HAND14X,data_HAND.HAND14Y)
hold on
plot(data_HAND.HAND15X,data_HAND.HAND15Y)
title('Hand')
xlabel('x position (mm)')
ylabel('y position (mm)')

Réponse acceptée

William Rose
William Rose le 23 Fév 2023
Modifié(e) : William Rose le 23 Fév 2023
[edit: added 'filled' to fill in the points]
[edit 2: change from red -> green to green -> red, as originally requested by @Jared Dube]
Yes. Here is an example, using data comparable to one cycle of your hand position data.
t=0:100;
x=-500*cos(2*pi*t/100);
y=500*(x/500).^2+t;
%hsv=[linspace(0,.333,length(x))',ones(length(x),2)]; %red -> green
hsv=[linspace(.333,0,length(x))',ones(length(x),2)]; %green -> red
c=hsv2rgb(hsv);
scatter(x,y,[],c,'filled');
Try it. Good luck.
  1 commentaire
William Rose
William Rose le 23 Fév 2023
@Jared Dube, see the help for scatter for many more options, such as plot symbol, size, etc. If you want to go fromm red to dark green, adjust the "v" (value) column in the hsv array. The code above uses all 1's for the saturation and value columns of the hsv array. If you taper the value (basically, brightness) column from 1 at the start to 0.5 at the end, you wil get a darker shade of green at the end, but the red aththe start will still be bright. I also made the points larger, by specifying size=50, versus 36 by default.
t=0:100;
x=-500*cos(2*pi*t/100);
y=500*(x/500).^2+t;
hsv=[linspace(0,.333,length(x))',ones(length(x),1),linspace(1,.5,length(x))'];
c=hsv2rgb(hsv);
scatter(x,y,50,c,'filled');

Connectez-vous pour commenter.

Plus de réponses (1)

William Rose
William Rose le 23 Fév 2023
@Jared Dube, to plot a line with continuously variable color:
t=0:100;
x=-500*cos(2*pi*t/100);
y=500*(x/500).^2+t;
hsv=[linspace(.333,0,length(x))',ones(length(x),1),linspace(.5,1,length(x))'];
cmap=hsv2rgb(hsv); % array of colors
surface([x;x],[y;y],zeros(2,length(t)),[t;t],...
'facecol','no','edgecol','interp','linew',2);
colormap(cmap); % use cmap and t values to color the line
c=colorbar; % display colorbar (optional)
c.Label.String = 'Time (s)'; % add label
xlabel('X (mm)'); ylabel('Y (mm)'); grid on

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by