Updated February 28, 2023
Introduction to 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]
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:
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
- 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 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]
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:
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)])
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:
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)’})
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:
We can see in our output that there are 2 rows of labels now as expected.
Conclusion – xlabel Matlab
MATLAB can be used to plot our data for visualizing and intuitively understanding it. There could be instances when we have to provide a label to our x-axis. In such cases, we make use of the function ‘xlabel’.
Recommended Articles
This is a guide to xlabel Matlab. Here we discuss an introduction, syntax, how to use apply xlabel in Matlab with examples codes and output. You can also go through our other related articles to learn more –