# Loren on the Art of MATLAB

## Multiple Y Axes

We were musing here about how common it is to want more than two Y axes on a plot. Checking out the File Exchange, there seem to be several candidates, indicating that this is something at least some people find useful.

### Contents

#### Sample Plot

Here's a sample plot using plotyy that comes with MATLAB.

x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
plotyy(x,y1,x,y2,'plot');


#### List of Some Possibilities

In addition to plotyy in MATLAB, here's a list of some of the candidates from the File Exchange.

#### What are You Plotting with More Y Axes?

I am curious to know what kind of data or results you are plotting so that having multiple y-axes makes a compelling presentation. Let us know here.

Get the MATLAB code

Published with MATLAB® R2013a

### 15 Responses to “Multiple Y Axes”

1. sean replied on :

I use plotyy occasionally, but I can’t think of a specific case right now. I am, however, using plotxx off the file exchange, and often wonder why there is a plotyy but no plotxx. The reason I am using it is to show the spectrum of the same signal at different sample rates, where the sample rate is the x-axis. If I use the same x axis for the different sample rates, the over sampled signal dominates the resulting plot, and I have to constantly zoom in to see what I want. plotxx makes it handy to see both signals at the same time.

2. Alexandre Kozlov replied on :

I didn’t find how to synchronize two Y-axes. For your exemple, how to show 0 on the left axes and 0.2 on the right being at the same level.

Another problem concerns X-axes — there are really 2 X-axes with 2 sets of Ticks and TickLabels. So if you want to change somethong (for exemple, show dates at the X-axes), you have to either do it twice, either hide one of these X-axes.

3. Loren Shure replied on :

Thanks Sean and Alexandre,

It’s very useful for us to hear your use cases and issues. I’ve created enhancement requests for your suggestions.

–Loren

4. Richard replied on :

Matlab graphics restrictions is one reason I still use Excel to present results. An example presents the risk-reward of an investment:
3 data sets, 2 y-axes, common date x-axis
1 data set is a vertical bar-chart of compounded returns over a rolling time period.
1 data set is an overlaid vertical bar-chart (different color) of return volatility over the same rolling time period.
1 data set is a line-plot (2nd y-axis) of the Sharpe ratio, again, over the same rolling time period.
I would post a jpeg of this, but don’t know how.

5. Eric replied on :

Hi Loren, I don’t use dual Y-axes very often, but I would give someone at TWM a hug if they implemented formattable axis labels (via XTickLabelFormat?). Being able to either specify things like longg and/or C-style format strings would be awesome. Sometimes the current behavior drives me a little crazy with lack of sig-figs and exponent usage (is it like format short?)

6. Ofer replied on :

Matlab graphics are great and powerful. I often use 2 y-axes for presenting data but I do it myself because as the previous responders pointed out the plotyy function is quite limited when it comes to synchronization or x-axis control so I prefer to play with the graphic handles myself to get the required result. It is tedious and somewhat customized for each project but it gets the pretty result that I need.
One such example is plotting ESS profile data where one y-axis is temperature and the other y-axis is vibration amplitude and the x-axis is time.
One example of needing more than 2 y-axes is plotting scope data. If you want to plot 4-channels of scope data, it would be ideal to plot each channel with its own y-axis (synchronized in some way) so that we can observe or measure timing in the plots (one relative to another). The way I do it now is I normalize the data of 3 of the channels to the 1st channel, thus I have the timing right but the y-axis is meaningless for 3 of the channels.

7. Loren Shure replied on :

Eric-

Please go to MathWorks support web site (link on right of my blog) and enter in an enhancement request. Thanks.

–Loren

8. Loren Shure replied on :

Ofer-

Thanks for the detailed info. As you said, the things that cause you some pain are similar to those mentioned in earlier comments. It’s very helpful to hear the points reiterated though.

–loren

9. Brad Stiritz replied on :

Alexandre,

>I didn’t find how to synchronize two Y-axes. For your example, how to show 0 on the left axes and 0.2 on the right being at the same level.

You should take a look at addaxis_unit() on the MATLAB File Exchange. The description of that function sounds exactly what you’re looking for: “Function adds a second y-axis (right) to the figure in such a way that only one curve is shown but with two different units.”

I use that function extensively when I want to conceptualize the same graphical data in two completely different systems, e.g. linear-space & log-space.

I can’t find in my historical notes why I concluded that yyplot() wouldn’t work for me. Is it perhaps that the horizontal ticks can’t be guaranteed to be aligned in yyplot()?

If you look at my comments on the addaxis_unit(), the author was very kind to add this capability at my request, as well as a couple of other tweaks. As you can see by the dates of my posts, it’s been awhile since I discovered that function. I take it for granted now, but I’m very grateful for this function.

10. Bruce replied on :

(I’ll be upfront, it’s been several years since I tried this in MATLAB)…

I agree with Richard and excel. I plot lots of plots where 2 properties covary, but have VASTLY different scales. These plots, on occasion need to be “visually” compared with other plots. It’s “trivially easy” in excel to make 2 plots, one XY, the other XYY where the x axis is “physically” the same length on both plots (in fact it’s hard to make them different). My experience in MATLAB is that when you add the second Y axis, it shrinks the length of the X axis so that the overall width of the plot, including labels, etc, remains the same.

11. Thierry Dalon replied on :

Hello Loren,
It would be really great if Matlab could support multiple Y-axis. Use Cases coming from the automotive industry: we get measurements from a car or test bench and want to visualize them: different signals with different units/scale but same time x-axis. Or we simulate a control algorithm in Simulink (Model-based development) and want to visualize the logged signals in Matlab. Most of the time here we have to use other Data plotting tools like ETAS MDA or AVL Concerto. But it would be nice to have such plot functions in Matlab.
I can recommend for this purpose the POTW FEX plt http://www.mathworks.de/matlabcentral/fileexchange/4936 by Paul Mennen. Other alternative could be to use JFreeChart plots; see for example http://undocumentedmatlab.com/blog/specialized-matlab-plots/.

12. Loren Shure replied on :

My thanks as well for your use cases, input, and suggestions.

–Loren

13. David replied on :

Wow, this was really useful for a novice such as myself! Exactly what I needed. Thanks a lot!

14. HW replied on :

Hi,
It’s good to have the information in this post. I wonder if it can also be extended to plots with 2 x-axes and 2 y-axes? I encounter like this: I have a force vs. displacement plot and I want to have one plot with multiple units say the force both in Newton and lb also the displacement both in m and in. Can somebody please help? :)

Thanks!

15. Jiro Doke replied on :

@HW,

I have a function called plot2axes that’s good for your use case. It doesn’t work with PLOTYY and the other alternatives for multiple axes for multiple data sets, though.

http://www.mathworks.com/matlabcentral/fileexchange/7426

Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.

These postings are the author's and don't necessarily represent the opinions of MathWorks.