plotting two axes semilog matlab

matlab matlab-figure

1273 观看

1回复

94 作者的声誉

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 的来源 发布者: 2016 年 5 月 17 日

回应 (1)


1

1004 作者的声誉

决定

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!

作者: Vladislav Martin 发布者: 17.05.2016 01:35
32x32