1
votes

I am trying to create area plots with 2 y-axis using plotyy by using 'area' function. I don't want to have any tick marks or labels or titles but I just want the outside box. I would also like to save just the plot (not the entire figure window) as a png file.

When I turn off the x and y-axis ticks and labels, the box looks thin on the bottom and left, thick on the top and right. What am I doing wrong?

Also, if I try to make the box 'LineWidth' fat, I see two tick marks at the bottom left and bottom right - which ruin the plot and I am unable to remove. Can someone help?

My test code is below:

x = 1:100;
y1 = rand(size(x));
y2 = 100*rand(size(x));

fig_handle = figure('units','inches','position',[1 1 9 3]);
[a,p1,p2] = plotyy(x,y1,x,y2,'area');

c1 = get(p1,'child');
c2 = get(p2,'child');

set(c1,'facea',0.5,'FaceColor','b','EdgeColor',[0 0 0]);
set(c2,'facea',0.5,'FaceColor','r','EdgeColor',[0 0 0]);
set(c1,'Line','None');
set(c2,'Line','None');

set(a,'Layer','top')
set(a,'XTick',[]);
set(a(1),'YTick',[]);
set(a(2),'YTick',[]);
set(a,'TickDir','in')
set(a,'LineWidth',5);

Also, notice how the left Y-axis has red area on it, while the right Y-axis doesn't. Is this fix-able?

Any help would be appreciated! Also, I am new to StackOverflow - so, if these are too many questions in one post, please pardon me and I will post them as separate requests/questions.

1
"When I turn off the x and y-axis ticks and labels, the box looks thin on the bottom and left, thick on the top and right. What am I doing wrong ?" - Can you post a screenshot please ? - Ratbert
This is how the plot appears: i60.tinypic.com/1238b5u.png Of course, this is before the LineWidth is set to 5. I am not entirely sure what's happening. - Sandeep Makam

1 Answers

1
votes

Here is a workaround for the red appearing on the left Y-axis.

Since the axes line is quite thick, the data displayed close to it is drawn over it. To avoid this, you can slightly shift the x limits of the axes to make more room for the data. Do so by changing the XLim property of either axes since they share the same x limit:

XL = get(a,'Xlim');

xl = XL{1}; %// here XL{1} and XL{2} are the same...[1 100]
set(a(:),'Xlim',[xl(1)-.5 xl(2)+.5])

As for the annoying tick marks at the bottom of the plot, I must say that I don't know how to remove them while keeping the axes visible.

As an alternative solution to plotyy, here is a way to obtain a good result (I think) without plotyy. The trick is to superimpose 2 axes and make both of them not visible, then set the figure's color to white and add a black rectangle surrounding the plot.

Here is the code:

clear
clc
close all

x = 1:100;
y1 = rand(size(x));
y2 = 100*rand(size(x));

H1 = area(x,y1);

%// Set the figure color to white
set(gcf,'Color','w')


%// Plot data and set different properties for each axes
A1 = gca;
A2 = axes('Position',get(A1,'Position'));

H2 = area(x,y2);

set(A2,'YAxisLocation','right','Color','none','XTickLabel',[]);
set(A2,'XLim',get(A1,'XLim'),'XTick',[],'YTick',[]);

set(A1,'XTick',[],'YTick',[]);

%// Make both axes not visible.
set(A2,'Visible','off')
set(A1,'Visible','off')

axes(A1)

%// Get axes limits
XL = get(gca,'XLim');
YL= get(gca,'YLim');

%// Create rectangle as a bounding box
rectangle('Position',[XL(1) YL(1) XL(2)-1 YL(2)],'LineWidth',5)


%//===
%// Change the data color/properties
hP = findobj('Type','patch');

set(hP(1),'FaceColor','b','FaceAlpha',.5,'EdgeColor',[0 0 0],'line','none')
set(hP(2),'FaceColor','r','FaceAlpha',.5,'EdgeColor',[0 0 0],'line','none')

And the output:

enter image description here

It's not perfect but hope that helps!