You are reading the article Complete Guide To Examples To Implement Xlabel Matlab updated in September 2023 on the website Cersearch.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Complete Guide To Examples To Implement Xlabel Matlab
Introduction to xlabel MatlabMATLAB, 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.
Start Your Free Data Science Course
Hadoop, Data Science, Statistics & others
Let us start by taking the example of a bar plot depicting salaries of employees.
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 ] 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]
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:
A = [ 20000, 25000, 30000, 42000, 70000, 35000 ] xlabel('Employee Salary', 'color', 'g')
Output:
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 = 2023[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] xlabel(['Sales in millions for half year',num2str(year)])
Output:
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 ‘2023’ 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’, ‘(2023)’})
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] xlabel({'Sales in millions for half year','(2023)'})
Output:
We can see in our output that there are 2 rows of labels now as expected.
Conclusion – xlabel MatlabMATLAB 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 ArticlesThis 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 –
You're reading Complete Guide To Examples To Implement Xlabel Matlab
Update the detailed information about Complete Guide To Examples To Implement Xlabel Matlab on the Cersearch.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!