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 Matlab Plot Colors
 

Matlab Plot Colors

Priya Pedamkar
Article byPriya Pedamkar

Updated February 27, 2023

Matlab Plot Colors

 

 

Introduction to Matlab Plot Colors

MATLAB provides us with plenty of functionalities, useful in various computational problems. Apart from its computational capabilities, MATLAB is also a great tool for visualization. It provides us with the ability to create a wide variety of plots. In this article, we will focus on how to obtain plots of the required colors.

Watch our Demo Courses and Videos

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

As we are aware, we can have multiple functions plotted in a single figure; colors help us to differentiate between these functions. In MATLAB we use pre-defined color codes to get plots of desired colors.

TABLE for Color Codes

Below is the color table code as follows:

Color Code
Red r
Blue b
Cyan c
Black k
Green g
White w
Magenta m
Yellow y

Examples to Implement Matlab Plot Colors

Let us start by taking the example of 2 functions created in the same plot.

Example #1 – 2 functions

Syntax

Defining our functions:

X = linspace(-2*pi,2*pi)
a = sin (X)
b = cos (X)

  • [Defining 100 equally distant points]
  • [Defining our first function: sin function]
  • [Defining our second function: cos function]
Note: For this example, we will be creating our functions in the following colors: 1st function in Green and 2nd function in Blue. Example: plot(X, a, ‘g’, X, b, ‘b’)[Plotting our functions]

Observe in the code that we have passed pre-defined color codes as arguments (Please refer to the table at the end of the article for pre-defined color codes)

Input:

X = linspace(-2*pi,2*pi)
a = sin (X)
b = cos (X)
figure
plot(X, a, 'g', X, b, 'b')

Output:

2 functions

Explanation: Observe in the output that we have obtained our functions in the colors as expected.

Example #2 – 3 functions

Let us next take an example of 3 functions created in the same plot.

Syntax

Defining our functions:

X = linspace(-2*pi,2*pi)
a = sin (X)
b = cos (X)
c = X

  • [Defining 100 equally distant points]
  • [Defining our first function: sin function]
  • [Defining our second function: cos function]
  • [Defining our third function: a straight line]
Note: For this example, we will be creating our functions in following colors: 1st function in Green, 2nd function in Blue and 3rd function in Yellow] example:  plot(X, a, ‘g’, X, b, ‘b’, X, c, ‘y’)[Plotting our functions]

Observe in the code that we have passed pre-defined color codes as arguments (Please refer to the table at the end of the article for pre-defined color codes)

How our input and output will look like in MATLAB console?

Code:

X = linspace(-2*pi,2*pi);
a = sin (X);
b = cos (X);
c = X
figure
plot(X, a, 'g', X, b, 'b', X, c, 'y')

Output:

3 functions

Explanation: As we can see in our output, we have got our functions in the colors as passed by us in the input.

How to Set the Color of Bar Plots?

For this example, we will create a bar plot with bars of green color.

Syntax

Let us first define an array to create a Bar plot:

[71 746756 192 1007547 8133 ] bar (X, ‘g’)

  • [Defining the array]
  • [Plotting our bar plot]

Observe in the code that we have passed pre-defined color code for green as an argument (Please refer to the table at the end of the article for pre-defined color codes)

Code:

X = [71 74 67 56 192 100 75 47 81 33 ] bar (X, 'g'

Output:

Matlab Plot Colors - 3

How to Set Color of Scatter Plots?

For this example, we will scatter plot of red color

Syntax:

a = linspace(0, 2*pi,100)
b = cos (a) + rand (1, 100)
scatter (a, b, ‘r’)

  • [Defining 100 equally distant points]
  • [Defining our cos function]
  • [Plotting our scatter plot]

Observe in the code that we have passed pre-defined color code for red as an argument (Please refer to the table at the end of the article for pre-defined color codes)

Code:

a = linspace(0, 2*pi,100);
b = cos(a) + rand(1,100);
scatter(a, b, 'r')

Output:

Matlab Plot Colors - 4

Observe in the output that we have obtained a scatter plot of red color, as passed by us in the input argument.

Conclusion

MATLAB can be used to plot our data for visualizing and intuitively understanding it. There could be instances when we have to plot multiple functions in a single plot, in such cases, colors become very handy to differentiate between different functions.

Recommended Articles

This is a guide to Matlab Plot Colors. Here we discuss an introduction to Matlab Plot Colors, along with color code table and respective examples. You can also go through our other related articles to learn more –

  1. Colors in Matlab
  2. Fzero Matlab
  3. Matlab stem()
  4. Optimset 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