Create a legend according to the colors of a scatter plot

조회 수: 181 (최근 30일)
RAFFAELE MAGLIONE
RAFFAELE MAGLIONE 2022년 5월 19일
댓글: Walter Roberson 2022년 5월 23일
Hi, I have the gps coordinates (xCG and yCG) of the trajectories in a circuit of different drivers, what I would like to do is represent these colored trajectories based on the gear engaged which is saved in an NGear vector. This I did with a scatter plot like below:
scatter(xCG,yCG,10,NGear,'filled');
Now I want to create a legend like this:
legend('Gear 1','Gear 2','Gear 3','Gear 4','Gear 5','Neutral');
But clearly I would like the color in the legend to match the color in the plot. The problem is that NGear does not always contain all five gears, because for example the 5th gear is not put in one lap, this then changes from driver to driver.
So, my question is, there is a way to fix 6 colors accorting to that labels in the legend and than reproduce the same colors in the plot according to the gear engaged?
Thanks.
  댓글 수: 5
Walter Roberson
Walter Roberson 2022년 5월 22일
Do you have an array that contains the "master" color for the 6 different gears? And are you ensuring that NGear is either color information appropriate for the gear actually used, or else is gear index 1 to 6, with missing gears just not present ?
RAFFAELE MAGLIONE
RAFFAELE MAGLIONE 2022년 5월 22일
I solved it by creating a 'Color' vector which for each row contains a triple RGB based on the gear engaged, and then plots 6 NaN, one for each gear, with the color chosen for that gear, to make the legend.

댓글을 달려면 로그인하십시오.

답변 (1개)

the cyclist
the cyclist 2022년 5월 22일
Unless I have misunderstood your question, you should be able to do this using the gscatter command. The legend will be tied to the group (i.e. the gear), which also drives the color.
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 5월 23일
This sounds like a good approach for the case where the user NGear variable is a gear index, rather than being a color array directly.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by