Customizing MATLAB Plots and Subplots

23 views (last 30 days)
VS
VS on 25 Jun 2019
Commented: Bjorn Gustavsson on 27 Jun 2019
In an economics paper (https://www.federalreserve.gov/econres/feds/files/2018043pap.pdf), all the plots have invisible top axis, with Y-axis values marked on the right hand side with the axis label on top of it, differently from they way MATLAB plots figures in which Y-axis values are marked on the left hand side and axis label is written beside it in a vertical manner.
If you see their figure 9, for instance, which has 4 by 3 subplots, they all share the aforesaid features. More importantly, each individual plot is longer than wider.
THEIR PLOTS
MATLAB PLOTS
I want to generate plots like theirs in MATLAB. I looked around and experimented but couldn't find a solution. Worse, I cannot even figure out which application they have used for all their figures, if it's not MATLAB. Will appreciate any help.

Accepted Answer

dpb
dpb on 25 Jun 2019
Edited: dpb on 26 Jun 2019
The new(ish) yyaxis function to arbitrarily create two y axes and then switch between left and right axes w/o the need of yyplot to have two separate plot() arguments is useful for such things. As Bjorn says, it's a lot of fine tuning; some of which just can't do with the default Matlab components--like you don't have the flexibility to move axes labels so you'll just have to do them manually.
Here's another very rough starting point---
yyaxis right % create two axes; RH axis has focus
hAx=gca; % get a handle (YAxis property is the two axes handles)
hAx.YAxis(1).TickLabel=[]; % don't show labels on LH axis
set(hAx.YAxis,{'Color'},{'k'}) % use sedate color...
hTx=text(1,1,'Percent of GDP','HorizontalAlignment','right','VerticalAlignment','bottom');
produces
which has most of the features other than the partial box besides the tick marks at the top. You can always simulate this by line if it's important enough.
ADDENDUM
With Steven L's "gotcha!" comment, the above can be generated by using
hRYlab=ylabel('Percent of GDP','Position',[1 1],'HorizontalAlignment','right', ...
'VerticalAlignment','bottom','Rotation',0);
Since it's a Y-axis label, Matlab by default writes it vertically so have to override that with the 'Rotation' parameter even tho is the default for a text() object....
Definitely better to use the axes properties and the access via x/ylabel() functions beats having to go handle-diving inside the axes object for hidden properties from its viewpoint.
My bad for not having carried through and written a label and probed it instead of jumping to the wrong conclusion regarding visibility -- have to admit in 30 yrs, don't know I've ever done anything to an axis label but change font size or the like, though.
  12 Comments
VS
VS on 27 Jun 2019
Thank you all for your time and input. How do I accept both the answers and is it possible? Should I just click on 'accept'? I ask because both the answers were helpful and I don't want to click on 'accept' for one answer, only to realize that now I cannot 'accept' the other answer.
Bjorn Gustavsson
Bjorn Gustavsson on 27 Jun 2019
Don't worry that much about it, accept the most useful and put a vote for the other useful contributions.
More importantly: Pass it on forward. When you see a question or problem you can help with do it!

Sign in to comment.

More Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 25 Jun 2019
There are contributions on the FEX that facilitate multiple y-axises. This level of figure-tuning often (for me)
turns into a bit of handcraft-work. Something like this might get the axis-locations a few steps toward what you
want:
y = rand(1,7);
[yy1,yy2,yy3] = plotyy(1:7,y,1:7,y);
set(yy3,'color','r')
set(yy1(1),'ycolor','k','yticklabel','','box','off')
set(yy1(2),'ycolor','k')
HTH
  2 Comments
VS
VS on 25 Jun 2019
Thank you, your answer certainly takes me a step closer to what I want to do -- it hides the top axis and marks the Y-axis values on the right hand side. Can I also do it in a subplot environment, for example, in a 4 by 3 subplot as in the question? Is there anyway I can make all of my subplots longer than wider as in the question figure and have them individually numbered on the top left hand side for each subplot?
dpb
dpb on 25 Jun 2019
See alternate Answer and follow-up comment thereto...
"axes are axes" -- it doesn't make any difference whether they're subplots or not, you treat them the same other than needing to keep the handle to which is which in the array of handles you're (hopefully) saving when you create them.

Sign in to comment.

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!