Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Is it possible to specify certain plotting properties when using plotyy()?

Subject: Is it possible to specify certain plotting properties when using plotyy()?

From: Kate J.

Date: 17 Nov, 2013 22:40:24

Message: 1 of 6


I have an unusual set of data that I would like to display using the plotyy() function. Data Set A has pairs of values that need to be connected by a line, but each pair should appear separate from all other pairs. Data Set B consists of values that were collected in between each pair of values from Data Set A. Currently, I have had no problem using the regular plot() function to generate a reasonable plot, although the scale of Set B is not shown. For reference, here is the code for the single-Y-axis plot I generate:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

x_set1 = [1 2];
x_set2 = [3 4];
x_set3 = [5 6];
x_set4 = [7 8];
x_set5 = [9 10];

S1_data_subset1 = [-0.18 -0.18];
S1_data_subset2 = [-0.42 -0.38];
S1_data_subset3 = [-0.42 -0.44];
S1_data_subset4 = [-0.42 -0.48];
S1_data_subset5 = [-0.60 -0.72];

x_learning = [1.5 3.5 5.5 7.5 9.5];
S1_learning = [19 1 1 9 1];
 
figure;
plot(x_set1, S1_data_subset1,'ko-','LineWidth',2);
hold on;
plot(x_set2, S1_data_subset2,'ko-','LineWidth',2);
hold on;
plot(x_set3, S1_data_subset3,'ko-','LineWidth',2);
hold on;
plot(x_set4, S1_data_subset4,'ko-','LineWidth',2);
hold on;
plot(x_set5, S1_data_subset5,'ko-','LineWidth',2);
hold on;
plot(x_learning, S1_learning,'bo','LineWidth',2);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

However, now I am trying to use the plotyy() function so that Data Set B’s value will have some meaning, and ideally so that I will be able to scale the 2 y axes so that I can match up their 0 values to be the same. However, I’m having difficulty with using plotyy(); the following code:

x_set1 = [1 2];
x_set2 = [3 4];
x_set3 = [5 6];
x_set4 = [7 8];
x_set5 = [9 10];
 
S1_data_subset1 = [-0.18 -0.18];
S1_data_subset2 = [-0.42 -0.38];
S1_data_subset3 = [-0.42 -0.44];
S1_data_subset4 = [-0.42 -0.48];
S1_data_subset5 = [-0.60 -0.72];
 
x_learning = [1.5 3.5 5.5 7.5 9.5];
S1_learning = [19 1 1 9 1];
 
x_learning1 = [1.5];
x_learning2 = [3.5];
x_learning3 = [5.5];
x_learning4 = [7.5];
x_learning5 = [9.5];
 
S1_learning1 = [19];
S1_learning2 = [1];
S1_learning3 = [1];
S1_learning4 = [9];
S1_learning5 = [1];
 
[AX1,H1,H2] = plotyy(x_set1,S1_data_subset1,x_learning1,S1_learning1);
hold on;
[AX2,H3,H4] = plotyy(x_set2,S1_data_subset2,x_learning2,S1_learning2);
hold on;
[AX3,H5,H6] = plotyy(x_set3,S1_data_subset3,x_learning3,S1_learning3);
hold on;
[AX4,H7,H8] = plotyy(x_set4,S1_data_subset4,x_learning4,S1_learning4);
hold on;
[AX5,H9,H10] = plotyy(x_set5,S1_data_subset5,x_learning5,S1_learning5);


… generates a figure that is problematic because:
~ there are 2 sets of x axis labels overlaid and it is a mess
~ the second Y axis has multiple values overlaid at each tick mark
~ the Edit -->Axes Properties dialog box only allows me access to the first (and already correct) Y axis
~ I can’t seem to specify the marker types and sizes for the 2 data sets

I have tried to manage the handle properties, but these don’t appear to be able to change the marker size & line style properties that I need to modify.

Does anyone have suggestions about how to solve the above issues? Thanks in advance for your help!

Subject: Is it possible to specify certain plotting properties when using plotyy()?

From: Steven Lord

Date: 18 Nov, 2013 15:16:32

Message: 2 of 6


"Kate J. " <kmj.work@gmail.com> wrote in message
news:l6bgko$rdl$1@newscl01ah.mathworks.com...
>
> I have an unusual set of data that I would like to display using the
> plotyy() function. Data Set A has pairs of values that need to be
> connected by a line, but each pair should appear separate from all other
> pairs. Data Set B consists of values that were collected in between each
> pair of values from Data Set A. Currently, I have had no problem using the
> regular plot() function to generate a reasonable plot, although the scale
> of Set B is not shown. For reference, here is the code for the
> single-Y-axis plot I generate:

*snip some of the code*

> hold on;
> plot(x_set5, S1_data_subset5,'ko-','LineWidth',2);
> hold on;
> plot(x_learning, S1_learning,'bo','LineWidth',2);

Yes, this works. PLOT doesn't create a new axes normally.

> However, now I am trying to use the plotyy() function so that Data Set Bs
> value will have some meaning, and ideally so that I will be able to scale
> the 2 y axes so that I can match up their 0 values to be the same.
> However, Im having difficulty with using plotyy(); the following code:

