시간에 따라 변하는 현상을 보기 위해서, 

한 페이지에 한 번에 20개 정도의 선을 plot 할 때가 있습니다.


자동으로 시간에 따라 gradation으로 색이 변하게 plot하는 방법을 찾아보던 중

저에게 딱 맞는 방법을 찾았습니다.


가령, 다음과 같이 부터 까지 한번에 plot을 하되,

색이 점차적으로 변하도록 해보겠습니다.






우선 MATLAB 명령 창에 입력하는 코드는 다음과 같습니다. 



>> x = [-30:30];                   : plot할 x의 범위는 -30부터 30까지

>> fx = @(x) -x.^2;              :  정의

>> C=jet;                            : 내장된 색깔 불러오기 (64가지 색)

>> for i = 1:20                     : i = 1부터 20까지

y = i*fx(x);                             : y을 계산하고

CC = C(ceil((i/20)*64),:);           : 64가지 색을 대략 20등분하여 차례로 사용하기 

                                             (ceil을 통해 i/20을 정수로 바꿔주었습니다)

plot(x,y,'Color',CC);                 : color는 방금 계산한 CC가 가리키는 색

hold on; drawnow;                  : plot 유지시키고, loop 돌며 바로바로 plot시키기

end





참고로 jet는 명령창에서 호출하면 다음과 같이 생겼습니다.


>> jet


ans =


         0         0    0.5625

         0         0    0.6250

         0         0    0.6875

         0         0    0.7500

         0         0    0.8125

         0         0    0.8750

         0         0    0.9375

         0         0    1.0000

         0    0.0625    1.0000

         0    0.1250    1.0000

         0    0.1875    1.0000

         0    0.2500    1.0000

         0    0.3125    1.0000

         0    0.3750    1.0000

         0    0.4375    1.0000

         0    0.5000    1.0000

         0    0.5625    1.0000

         0    0.6250    1.0000

         0    0.6875    1.0000

         0    0.7500    1.0000

         0    0.8125    1.0000

         0    0.8750    1.0000

         0    0.9375    1.0000

         0    1.0000    1.0000

    0.0625    1.0000    0.9375

    0.1250    1.0000    0.8750

    0.1875    1.0000    0.8125

    0.2500    1.0000    0.7500

    0.3125    1.0000    0.6875

    0.3750    1.0000    0.6250

    0.4375    1.0000    0.5625

    0.5000    1.0000    0.5000

    0.5625    1.0000    0.4375

    0.6250    1.0000    0.3750

    0.6875    1.0000    0.3125

    0.7500    1.0000    0.2500

    0.8125    1.0000    0.1875

    0.8750    1.0000    0.1250

    0.9375    1.0000    0.0625

    1.0000    1.0000         0

    1.0000    0.9375         0

    1.0000    0.8750         0

    1.0000    0.8125         0

    1.0000    0.7500         0

    1.0000    0.6875         0

    1.0000    0.6250         0

    1.0000    0.5625         0

    1.0000    0.5000         0

    1.0000    0.4375         0

    1.0000    0.3750         0

    1.0000    0.3125         0

    1.0000    0.2500         0

    1.0000    0.1875         0

    1.0000    0.1250         0

    1.0000    0.0625         0

    1.0000         0         0

    0.9375         0         0

    0.8750         0         0

    0.8125         0         0

    0.7500         0         0

    0.6875         0         0

    0.6250         0         0

    0.5625         0         0

    0.5000         0         0


jet의 사이즈를 통해 총 64개의 색이 RGB값으로 저장되어있음을 알 수 있습니다.


>> size(jet)


ans =


    64     3



'Engineering > Matlab' 카테고리의 다른 글

Matlab 내장합수: ceil  (0) 2016.05.24
Matlab: drawnow, 실시간으로 그래프 그리기  (0) 2016.05.09
Mablab: 그래프 편집하기  (1) 2016.05.09

우선 matlab에서 help ceil 을 통해 ceil이 어던 함수인지 살펴보았습니다.



>> help ceil

ceil   Round towards plus infinity.

    ceil(X) rounds the elements of X to the nearest integers

    towards infinity.

 


