Changing colour of plot every time X data changes direction
0 Comments
Accepted Answer
4 Comments
More Answers (1)
Hi @Jason,
You asked, “I have a set of data (50k lines) representing a raster scan so there are several occurrances of the position zero (=X in my data). When I plot this, the whole plot is the same colour. I want to be able to change the colour once the x Pos changes direction (So everytime it reaches a zero, but there are sometimes 2 zeros)”.
After observing comments exchanged between you and @Star Star Strider, to achieve the desired effect of changing the plot color each time the x-position changes direction (especially at zero), I will focus on the relevant columns for x and y positions since data provided by you is large. Then, identify the points where the x-position changes direction which can be done by checking for zeros and determining the direction of change. Once the segments of data between direction changes is identified, you can plot each segment with a different color. Finally, plot the segments using the specified colors. Here is the complete code that implements the above steps:
% Sample Data (Replace this with your actual data) data = [ 0 1159.681; 30 1159.690; 60 1159.700; 90 1159.709; 0 1160.000; % Direction change 30 1160.986; 60 1160.993; 90 1160.999; 0 1161.663; % Direction change 30 1161.638; 60 1161.614; 90 1161.590; ];
% Extract x and y columns xcol = 1; ycol = 2; X = data(:, xcol); Y = data(:, ycol);
% Define colors for plotting newcolors = [ 0.47 0.25 0.80; % Color 1 0.83 0.14 0.14; % Color 2 0.25 0.80 0.54; % Color 3 1.00 0.54 0.00; % Color 4 0.3960 0.5090 0.9920; % Color 5 1 0 1; % Color 6 0.2270 0.7840 0.1920; % Color 7 1.0000 0.2700 0.2270; % Color 8 ];
% Initialize figure figure; hold on;
% Find indices where x changes direction (i.e., where X is zero) zeroIndices = find(X == 0); segments = [1; zeroIndices; length(X) + 1]; % Include start and end
% Loop through segments and plot each with a different color for i = 1:length(segments) - 1 startIdx = segments(i); endIdx = segments(i + 1) - 1;
if startIdx <= endIdx % Select color based on segment index colorIdx = mod(i - 1, size(newcolors, 1)) + 1; % Cycle through colors plot(X(startIdx:endIdx), Y(startIdx:endIdx), 'LineWidth', 2, 'Color', newcolors(colorIdx, :)); end end
% Finalize plot grid on; xlabel('X Position'); ylabel('Y Position'); title('Raster Scan Data with Direction Change Colors'); hold off;
Please see attached.
As you can see in the script above,sample data is defined in a matrix format. You should replace this with your actual data input method. Afterwards, set of colors is defined in an array. You can modify or expand this array as needed. The code identifies the indices where the x-position is zero, which indicates a change in direction, loops through each segment of data between the identified indices and plots them using different colors. The mod function is used to cycle through the defined colors. For more information on mod function, please refer to
https://www.mathworks.com/help/matlab/ref/double.mod.html
Finally, plot is finalized with grid lines, axis labels, and a title for clarity.
This approach will allow you to visualize your raster scan data, with clear indications of direction changes through color differentiation. You can further customize the colors and styles as per your requirements.
Hope this helps.
If you have any additional questions or need further modifications, feel free to ask!
0 Comments
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!