0

I have 2 plots with the same x data:

x=[0 100];
y1=x^2;
y2=e^x;

I want to plot y2 by semilogy (i.e in log scale). How can I plot them together? Left side - y1 Y axis; Right side - y2 Y axis;

delkov
  • 301
  • 3
  • 15

1 Answers1

1

Well, a quick Google search of the terms that you provide in your question (i.e., 'semilogy' and 'plotting two axes matlab') would have shown you what I'm showing you. Anyhow...

You can use the built-in yyaxis function to...

Create a chart with two y-axes.

You can use the built-in semilogy function to...

Create a plot with a logarithmic scale for the y-axis and a linear scale for the x-axis.

Putting it all together, this is pretty much just a generalized version of the code provided in the yyaxis function documentation I linked above...

x = [ insert your x-data];
y1 = insert-your-first-func;
yyaxis left
plot(x,y1)

y2 = insert-your-second-func;
yyaxis right
semilogy(x,y2)

EDIT: If using a Matlab version <2016a, then you won't be able to take advantage of the utility of the yyaxis function. In that case, there are plenty of questions on StackOverflow (like this one and that one) that explain how you might go about plotting two sets of data on the same x-axis, but different y-axes (i.e., linear and semilog, for instance).

The answer is in the plotyy documentation too! Here it is:

plotyy(X1,Y1,X2,Y2,'function1','function2') uses function1(X1,Y1) to plot the data for the left axis and function2(X2,Y2) to plot the data for the right axis.

function can be either a function handle or a string specifying plot, semilogx, semilogy, loglog, stem, or any MATLAB® function that accepts the syntax: h = function(x,y)

The code above would now look like...

x = [ insert your x-data];
y1 = insert-your-first-func;
y2 = insert-your-second-func;
plotyy(x,y1,x,y2,'plot','semilogy');

Happy coding! Remember that Google is your friend!

Community
  • 1
  • 1
Vladislav Martin
  • 1,504
  • 2
  • 15
  • 34
  • That's true. If the OP is using a version of MATLAB <2016a, then `plotyy` is their best bet. There are plenty of questions on StackOverflow, like [here](http://stackoverflow.com/questions/2676004/different-right-and-left-axes-in-a-matlab-plot) and [here](http://stackoverflow.com/questions/28634426/create-an-xy-plot-with-two-y-axis), that already explain how to use `plotyy` for this purpose. – Vladislav Martin May 17 '16 at 13:46
  • 2
    @BillBokeey I'll mention it in my answer, just so anyone who finds this questions knows there are other options, too :) – Vladislav Martin May 17 '16 at 13:49