*snip more code*

> hold on;
> [AX4,H7,H8] = plotyy(x_set4,S1_data_subset4,x_learning4,S1_learning4);
> hold on;
> [AX5,H9,H10] = plotyy(x_set5,S1_data_subset5,x_learning5,S1_learning5);

This creates six separate axes. That's not what you want.

> generates a figure that is problematic because:
> ~ there are 2 sets of x axis labels overlaid and it is a mess

Six, actually.

> ~ the second Y axis has multiple values overlaid at each tick mark
> ~ the Edit -->Axes Properties dialog box only allows me access to the
> first (and already correct) Y axis
> ~ I cant seem to specify the marker types and sizes for the 2 data sets
>
> I have tried to manage the handle properties, but these dont appear to be
> able to change the marker size & line style properties that I need to
> modify.
> Does anyone have suggestions about how to solve the above issues? Thanks
> in advance for your help!

Don't call PLOTYY multiple times. Instead, make use of the fact that PLOT
and PLOTYY treat NaN values in the data to be plotted as "blanks"

    M1 = [x_set1, NaN, x_set2, NaN, x_set3, NaN];
    M2 = [x_learning1, NaN, x_learning2, NaN, x_learning3, NaN];
    Y1 = [S1_data_subset1, NaN, S1_data_subset2, NaN, S1_data_subset3, NaN];
    Y2 = [S1_learning1, NaN, S1_learning2, NaN, S1_learning3, NaN];
    [AX, H1, H2] = plotyy(M1, Y1, M2, Y2);

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Is it possible to specify certain plotting properties when using plotyy()?

From: Kate J.

Date: 18 Nov, 2013 19:24:11

Message: 3 of 6

Thanks for your response, Steven.

>> Don't call PLOTYY multiple times. Instead, make use of the fact that PLOT
>> and PLOTYY treat NaN values in the data to be plotted as "blanks"

>> M1 = [x_set1, NaN, x_set2, NaN, x_set3, NaN];
>> M2 = [x_learning1, NaN, x_learning2, NaN, x_learning3, NaN];
>> Y1 = [S1_data_subset1, NaN, S1_data_subset2, NaN, S1_data_subset3, NaN];
>> Y2 = [S1_learning1, NaN, S1_learning2, NaN, S1_learning3, NaN];
>> [AX, H1, H2] = plotyy(M1, Y1, M2, Y2);

Your suggested code above is a good start. However, I still have some lingering problems:

~ I would like to have substantial markers for each data point (e.g. o or triangle markers), particularly for Data Set B, which, if the markers aren't enlarged, appear only as tiny, almost invisible single pixels. However, I can't seem to change the marker size using plotyy() in the same way that this can easily be done using plot().

~ I'm not sure how to specify the axes limits. I would like the left Y axis to have limits of [-2, 2] and the right Y axis to have limits of [0,20] (or, if possible, to have the 0 values of both axes line up, and include -20 as the lower limit on the right axis). It doesn't appear that the 'Alim' property is what I should modify, and if I try to do this, nothing changes. If there is only one variable for the axis, how would it be possible to change the properties of 2 different axes?

~ Also, I would like to add labels to both Y axes, but similarly to the axes limits issue above, I'm not sure how to specify this using plotyy(). If I try to do

set(AX, 'Ylabel', 'Mean Rating Value'); ... I get an error that the 'value must be a handle'; however, if I try set(H1,'Ylabel','Mean Rating Value'); , I get:
??? Error using ==> set
There is no 'Ylabel' property in the 'lineseries' class.

Thanks for any insights you have!

Subject: Is it possible to specify certain plotting properties when using plotyy()?

From: Kate J.

Date: 19 Nov, 2013 19:10:07

Message: 4 of 6

I made some progress, and now have the axes correctly labeled. However, I'm still encountering the problem of not being able to correctly specify the axes limits. Below is my full script & a description of the specific problem:

x_set1 = [1 2];
x_set2 = [3 4];
x_set3 = [5 6];
x_set4 = [7 8];
x_set5 = [9 10];

S1_data_subset1 = [-0.18 -0.18];
S1_data_subset2 = [-0.42 -0.38];
S1_data_subset3 = [-0.42 -0.44];
S1_data_subset4 = [-0.42 -0.48];
S1_data_subset5 = [-0.60 -0.72];

x_learning = [1.5 3.5 5.5 7.5 9.5];
S1_learning = [19 1 1 9 1];

x_learning1 = [1.5];
x_learning2 = [3.5];
x_learning3 = [5.5];
x_learning4 = [7.5];
x_learning5 = [9.5];

S1_learning1 = [19];
S1_learning2 = [1];
S1_learning3 = [1];
S1_learning4 = [9];
S1_learning5 = [1];

