Search code examples
matlabtextlabel

set a plotyy function vs text in matlab


I need to plot two sets of data on separate y axis and plot them vs an x that is text

y1 = 

    0.0217545037216382
    0.0218522372528616
    0.00848250610252238
    0.00631477252766555
    0.00836543852985331
    0.00404498017959712
    0.0111088524041279
    0.0137053885611881
    0.0127748811974527
    0.0127058407416728

y2= [3,3,3,4,5,4,5,4,3,4]

x={'AU10','WI11','SP11','AU11','WI12','SP12','AU12','SP13','AU13','SP14']

I couldn't plot vs text so I created

x2=[1:10]

plotyy(x2,y1,x2,y2)

then to change x to the text I used

set(gca,'XTickLabel',x);

but the problem is the 1:10 labels are still there underneath the text labels so I tried removing the labels with

set(gca,'XTick',[])
set(gca,'XTickLabel',[])

and

set(gca,'Ticklength',[0 0])

also tried replacing with this

set(gca, 'XTickLabel',x, 'XTick',1:numel(x))

but the 1:10 string is still there underneath the text string.


Solution

  • The problem arises because plotty creates two axes. When you do set(gca,'XTickLabel',x); you are affecting only one of those axes (gca refers to only one axis, probably the one that was created last).

    Solution:

    1. Get handles to the two axes

      h = plotyy(x2,y1,x2,y2);
      

      (check that h is a 1x2 vector of handles)

    2. Set the 'Xticklabel' property of both axes to the desired labels:

      set(h,'XTickLabel',x);
      

      Alternatively, set 'Xticklabel' of one of the axes to the desired labels, and for the other axis set it to [] (no labels):

      set(h(1),'XTickLabel',x);
      set(h(2),'Xticklabel',[])