EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial xlabel Matlab
 

xlabel Matlab

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

xlabel Matlab

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

  • 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:

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.

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 –

  1. Matlab Plot Circle
  2. Summation in Matlab
  3. Matlab Line Style
  4. Fzero Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW