Search code examples
matlabplotmatlab-figure

How to change the color of multiple freqz plots?


I am plotting multiple frequency responses on the same plot using "hold" and "freqz" in MATLAB. Is there any way to adjust the color of each plot so I can identify which one is which? Right now it looks like a mess.

enter image description here

Freqz doesn't appear to support changing the plot's color like "plot" does.


Solution

  • It's indeed a little tricky as freqz does not provide a handle.

    b = fir1(80,0.5,kaiser(81,8));
    freqz(b,1); hold on
    c = fir1(80,0.9,kaiser(81,8));
    freqz(c,1); hold on
    

    But you can get them by using findall:

    lines = findall(gcf,'type','line');
    

    and then color the lines as usual:

    lines(1).Color = 'red'
    lines(2).Color = 'green'
    lines(3).Color = 'red'
    lines(4).Color = 'green'
    

    or for Matlab versions prior to 2014b:

    set(lines(1),'Color','red')
    set(lines(2),'Color','green')
    set(lines(3),'Color','red')
    set(lines(4),'Color','green')
    

    It works for all LineSpec properties.

    enter image description here