간단히 말하자면, 올림을 해주는 함수입니다.


ceil(5.3) = 6

ceil(2.9) = 6

ceil(-5.2) = -5

ceil(-0.7) = 0


간혹 유용하게 쓰일 일이 있을 것 같네요.



drawnow




for 루프가 다 돌고 그래프가 그려진다



for i = 1:11

plot ([time(i),time(i)],[kmsd(i),kpsd(i)],'r.-');

hold on;

end






drawnow가 호출되는 시점마다 그래프가 그려진다.



for i = 1:11

plot ([time(i),time(i)],[kmsd(i),kpsd(i)],'r.-');

hold on;

drawnow;

end



● 그래프 이름 지정

title('그래프 이름');

ex) title('Spiraea's lab');



● x축, y축 제목 지정 (labeling)

xlabel('x축 제목');

ylabel('y축 제목');

ex) xlabel('this is x-axis');

ylabel('this is y-axis');



● x축, y축 범위 지정

axis([xmin, xmax, ymin, ymax]); ==> axis([x축 최솟값, x축 최댓값, y축 최솟값, y축 최댓값]);

ex) axis([0,810,0.995,1.025])



● 글자 크기 조정

1) 범례(legend)의 글자 크기: set(legend이름, 'FontSize', 글자크기);

(이것을 사용하기 위해서는 legend 지정을 할 때, legend 이름을 설정해주어야 한다.)

 ex) legend_A = legend('A');

set(legend_A, 'FontSize', 15);


2) 전체 글자 크기 조절: set(gca,'FontSize',글자크기);

ex) set(gca,'FontSize',20);



● 축의 간격 조정

1) x축:

> set(gca,'XTick',[최솟값:간격:최댓값]);

> set(gca,'XTick',[원하는 값 나열]);

ex) set(gca,'XTick',[-5:0.5:7]);

set(gca,'XTick',[0 1 3 8]);

2) y축은 XTick 대신 YTick 사용



● 축의 눈금 변경

1) x축: set(gca,'XTickLabel',눈금 이름);

ex) set(gca, 'XTickLabel', {'h','e','l','l','o'});


2) y축은 XTick 대신 YTick 사용



● 축의 유효 숫자 변경

1) x축의 유효 숫자 변경

ex) set(gca, 'XTickLabel', num2str(get(gca, 'XTick')', '%.3f'));


2) y축의 유효 숫자 변경

ex) set(gca, 'YTickLabel', num2str(get(gca, 'YTick')', '%.3e'));




<참고>

>> num2str : number to string (숫자에서 문자열로 바꿔주는 함수)

num2str(get(gca, 'YTick')', '%.3e') : y축의 숫자를 소수점 3자리 까지의 지수형식의 문자열로 변환.


num2str Convert numbers to a string.


    T = num2str(X) converts the matrix X into a string representation T

    with about 4 digits and an exponent if required.  This is useful for

    labeling plots with the TITLE, XLABEL, YLABEL, and TEXT commands.

 

    T = num2str(X,N) converts the matrix X into a string representation

    with a maximum N digits of precision.  The default number of digits is

    based on the magnitude of the elements of X.

 

    T = num2str(X,FORMAT) uses the format string FORMAT (see SPRINTF for

    details).

 

    Example 1:

        num2str(randn(2,2),3) produces a string matrix such as

 

         1.44    -0.755

        0.325      1.37

 

    Example 2:

        num2str(rand(2,3) * 9999, '%10.5e\n') produces a string matrix

        such as

 

        8.14642e+03

        1.26974e+03

        6.32296e+03

        9.05701e+03

        9.13285e+03

        9.75307e+02








적용




>> plot(x,y);

>> title('random number');

>> xlabel('sequence');

>> ylabel('probability');

>> axis([0 11 -0.1 1.1]);

>> set(gca,'FontSize',12);

>> set(gca,'YTick',[0:0.1:1]);

>> set(gca,'XTickLabel',{'a','b','c','d','e','f'});

>> set(gca, 'YTickLabel', num2str(get(gca, 'YTick')', '%.3e'));




+ Recent posts