How to plot to axes instead figure?

2 views (last 30 days)
Daniele Morello
Daniele Morello on 5 Oct 2015
Commented: Kirby Fears on 6 Oct 2015
hi everyone, i'm using this function:
function [ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels)
%PLOTYYY - Extends plotyy to include a third y-axis
%
%Syntax: [ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels)
%
%Inputs: x1,y1 are the xdata and ydata for the first axes' line
% x2,y2 are the xdata and ydata for the second axes' line
% x3,y3 are the xdata and ydata for the third axes' line
% ylabels is a 3x1 cell array containing the ylabel strings
%
%Outputs: ax - 3x1 double array containing the axes' handles
% hlines - 3x1 double array containing the lines' handles
%
%Example:
%x=0:10;
%y1=x; y2=x.^2; y3=x.^3;
%ylabels{1}='First y-label';
%ylabels{2}='Second y-label';
%ylabels{3}='Third y-label';
%[ax,hlines] = plotyyy(x,y1,x,y2,x,y3,ylabels);
%legend(hlines, 'y = x','y = x^2','y = x^3',2)
%
%m-files required: none
%Author: Denis Gilbert, Ph.D., physical oceanography
%Maurice Lamontagne Institute
%Dept. of Fisheries and Oceans Canada
%email: gilbertd@dfo-mpo.gc.ca
%Web: http://www.qc.dfo-mpo.gc.ca/iml/
%April 2000; Last revision: 14-Nov-2001
if nargin==6
%Use empty strings for the ylabels
ylabels{1}=' '; ylabels{2}=' '; ylabels{3}=' ';
elseif nargin > 7
error('Too many input arguments')
elseif nargin < 6
error('Not enough input arguments')
end
figure('units','normalized',...
'DefaultAxesXMinorTick','on','DefaultAxesYminorTick','on');
%Plot the first two lines with plotyy
[ax,hlines(1),hlines(2)] = plotyy(x1,y1,x2,y2);
cfig = get(gcf,'color');
pos = [0.1 0.1 0.7 0.8];
offset = pos(3)/5.5;
%Reduce width of the two axes generated by plotyy
pos(3) = pos(3) - offset/2;
set(ax,'position',pos);
%Determine the position of the third axes
pos3=[pos(1) pos(2) pos(3)+offset pos(4)];
%Determine the proper x-limits for the third axes
limx1=get(ax(1),'xlim');
limx3=[limx1(1) limx1(1) + 1.2*(limx1(2)-limx1(1))];
%Bug fix 14 Nov-2001: the 1.2 scale factor in the line above
%was contributed by Mariano Garcia (BorgWarner Morse TEC Inc)
ax(3)=axes('Position',pos3,'box','off',...
'Color','none','XColor','k','YColor','r',...
'xtick',[],'xlim',limx3,'yaxislocation','right');
hlines(3) = line(x3,y3,'Color','r','Parent',ax(3));
limy3=get(ax(3),'YLim');
%Hide unwanted portion of the x-axis line that lies
%between the end of the second and third axes
line([limx1(2) limx3(2)],[limy3(1) limy3(1)],...
'Color',cfig,'Parent',ax(3),'Clipping','off');
axes(ax(2))
%Label all three y-axes
set(get(ax(1),'ylabel'),'string',ylabels{1})
set(get(ax(2),'ylabel'),'string',ylabels{2})
set(get(ax(3),'ylabel'),'string',ylabels{3})
but this function plot data in a figure. I need to plot into an axes in GUI, instead of figure. How can i do?

Answers (1)

Kirby Fears
Kirby Fears on 5 Oct 2015
Edited: Kirby Fears on 5 Oct 2015
If you want to plot on a specific graphics object, you should pass the handle of that object into your plotyyy function. You could make this an optional argument to plotyyy.
The figure() statement inside plotyyy makes a new figure with the desired properties.
figure('units','normalized',...
'DefaultAxesXMinorTick','on','DefaultAxesYMinorTick','on');
If you pass a specific figure handle to plotyyy instead (let's call the handle h) then you can plot onto it as follows.
h=figure(1); % Creating a handle h to figure 1
% Inside plotyyy, replace the figure() call with the following:
if (you passed an optional h argument)
figure(h); % Focus on plot h
% Set the figure properties as before
set(h,'units','normalized',...
'DefaultAxesXMinorTick','on','DefaultAxesYMinorTick','on');
else
figure('units','normalized',...
'DefaultAxesXMinorTick','on','DefaultAxesYMinorTick','on');
end
Then plotyyy will plot onto the figure identified by h or onto a new figure if you did not specify an h value.
Hope this helps.
  2 Comments
Daniele Morello
Daniele Morello on 5 Oct 2015
Edited: Daniele Morello on 5 Oct 2015
really thank you for your answer. i tried to pass the handle of the graphic object (axes) to the plotyyy function, but i'm new in matlab and i don't think i've done well.
My idea was to add an additional argument "handles" to the function:
function [ax,hlines] = plotyyy(handles,x1,y1,x2,y2,x3,y3,ylabels)
and then, when i call that function i use the handle of the object, but i think it's all wrong!
if my graphic object is "axes2", how can i do?
Kirby Fears
Kirby Fears on 6 Oct 2015
Check out the findobj function. You can find the axes2 handle by finding the 'Tag' value, which is probably 'axes2'. If you inspect axes2 properties, you'll find the Tag value for sure.
As for modifying your plotyyy function, be sure you make corrections all the way through. E.g. nargin handling will need to change since you have one more input argument. I'd suggest a function declaration that looks like this:
function [ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels,h)
Then you can do nargin handling like this:
if nargin==6,
%Use empty strings for the ylabels
ylabels{1}=' '; ylabels{2}=' '; ylabels{3}=' ';
h=figure;
elseif nargin == 7,
h=figure;
elseif nargin > 8,
error('Too many input arguments');
elseif nargin < 6,
error('Not enough input arguments');
end
Now your h handle will be defined even if you don't provide it as a function input. All that if/else handling I suggested earlier would no longer be necessary. You can always apply the settings directly to h whether it is a function input or a brand new figure:
% Set figure properties
set(h,'units','normalized',...
'DefaultAxesXMinorTick','on','DefaultAxesYMinorTick','on');
Hope this helps.

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!