How do I manually set color of individual datapoints of figure ?

8 ビュー (過去 30 日間)
Jad
Jad 2024 年 9 月 17 日
回答済み: Star Strider 2024 年 9 月 18 日
I have a 5x302 matrix which describes 5 curves. With each column of the matrix containing the 5 values of each curve for the same x -axis-value. I am currently plotting this in the following way :
scatter([1:1:302], E(:,:), 'b.');
I now would like for each of these five curves to be a different color. I believe the easy answer would be something like
for m = [1:1:5]
plot([1:1:302], sqrt(abs(real(E(m,:)))), '.');
hold on
end
the problem is that, for each column of my matrix, the 5 values appear in a random order. This is a quirk of the program that produces these values and is, as far as I know, unpredictable. This makes the 5 curves change color randomly :
I don't want to go through the trouble of doing some sort of data-analysis / fitting to figure out which data-point belongs to which curve. I don't mind coloring the points myslef since I only need to produce a few of these figures. I tried using the brush to paint points one color but I can't manage to get multiple brush colors. How can I manually paint these points ?

回答 (3 件)

Star Strider
Star Strider 2024 年 9 月 17 日
編集済み: Star Strider 2024 年 9 月 17 日
One option is to specify a colormap and then index into it —
cm = turbo(5);
Fs = 1000;
t = linspace(0, 301, 302).'/Fs;
E = sin(2*pi*t*(1:5:25));
figure
hold on
for k = 1:size(E,2)
plot(t, E(:,k),'Color',cm(k,:), 'DisplayName',["Row "+k])
end
hold off
legend('Location','best')
.
  3 件のコメント
Star Strider
Star Strider 2024 年 9 月 17 日
I don’t have that data, and I‘m not certain how to simulate it.
It would be necessary to determine the discontinuities (that I would guess are NaN values since you’re using plot), and then separate out each segment individually. You could do this using the isnan funciton (and then take its logical negative, so ~isnan(x)) and probably use find to determine where the individual segments began and ended. Then, it would be a matter of isolating and plotting each segment individually, along with its corresponding independent variable value, whether this was a specific vector or a vector of indices into the dependent variable vector.
With no data to work with, that is the best I can do.
Umar
Umar 2024 年 9 月 17 日

Hi @Jad,

To address the issue of color-coding individual curves in a shuffled matrix, you can manually specify the indices of the points that belong to each curve. This approach allows you to plot each curve with a distinct color, even when the data is not organized in a conventional manner. I took liberty to refine @Strider’s code, Hope he does not mind to help you out.

cm = turbo(5); % Define a colormap with 5 colors
Fs = 1000;
t = linspace(0, 301, 302).'/Fs;
% Example shuffled data
E = [sin(2*pi*t*1), sin(2*pi*t*5), sin(2*pi*t*10), sin(2*pi*t*15), sin(2*pi*t*20)];
figure
hold on
% Manually specify the indices for each curve
curveIndices = {1:100, 101:200, 201:302}; % Adjust these ranges based on your   
data
for k = 1:length(curveIndices)
  plot(t(curveIndices{k}), E(curveIndices{k}, k), 'Color', cm(k,:), 'DisplayName', 
 ["Curve " + k]);
end
hold off
legend('Location', 'best')

Please see attached.

In this refined code, curveIndices is an array of ranges that specify which points belong to each curve. Adjust these indices according to your specific data structure. This method provides clarity and control over the visualization of your curves. Hope this should help resolve your problem.

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


Umar
Umar 2024 年 9 月 17 日

Hi @Jad,

You asked, “I don't want to go through the trouble of doing some sort of data-analysis / fitting to figure out which data-point belongs to which curve. I don't mind coloring the points myslef since I only need to produce a few of these figures. I tried using the brush to paint points one color but I can't manage to get multiple brush colors. How can I manually paint these points ?”

Please see my response to your comments below.

Your original approach involves plotting the matrix using a scatter plot, but due to the random order of values, you are experiencing issues with color consistency. Also, you want to manually assign colors to each curve without resorting to data fitting or complex analysis. Here's an updated MATLAB code snippet that accomplishes this:

% Sample matrix E (5x302)
E = rand(5, 302);  % Replace this with your actual data
% Define colors for each curve
colors = {'r', 'g', 'b', 'm', 'c'}; % Red, Green, Blue, Magenta, Cyan
% Create a figure
figure;
hold on;
% Loop through each curve
for m = 1:5
  % Extract the current curve's values and plot them
  % Here we use a scatter plot with specific colors
  scatter(1:302, sqrt(abs(real(E(m,:)))), 36, colors{m}, 'filled'); 
end
% Add labels and title for clarity
xlabel('X-axis');
ylabel('Y-axis');
title('Colored Curves');
legend({'Curve 1', 'Curve 2', 'Curve 3', 'Curve 4', 'Curve 5'}, 'Location', 'Best');
hold off;

So, in the above code, I have initialized E as a random matrix for demonstration purposes. Replace it with your actual data. Also, a cell array named colors is created to hold the desired colors for each curve. You can expand or modify the colors array if you wish to use different color schemes. A figure is created and hold on allows multiple plots on the same figure. The loop iterates over each curve index m from 1 to 5 and the scatter function is used to plot each curve's values with its corresponding color. The third parameter (36) specifies the marker size. Adjust the marker size (currently set at 36) based on your visual preferences or data density. Using sqrt(abs(real(E(m,:)))) makes sure that you're applying your transformation consistently across all curves. Make sure that your matrix E contains valid numerical data before plotting.

Please see attached.

Please let me know if you have any further questions.


Star Strider
Star Strider 2024 年 9 月 18 日
Stiill lacking your data, I am guessing at the problem and proposing a solution.
Try this —
x = (0:14).'; % Postulated Independent Variable Vector
y = [5 4 3 2 1 NaN 8 8 8 NaN 1,2,3,4,5].'; % Postulated Dependent Variable Vector
figure
plot(x, y, 'LineWidth',3)
ylim('padded')
title('Original')
Lv = [false; ~isnan([NaN; y; NaN]); false].'; % Logical Vector Defining Non-NaN Values
first = strfind(Lv,[0 1])-1;
last = strfind(Lv, [1 0])-2;
segments = [first(:) last(:)]
segments = 3×2
1 5 7 9 11 15
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
hold on
cm = [1 0 0; 0 1 0; 0 0 1]; % Colormap Defining Line Colours (Can Be Anything That Works)
for k = 1:size(segments,1)
idxrng = segments(k,1) : segments(k,2);
plot(x(idxrng), y(idxrng), 'Color',cm(k,:), 'LineWidth',3)
end
hold off
ylim('padded')
title('Result')
Make appropriate changes to get the result you want.
.

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by