delkov
delkov

Reputation: 311

plotting two axes semilog matlab

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;

Upvotes: 0

Views: 2803

Answers (1)

Vladislav Martin
Vladislav Martin

Reputation: 1674

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!

Upvotes: 1

Related Questions