How to move labels in a R plot?

How do I use subplot to plot everything from an imported .fig file (while preserving legend+axes+labels)?

  • subplot(2,2,1) a = open('blah.fig') I'd like blah.fig to be subplot 1. some subproblems: legend: figures localized at http://www.atmos.uw.edu/~akchen0/CERES_Project/. ==== Extra details at:http://stackoverflow.com/questions/23717172/how-do-i-use-subplot-on-an-external-figure-that-i-opened-while-preserving-axes. Looking around at the resources available on other sites, it looks like this is a difficult problem... Quick synopsis of where I'm at:

  • Answer:

    I have two versions of the code below. Outputs from the code+versions follow. Version 2 is the newest one with most of the functionality you need imo. Version 1 is the older version and follows under the header '[Older junk]'. The thing to realize about Matlab is that the figure window is not a static, vector image, but a fully functional GUI implemented by wrapping java objects/classes/methods (the abstract windows toolkit). This creates both annoying problems with arcane hierarchy/documentation as well as wonderful functionality. The figure window and GUIDE in Matlab are almost exactly the same implementation-wise. Yair Altman uses this vector to create his cool interface hacks. But the final graphical quality of the images is left for the user to tweak and most users don't do any (This is also why plot.ly thinks it has a business case). The initial problems that you had in using Sulimon's suggestion occurred  because the original .figs have their appearances rigidly defined. So the simple copyobj approach didn't work because you are copying fixed parameters from a fixed graphics environment to a different graphics environment without any constraints. So my approach is to discard all graphics properties except the minimum I need to retain the primary appearance, and let Matlab's handle graphics defaults take care of the rest. Because you replot everything in this approach, you can knock yourself out reanalyzing the data and manipulating the visual appearance of the plots. The problem with legends is not completely solved. You will have to do some work if you really need your legend plotted neatly. My approach is to detect the legend child (from the identifier Tag), modify it's OuterPosition, Position, and FontSize properties, then copyobj to new plot. Like in my other answer. Here's what results look like w/ script ver 2. Change the var plotsPerWindow to increase/decrease num of subplots per figure. WARNING. Changing plotsPerWindow will require modification in legendLocation entries for legends to locate correctly. Script Version 2 The variable fdir should have http : // in front. Quora automatically renders the link in the edit box itself, which then displays the page title as the clickable text - and then finally it renders the title to plain text when the server parses the link as being code. This bug also keeps me from using struct arrays like Page on structure.ar close all; clear all; clc; warning off % get rid of java depreciation warning %% Usar defined properties % Dummy block for search path/directory declaration fdir='www.atmos.uw.edu/~akchen0/CERES_Project/'; flist={'Net12MonthMA_HemisphericDifs_0-90.fig', 'Precip12MonthMA_HemisphericDifs_0-90.fig','NetClear12MonthMA_HemisphericDifs_0-90.fig'}; FigNumber=11; % Initialize figure numbering variable, should be >=2 plotsPerWindow=3; % Define subplots per window. Assumption, subplots are columns subplotNumber=1; % Initialize subplotNumber. No need to change this. % Location of legends wrt gcf. Determined manually. Will be a pain in butt for >500 images, can be automated, but I am bored now. % To automate this, get OuterPos and Pos of each legend handle in set of % figures. Uncomment line 68 and run code on all figs you need plotted. % Then analyze the numbers and their trends. You should be able to predict % those, w/ formula or interpolation (use splines for exact fit). legendLocation={[0.894 0.81 0.08 0.06], ... [17.4 4.5 3 2],... [17.4 1.6 3 2]}; % Define the properties attached to axes objects that need extracting axisParameters={'GridLineStyle','LineWidth','Title','XLabel','YLabel','YAxisLocation','XLim','XTick','XTickLabel','YLim','YTick','YTickLabel'}; % get(gca,'GridLineStyle') %char % Equivalent manual implementation % get(gca,'LineWidth') %double % get(get(gca,'Title'),'String') %char % get(get(gca,'XLabel'),'String') %char % get(get(gca,'YLabel'),'String') %char % get(gca,'YAxisLocation') %char <--- Marker for dataset on the right axis % get(gca,'XLim') %double array % get(gca,'XTick') %double array % get(gca,'XTickLabel') %char array % get(gca,'YLim') %double array % get(gca,'YTick') %double array % get(gca,'YTickLabel') %char array % Define properties attached to lineseries objects that need extracting lineParameters={'XData','YData','Color','DisplayName','LineStyle'}; % get(b(1),'XData') %double array % get(b(1),'YData') %double array % get(b(1),'Color') %double array % get(b(1),'DisplayName') %char % get(b(1),'LineStyle') %char %% Dummy block for file i/o and management % Fetch files; for i=1:length(flist) fn{i}=['F' num2str(i,'%.4d')]; % New file name, also gives us a var % Check if file exists. If it does, skip; if it dont, fetch. urlwrite % reads from the url and writes it to pwd if exist([fn{i},'.fig'],'file')==0, urlwrite([fdir, flist{i}],[fn{i} '.fig']); end end %% Main loop for j=1:length(flist) % Open/Raise figure h1=openfig(fn{j},'new','invisible'); % Get child objects attached to figure a=get(gcf,'Children'); % Identify which one is legend object, the other two are assumed to be % axes objects from plotyy. Assumption that there's only 1 legend % object/orig. plot. There is no restriction on number of axes objects. for i=1:length(a),if isequal(get(a(i),'Tag'),'legend'), iLegend=i;end;end iAxis=setdiff(1:length(a),iLegend); % Separate legend from non-legend entries. % % % Get data from legend. We only need names. % % strLegend=get(a(iLegend),'String'); % get(a(iLegend),'OuterPosition'), get(a(iLegend),'Position') %% Get the object properties for u=1:length(iAxis) % Cycle through axes objects % Extract properties from current axes and store in cell container called axisProp for i=1:length(axisParameters) % Cycle through axes properties that need extraction pstr=axisParameters{i}; % Get axis parameters if isequal('Title',pstr) || isequal('YLabel',pstr) || isequal('XLabel',pstr) % Check if the property is a child to axes axisProp(u,i)={get(get(a(iAxis(u)),pstr),'String')}; else axisProp(u,i)={get(a(iAxis(u)),pstr)}; end end b(u)={get(a(iAxis(u)),'Children')}; % Get the lineseries children attached to uth axis % Extract properties from lineseries objects and store in cell container called lineProp for i=1:length(b{u}) % Cycle through lineseries objects for k=1:length(lineParameters) % Cycle through lineseries properties that need extraction lineProp(u,i,k)={get(b{u}(i),lineParameters{k})}; end end end %% Variables to control plot process % Create conditionals that prevent axes objects from being declared multiple times junkCreateRightAxis=true; junkCreateLeftAxis=true; junkExistAxes=false; %% Start the plot process f1=figure(FigNumber); vv=subplot(plotsPerWindow,1,subplotNumber); grid on; axes1=gca; % Define current subplot position to be the one to track for u=1:length(iAxis) % Cycle through axes objects for i=1:length(b{u}) % Cycle through lineseries attached to each axes object if isequal(axisProp{u,6},'right') % Distinguish between RHS or LHS axes if junkCreateRightAxis set(axes1,'Color','none','YAxisLocation','right', 'XLim',axisProp{u,7}, 'YColor',lineProp{u,i,3}, 'XTick',axisProp{u,8}, 'XTickLabel',axisProp{u,9}, 'YLim',axisProp{u,10}, 'YTick',axisProp{u,11}, 'YTickLabel',axisProp{u,12}); junkCreateRightAxis=false; junkExistAxes=true; end p1=line(lineProp{u,i,1},lineProp{u,i,2},'Color',lineProp{u,i,3},'LineStyle',lineProp{u,i,5},'LineWidth',3,'Parent',axes1); % set() title(axisProp{u,3}),xlabel(axisProp{u,4}),ylabel(axisProp{u,5}), else % Plot on left axis if junkCreateLeftAxis axes2=axes('Position',get(axes1,'Position'),'Color','none','YAxisLocation','left', 'XLim',axisProp{u,7},'YLim',axisProp{u,10},'XTick',axisProp{u,8}, 'XTickLabel',axisProp{u,9}, 'YTick',axisProp{u,11}, 'YTickLabel',axisProp{u,12}); junkCreateLeftAxis=false; end; line(lineProp{u,i,1},lineProp{u,i,2},'Color',lineProp{u,i,3},'LineWidth',3,'LineStyle',lineProp{u,i,5},'Parent',axes2); title(axisProp{u,3}),xlabel(axisProp{u,4}),ylabel(axisProp{u,5}), end end end % End of plot process % Convert the lowest axes background to white from transparent set(axes1,'Color',[1 1 1]); % Remove properties from legend object, then copy to current plot set(a(iLegend), 'FontSize',10, 'OuterPosition',legendLocation{j}, 'Position',legendLocation{j}); copyobj(a(iLegend),f1); close(h1); % Close the original figure %% Update counters for figure & subplots subplotNumber=subplotNumber+1; if subplotNumber>plotsPerWindow subplotNumber=1; % Step subplot number FigNumber=FigNumber+1; % Step figure set(get(f1,'JavaFrame'),'Maximized',true); % Resize the plot window to fit the screen. Does not require localization. end end % Use cyclefigs to scroll through the mess you just created. %eof-ssh-May 20, 2014 [Older junk] 'All right. The current version of the script follows. There's no legend import functionality yet. But I will finish that as I get the time. I will also add more comments and generalize it for your purpose later. Here's the output with two plots - You will have to localize it for your display. Output with three plots Output with four plots (the two plots from the first figure are repeated) Have fun. I will update typos/bugs as I notice them. Script Version 1 close all; clear all; clc % Dummy block for search path/directory declaration fdir= 'www.atmos.uw.edu/~akchen0/CERES_Project/' flist={'Net12MonthMA_HemisphericDifs_0-90.fig', 'Precip12MonthMA_HemisphericDifs_0-90.fig','NetClear12MonthMA_HemisphericDifs_0-90.fig'}; % Fetch files; Dummy block for file i/o and management for i=1:length(flist) fn{i}=['F' num2str(i,'%.4d')]; % New file name, also gives us a var % Check if file exists. If it does, skip; if it dont, fetch. urlwrite % reads from the url and writes it to pwd if exist([fn{i},'.fig'],'file')==0, urlwrite([fdir, flist{i}],[fn{i} '.fig']); end end % Read file, store data in var called fn{i} for j=1:length(flist) % Open/Raise figure h1=openfig(fn{j},'new','invisible'); % Get child objects attached to figure a=get(gcf,'Children'); % Identify which one is legend object, the other two are assumed to be % axes objects from plotyy and plot. Assumption that there's only 1 % legend object. But we can have more than 2 plot overlays for i=1:length(a),if isequal(get(a(i),'Tag'),'legend'), iLegend=i;end;end iAxis=setdiff(1:length(a),iLegend); % Get data from legend. We need names only strLegend=get(a(iLegend),'String'); % Get data from axis. Cycle through iAxis. % Each axis represents one call to plot. axisParameters={'GridLineStyle','LineWidth','Title','XLabel','YLabel','YAxisLocation','XLim','XTick','XTickLabel','YLim','YTick','YTickLabel'}; % get(a(3),'GridLineStyle') %char % get(a(3),'LineWidth') %double % get(get(a(3),'Title'),'String') %char % get(get(a(3),'XLabel'),'String') %char % get(get(a(3),'YLabel'),'String') %char % get(a(3),'YAxisLocation') %char <--- Tells you which one's on the right axis % get(a(3),'XLim') %double array % get(a(3),'XTick') %double array % get(a(3),'XTickLabel') %char array % get(a(3),'YLim') %double array % get(a(3),'YTick') %double array % get(a(3),'YTickLabel') %char array % Each plot can have multiple lineseries. Cycle through nLines (children to axis) % Each line can have multiple properties. lineParameters={'XData','YData','Color','DisplayName','LineStyle'}; % get(b(1),'XData') %double array % get(b(1),'YData') %double array % get(b(1),'Color') %double array % get(b(1),'DisplayName') %char % get(b(1),'LineStyle') %char for u=1:length(iAxis) % Cycle through axis for i=1:length(axisParameters) % Cycle through parameters pstr=axisParameters{i}; % Get axis parameters if isequal('Title',pstr) || isequal('YLabel',pstr) || isequal('XLabel',pstr) axisVal(u,i)={get(get(a(iAxis(u)),pstr),'String')}; else axisVal(u,i)={get(a(iAxis(u)),pstr)}; end end b=get(a(iAxis(u)),'Children'); % Lineseries children for uth axis for i=1:length(b) for k=1:length(lineParameters) lineVal(u,i,k)={get(b(i),lineParameters{k})}; end end end junkRight=true; junkLeft=true; figure(11), subplot(length(flist),1,j), grid on; for u=1:length(iAxis) % Cycle through axis b=get(a(iAxis(u)),'Children'); for i=1:length(b) if isequal(axisVal{u,6},'right') if junkRight axes1=gca; set(axes1,'Color','none','YAxisLocation','right', 'XLim',axisVal{u,7}, 'YColor',lineVal{u,i,3}, 'XTick',axisVal{u,8}, 'XTickLabel',axisVal{u,9}, 'YLim',axisVal{u,10}, 'YTick',axisVal{u,11}, 'YTickLabel',axisVal{u,12}); junkRight=false; end line(lineVal{u,i,1},lineVal{u,i,2},'Color',lineVal{u,i,3},'LineStyle',lineVal{u,i,5},'LineWidth',3,'Parent',axes1); title(axisVal{u,3}),xlabel(axisVal{u,4}),ylabel(axisVal{u,5}), else if junkLeft axes2=axes('Position',get(axes1,'Position'),'Color','none','YAxisLocation','left', 'XLim',axisVal{u,7},'YLim',axisVal{u,10},'XTick',axisVal{u,8}, 'XTickLabel',axisVal{u,9}, 'YTick',axisVal{u,11}, 'YTickLabel',axisVal{u,12}); junkLeft=false; end; h2=line(lineVal{u,i,1},lineVal{u,i,2},'Color',lineVal{u,i,3},'LineWidth',3,'LineStyle',lineVal{u,i,5},'Parent',axes2); title(axisVal{u,3}),xlabel(axisVal{u,4}),ylabel(axisVal{u,5}), end end end set(axes1,'Color',[1 1 1]) end set(gcf,'OuterPosition',[ -7 33 1936 1056])

Sid Hazra at Quora Visit the source

Was this solution helpful to you?

Other answers

First get the handle of the subplot you want h = subplot(2,2,1) then use copyobj a = open('blah.fig') blah_items = get(gca,'Children'); copyobj(blah_items,h)

Sulimon Sattari

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.