If I have y = rand(100,1), can I plot y such that the portions of the line that are greater than or equal to 0.5 is plot in red while portions lying below 0.5 is plot in black?
MATLAB: Is there a way to make a plot of a line with different colors
multicolorplot
Related Solutions
It looks like your data is already sorted, so we can skip the sorting/ordering in your specific case, at least for this data sample, and just draw lines:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 20;x1=[ 1 1 1 1 0.999999912 0.999690873 0.998202938 0.996110802 0.992961257 0.980004982 0.924939672 0.820535897 0.602167816 0.398690056 0.203859669 -0.075955146 -0.304796417 -0.557870458 -0.864153125 -0.99993919 -0.988967944 -0.999529311]y1=[ 0 -2.54E-10 -3.15E-07 -1.62E-05 -0.000419996 -0.02486278 -0.059924071 -0.088109422 -0.118439611 -0.198972951 -0.380113933 -0.571594998 -0.798369539 -0.917085732 -0.97900012 -0.997111235 -0.952417526 -0.82992804 -0.50322895 -0.011027972 0.14812969 0.030678273]x2=[ 2 2 2 2 1.999999912 1.999690873 1.998202929 1.996110712 1.992960745 1.979993679 1.924375819 1.81311973 1.510835006 0.912920593 0.344944452 -0.139948732 -0.785972062 -1.356710455 -1.804704529 -1.980245442 -1.987180774 -1.998004715]y2=[ 0 -2.54E-10 -3.15E-07 -1.62E-05 -0.000419996 -0.024874491 -0.060061709 -0.08853234 -0.119452311 -0.203727586 -0.413690537 -0.693157058 -1.215890722 -1.774737732 -1.968997637 -1.995061545 -1.829041733 -1.431471605 -0.842880325 -0.208511267 0.088370616 0.085876712]x3=[ 3 3 3 3 2.999999912 2.999690873 2.998202929 2.996110712 2.992960745 2.979993678 2.924375609 2.813103613 2.509294444 1.843529705 0.621048704 -0.782583788 -1.680485396 -2.277495171 -2.690548186 -2.858916151 -2.893450016 -2.924109296]y3=[ 0 -2.54E-10 -3.15E-07 -1.62E-05 -0.000419996 -0.024874492 -0.060061779 -0.088532791 -0.119454264 -0.203752975 -0.414339097 -0.698834419 -1.27137721 -2.140752324 -2.93012533 -2.76123397 -2.276082999 -1.821542761 -1.306864177 -0.685939571 -0.334330296 -0.291390175]plot(x1, y1, 'ro', 'LineWidth', 2);hold on;plot(x2, y2, 'ko', 'LineWidth', 2);plot(x3, y3, 'bo', 'LineWidth', 2);% Fit a circle to the innermost
[xc,yc,R,a] = circfit(x1, y1);plot(xc, yc, 'r+', 'MarkerSize', 30, 'LineWidth', 2);grid on;% Draw lines from x1, y1 to xc, yc
for k = 1 : length(x1) hold on; plot([x1(k), xc], [y1(k), yc], 'r-');end% Draw lines from x2, y2 to x1, y1. Same for x3, y3.
for k = 1 : length(x1) plot([x2(k), x1(k)], [y2(k), y1(k)], 'r-', 'LineWidth', 2); plot([x2(k), x3(k)], [y2(k), y3(k)], 'b-', 'LineWidth', 2);endfunction [xc,yc,R,a] = circfit(x,y)%CIRCFIT Fits a circle in x,y plane
%
% [XC, YC, R, A] = CIRCFIT(X,Y)
% Result is center point (yc,xc) and radius R. A is an optional
% output describing the circle's equation:
%% x^2+y^2+a(1)*x+a(2)*y+a(3)=0
% by Bucher izhak 25/oct/1991
n=length(x); xx=x.*x; yy=y.*y; xy=x.*y;A=[sum(x) sum(y) n;sum(xy) sum(yy) sum(y);sum(xx) sum(xy) sum(x)];B=[-sum(xx+yy) ; -sum(xx.*y+yy.*y) ; -sum(xx.*x+xy.*y)];a=A\B;xc = -.5*a(1);yc = -.5*a(2);R = sqrt((a(1)^2+a(2)^2)/4-a(3));end
You are plotting the boundary using:
plot(I(700,:,k), trace_color(k,1));
This means, you have the coordinates i.e (x,y) in hand. The values I(700,:,k) are y values. You are plotting the curves w.r.t their positions. Knowing the dimensions if image i.e I..these positions can be generated and then call interp1.
Best Answer