plot graphic with different color

조회 수: 3 (최근 30일)
Giuseppe Dibenedetto
Giuseppe Dibenedetto 2020년 2월 14일
댓글: fred ssemwogerere 2020년 2월 14일
Hi,
I have a data set made by two vector x and y.
I need to plot it to obtain an effect like the one attached.
So i need the lines to be green if y >= 0,
and red elsewhere.
Can anyone help me?
  댓글 수: 1
Bob Thompson
Bob Thompson 2020년 2월 14일
If you have a pair of vectors, x and y, how are you getting lines? Are you using the x and y values of corresponding elements in the vectors to make cartesian coordinates? Are you connnecting these coordinates to each other or to a common point?
Do you have any code that you have written out so far?

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

답변 (2개)

fred  ssemwogerere
fred ssemwogerere 2020년 2월 14일
Hello, with logical indexing, you could create two separate pairwise sets of data, with one set comprised of plots of x versus y-data greater than zero, and another set comprised of x and y-values less than zero. These can then be plotted on the same figure but with different 'color' properties.
  댓글 수: 1
fred  ssemwogerere
fred ssemwogerere 2020년 2월 14일
Something like:
% Assuming your 'x' and 'y' vectors as;
x=[3 2 4 3 5 1];y=[-1 3 -5 2 -3 6];
% For y>0, use logical indexing to create separate set, while also choosing the corresponding values in 'x'
x1=x(y>0);y1=y(y>0);
% Do the same for y<0
x2=x(y<0);y2=y(y<0);
% Go ahead and plot (assuming you are using the 'plot' function)
figure
handl1=plot(x1,y1,'-r');
hold on
handl2=plot(x2,y2,'-g');
hold off

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


Bob Thompson
Bob Thompson 2020년 2월 14일
If you're using the plot command, it is relatively easy to define a different color for a plot. Setting the condition where this color applies is more difficult, and requires we have a better understanding of your application to answer.
plot(x,y,'g'); % Plot the x and y values as cartesian coordinates with a green line connecting them.

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by