M1 = [x_set1, NaN, x_set2, NaN, x_set3, NaN, x_set4, NaN, x_set5, NaN];
M2 = [x_learning1, NaN, x_learning2, NaN, x_learning3, NaN, x_learning4, NaN, x_learning5, NaN];
Y1 = [S1_data_subset1, NaN, S1_data_subset2, NaN, S1_data_subset3, NaN, S1_data_subset4, NaN, S1_data_subset5, NaN];
Y2 = [S1_learning1, NaN, S1_learning2, NaN, S1_learning3, NaN, S1_learning4, NaN, S1_learning5, NaN];
[AX, H1, H2] = plotyy(M1, Y1, M2, Y2);
xlabel('Session','FontSize',14,'FontWeight','bold');
    
set(get(AX(1),'Ylabel'),'String','Mean Value','FontSize',14,'FontWeight','bold');
set(get(AX(2),'Ylabel'),'String','Net Change (%)','FontSize',14,'FontWeight','bold');
    
% If I use this, the left Y axis becomes extremely compacted...
% ylim([-2 2]);

If I adjust the left Y axis using the Ylim() function, instead of having these [-2, 2] values span the entire left Y axis, these values are instead compacted (and interestingly, these left-Y-axis values do not at all match up with the right Y axis scale's values). Again, as I mentioned in my previous post above, using Edit --> Axes properties only affects the left Y axis, and accomplishes the same incorrect change as calling ylim().

To summarize, I'd like my left Y axis to have limits of [-2,2] (without being squished into a small area of this axis) and the right Y axis to have limits of [-20,20]. And I want my 2nd data set to have substantial markers so that the plotted values are visible, instead of being single pixels. Thanks for your suggestions.

Subject: Is it possible to specify certain plotting properties when using plotyy()?

From: Steven Lord

Date: 19 Nov, 2013 22:58:02

Message: 5 of 6


"Kate J. " <kmj.work@gmail.com> wrote in message
news:l6dpgr$m1$1@newscl01ah.mathworks.com...
> Thanks for your response, Steven.
>
>>> Don't call PLOTYY multiple times. Instead, make use of the fact that
>>> PLOT
>>> and PLOTYY treat NaN values in the data to be plotted as "blanks"
>
>>> M1 = [x_set1, NaN, x_set2, NaN, x_set3, NaN];
>>> M2 = [x_learning1, NaN, x_learning2, NaN, x_learning3, NaN];
>>> Y1 = [S1_data_subset1, NaN, S1_data_subset2, NaN, S1_data_subset3,
>>> NaN];
>>> Y2 = [S1_learning1, NaN, S1_learning2, NaN, S1_learning3, NaN];
>>> [AX, H1, H2] = plotyy(M1, Y1, M2, Y2);
>
> Your suggested code above is a good start. However, I still have some
> lingering problems:
>
> ~ I would like to have substantial markers for each data point (e.g. o or
> triangle markers), particularly for Data Set B, which, if the markers
> aren't enlarged, appear only as tiny, almost invisible single pixels.
> However, I can't seem to change the marker size using plotyy() in the same
> way that this can easily be done using plot().

It's easy with PLOT because you're creating many different lines. With this
PLOTYY call, you're creating one for each axes, split into multiple segments
visually by the NaNs.

You could define your data sets to be columns of larger M1 and Y1 matrices,
since if I recall correctly both PLOTYY and PLOT handle columns of matrices
to create multiple lines.

http://www.mathworks.com/help/matlab/ref/plot.html

"If X and Y are both matrices, then they must have equal size and MATLAB
plots columns of Y versus columns of X."

> ~ I'm not sure how to specify the axes limits. I would like the left Y
> axis to have limits of [-2, 2] and the right Y axis to have limits of
> [0,20] (or, if possible, to have the 0 values of both axes line up, and
> include -20 as the lower limit on the right axis). It doesn't appear that
> the 'Alim' property is what I should modify, and if I try to do this,
> nothing changes. If there is only one variable for the axis, how would it
> be possible to change the properties of 2 different axes?

PLOTYY does a lot of work to try to synchronize the ticks on the two axes.
If you manually manipulate the limits of those axes, they will not be
automatically synchronized anymore. You will need to adjust the ticks using
something like the code in this thread:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/164525#417882

> ~ Also, I would like to add labels to both Y axes, but similarly to the
> axes limits issue above, I'm not sure how to specify this using plotyy().
> If I try to do
> set(AX, 'Ylabel', 'Mean Rating Value'); ... I get an error that the 'value
> must be a handle'; however, if I try set(H1,'Ylabel','Mean Rating Value');
> , I get:
> ??? Error using ==> set
> There is no 'Ylabel' property in the 'lineseries' class.
>
> Thanks for any insights you have!

AX contains the handles of _two_ axes. Try:

set(AX(1), 'YLabel', 'Mean rating value')
set(AX(2), 'YLabel', 'Other axes');

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Is it possible to specify certain plotting properties when using plotyy()?

From: Kate J.

Date: 21 Nov, 2013 19:08:06

Message: 6 of 6

Thanks for your advice, Steve. As I mentioned in my earlier post above, in concurrence with your suggestion, using set() works nicely to specify axes labels. I'll look into rearranging my data into columns of matrices. Thanks for the link showing how to adjust the ticks.

-- Kate

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us