필터 지우기
필터 지우기

Colormap by variable using plot function

조회 수: 46 (최근 30일)
nclsg
nclsg 2022년 7월 7일
댓글: nclsg 2022년 7월 13일
Hello!
I am plotting data from a three-column table: x-value, y-value and cycleNumber. I would like to make a plot of y-value vs. x-value, but assign a different color from a colormap to each group of rows with the same cycle number.
I found how to do this using the scatter function:
s1 = scatter(table,'x-value','y-value','filled','ColorVariable','cycleNumber');
but I would prefer an output that the typical plot function gives. Is it possible to assign a 'ColorVariable' to the plot function?
Here is what my data look like right now. Not bad, but I wish the points were connected.
  댓글 수: 3
nclsg
nclsg 2022년 7월 7일
편집: nclsg 2022년 7월 7일
The only way I see would be to create new tables from the original table by filtering through the cycle number and then plot, e.g., 5 separate objects each with a colormap index.
The scatter function lets me plot this whole thing with one line of code. I've been looking for the equivalent of that 'ColorVariable' command within the plot function lit, but I can't find anything.
Rik
Rik 2022년 7월 7일
That is indeed the way I would go. Remember that higher level functions provide easier tools, at the cost of flexibility.
But there may be another way: Mathworks engineers have probably done the exact same thing I'm suggesting you do. So they probably call plot (or the line primitive) somewhere in their code. This means you can query the Children properties of the axes (or the object returned by the scatter function) and you will probably eventually find line objects. When you do, you can edit the LineStyle properties to match what you need.
Not quite 1 line of code, but once you find the correct call, you should be left with 1 or 2 lines.

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

채택된 답변

Chunru
Chunru 2022년 7월 8일
% generate data
x = rand(100,1);
c = randi([1, 5], [100, 1]);
y = exp(x).*c;
tbl = table(x, y, c);
scatter(tbl, 'x', 'y', 'ColorVariable', 'c');
cmap = jet(5); % 5 colors
figure; hold on
c1 = unique(tbl.c);
for i=1:length(c1)
idx = tbl.c == c1(i);
x1 = tbl.x(idx);
y1 = tbl.y(idx);
% sort x1
[x1, isort] = sort(x1);
y1 = y1(isort);
plot(x1, y1, 'Color', cmap(c1(i), :));
end
colormap(cmap)
colorbar
  댓글 수: 1
nclsg
nclsg 2022년 7월 13일
This worked!
(For my data, there was no need to sort x1). Thank you so much!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by