## Introduction to Matlab yyaxis

Matlab provides a different kind of functionality to the user; the yyaxis is the one functionality that is provided by Matlab. By using the yyaxis we can create the axes on both sides, that is the left side and right side. Basically in yyaxis left axes are activating from the current associated with the left side axes, at the same time some graphics commands choose the left side. When the current axis does not include the two y-axes at that time graphics commands add the required axis that is yaxis to yyaxis. By using yyaxis we can draw the graph as per our requirement.

**Syntax**

There are multiple syntaxes available for yyaxis as follows.

`yyaxis left activate side`

yyaxis left activate side

yyaxis (axis, specified activate side)

**Explanation**

In the first syntax, we define the left activate side of axes and that associated with the left y-axis. When we define this syntax, at the same graphics commands select the left side. If there are no axes then the graphics command adds a second y-axis.

In the second syntax, we define the right activated side of axes and that associated with the right y-axis. When we define this syntax, at the same graphics commands select the right side. If there are no axes then the graphics command added a second y-axis

In the third syntax, we can define the specified activated side of axes instead of the current axes. Similarly, there are no axes then the graphics command added two y-axes.

### How yyaxis works in Matlab?

Now let’s see how yyaxis works in Matlab as follows.

There are different ways to draw the graph as follows.

**Draw graph by using two y-axes:**

In this method, we need to use the y-axis on the left and right sides. We start the plotting graph from the left side to the right activated side. After that graphics command, select the activated right side against the provided data and specify the limit for the right y-axis.

**Now let’s see how we can add title and labels on each side:**

In this method we can add titles and labels for charts by using y-axes. To add title and labels we need to use hwtdata () and label () commands as per our requirement.

**Multiple sets on each data side:**

In this method we can plot the two different lines against records of the left y-axis by using the **hold on** command as per requirement.

**Now let’s see how we can add the colors to the chart:**

We can add color combinations by using the colororder () function. In this method we can specify the color of each side of the axes, here we can set the two different colors as per our requirement and after that, we can plot the two lines against the left side and right side y-axis.

**Now let’s see how we can control the plot colors:**

By using this method we can plot the bar chart as per our requirement and we can use different color combinations for each bar by using colororder () function.

Now let’s see what the input argument is and how it is helpful to yyaxis as follows.

Basically, we use two input arguments to plot the chart such as target axes. Normally target axes are used to specify the axes object if we miss specifying the object then yyaxis use current axes.

Yyaxis uses grid lines, color, and Line styles algorithm to plot the required chart or bar as per our requirement.

### Examples

Now let’s see the different examples of yyaxis in Matlab for better understanding as follows. Now let’s see how we can plot records by using two y axes as follows.

`a = linspace(0,8);`

b = sin(4*a);

yyaxis left

plot(a,b)

c = sin(4*a).*exp(1.5*a);

yyaxis right

plot(a,c)

ylim([-100 100])

**Explanation**

By using the above example we plot the records by using two y-axes. Here we use the linspace() function as well as sin as shown in the above example. At the end of the example, we set the limit for right side axes. The final output of the above statement we illustrated by using the screenshot as follows.

Now let’s see how we can plot a chart with multiple sets for each side as follows.

`a = linspace(0,8);`

yl1 = sin(a);

yl2 = sin(a/2);

yyaxis left

plot(a,yl1)

hold on

plot(a,yl2)

**Explanation**

In the above example, we use the hold-on command to plot the two lines against the left axes as shown in the above example. The final output of the above statement we illustrated by using the screenshot as follows.

Similarly we can plot two lines by using right side axes as follows.

`a = linspace(0,8);`

yr1 = a;

yr2 = a.^4;

yyaxis right

plot(a,yr1)

plot(a,yr2)

hold off

**Explanation**

In the above example, we use the linspace () function to plot the chart. See here when we plot right we need to use some different parameter as compared to the left y-axes. Here we use yr1 instead of yl1 and hold off of instead of hold on as shown in the above example. After that, we also need to change the direction that means left to right yyaxis as shown. The final output of the above statement we illustrated by using the screenshot as follows.

Suppose we need to activate the left side at that time we can use the following statement as follows.

`yyaxis left`

cla

**Explanation**

The final output of the above statement we illustrated by using the screenshot as follows.

Now let’s see the example of a color chart that means we can add the color as follows.

`colororder({'b','m'})`

yyaxis left

a = [2 3; 4 5];

plot(y)

yyaxis right

b = [5 4; 3 2];

plot(b)

legend

**Explanation**

In the above example, we use the colororder () command. The final output of the above statement we illustrated by using the screenshot as follows.

So in this way, we can plot any type of chart or bar as per our requirement with different color combinations.

### Conclusion

We hope from this article you learn Matlab yyaxis. From the above article, we have learned the basic syntax of yyaxis and we also see different examples of yyaxis. From this article, we learned how and when we use Matlab yyaxis.

### Recommended Articles

This is a guide to Matlab yyaxis. Here we discuss the basic syntax of yyaxis and we also see different examples of yyaxis. You may also have a look at the following articles to learn more –

3 Online Courses | 1 Hands-on Project | 8+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses