Categories
3. Matlab Functions

xlabel Matlab

MATLAB, as we know, is a great tool for visualization. It provides us with ability to create a wide variety of plots. In this article we will focus on how to label x axis according to our requirement. In MATLAB, xlabels function is used to set a custom label for x axis.

Let us start by taking the example of a bar plot depicting salaries of employees.

Syntax

Let A be an array containing salaries of 6 employees Jim, Pam, Toby, Mike, Sam, Steve; in the same order.

  • A = [ 20000, 25000, 30000, 42000, 70000, 35000 ][Defining an array of salaries]

Now we will plot a bar graph for the data above. We will pass our names as categorical array:

  • B = categorical ({‘Jim’, ‘Pam’, ‘Toby’, ’ Mike’, ‘Sam’, ‘Steve’ })[Defining an array of employee names]
  • B = reordercats(B, {‘Jim’, ‘Pam’, ‘Toby’, ‘Mike’, ‘Sam’, ‘Steve’})[Calling ‘reordercats’ to preserve the order]
  • bar (B, A)  [Creating the bar pot]

Now, we will name our x-axis as “Employee Salary”. For doing so, we will be using ‘xlabel’ function.

  • xlabel({‘Employee Salary’})[Setting the name of x-axis]

Note: That in above line of code, we have passed ‘Employee Salary’ as an argument to ‘xlabel’

This is how our input and output will look like in MATLAB console:

Code:

A = [ 20000, 25000, 30000, 42000, 70000, 35000 ] B = categorical ({'Jim', 'Pam', 'Toby', 'Mike', 'Sam', 'Steve' })
B = reordercats(B, {'Jim', 'Pam', 'Toby', 'Mike', 'Sam', 'Steve'})
bar (B, A)
xlabel({'Employee Salary'})

Output:

Employee Salary

As we can see, we have named our x axis as ‘Employee Salary’.

How to get the x-axis label in the color of our choice?

In the above output, MATLAB has created x-axis label in black color. Now what if we want it to be in some other color? For our understanding, we will create x-label in green color.

Syntax:

  • = [ 20000, 25000, 30000, 42000, 70000, 35000 ][Defining an array of salaries]

Now we will plot a bar graph for the data above. We will pass our names as categorical array:

  • B = categorical ({‘Jim’, ‘Pam’, ‘Toby’, ’ Mike’, ‘Sam’, ‘Steve’ })[Defining an array of employee names]
  • B = reordercats(B, {‘Jim, ‘Pam, ‘Toby, Mike, ‘Sam’, ‘Steve’})[Calling ‘reordercats’ to preserve the order]
  • bar (B, A) [Creating the bar pot]

Now, we will set the color of label for our x-axis as green. For doing so, we will be passing ‘g’in argument.

  • xlabel(‘Employee Salary’, ‘color’, ‘g’)[Setting the name and color]

Note: That in above line of code, we have passed ‘color’ and ‘g’ as an argument to ‘xlabel’

This is how our input and output will look like in MATLAB console:

Code:

A = [ 20000, 25000, 30000, 42000, 70000, 35000 ] B = categorical ({'Jim', 'Pam', 'Toby', 'Mike', 'Sam', 'Steve' })
B = reordercats(B, {'Jim', 'Pam', 'Toby', 'Mike', 'Sam', 'Steve'})
bar (B, A)
xlabel('Employee Salary', 'color', 'g')

Output:

color of our choice

Note: In the above output that ‘Employee Salary’ is now in green color as expected by us.

How to add value from a variable to our xlabel?

For this example, we will see data of half-yearly sales:

Syntax:

  • a = [10.1, 12.1, 14.1, 16.1, 18.1, 20.1][Defining the array with sales in millions ]
  • b = categorical({‘Jan’, ‘Feb’, ‘March’, ‘April’, ‘May’, ‘June’})[Respective months]
  • bar(b, a)
  • year = 2019[Declaring the variable from which we will extract the value]
  • xlabel([‘Sales in millions for half year’,num2str(year)])

Note: In the above code that we passed our variable as an argument 

This is how our input and output will look like in MATLAB console:

Code:

a = [10.1, 12.1, 14.1, 16.1, 18.1, 20.1] b = categorical({'Jan', 'Feb', 'March', 'April', 'May', 'June'})
bar(b, a)
year = 2019
xlabel(['Sales in millions for half year',num2str(year)])

Output:

xlabel Matlab - 3

As we can clearly see n the output, we have obtained the value of the year from our variable.

In the above example we can also create a multiline label; i.e we can have the label ‘Sales in millions for half year’ in one line and ‘2019’ in another line. Let us see how to do it.

Syntax:

  • a = [10.1, 12.1, 14.1, 16.1, 18.1, 20.1][Defining the array with sales in millions ]
  • b = categorical({‘Jan’, ‘Feb’, ‘March’, ‘April’, ‘May’, ‘June’})[Respective months]
  • bar(b, a)
  • xlabel({‘Sales in millions for half year’, ‘(2019)’})

Note: In the above code that we have passed an array of characters 

This is how our input and output will look like in MATLAB console:

Code:

a = [10.1, 12.1, 14.1, 16.1, 18.1, 20.1] b = categorical({'Jan', 'Feb', 'March', 'April', 'May', 'June'})
bar(b, a)
xlabel({'Sales in millions for half year','(2019)'})

Output:

xlabel Matlab - 4

We can see in our output that there are 2 rows of labels now as expected.

Leave a Reply

Your email address will not be published. Required fields are marked *