Categories

# Matlab color codes

Matlab provides the color code functionality to the user. Basically, Matlab is a very good visualization and by using color codes we can draw a variety of graphs. In which users can be able to plot any color as per their requirement. Matlab provides different color combinations such as blue, green, red, cyan, and magenta, yellow, black and white, etc. So we can use any color combination to plot the graph or any other uses such as 3D works. By using color codes we can use any color, the color contains the hexadecimal value or we can say that RGB values. Another way to access the color code is that we can use short names and long names to access the color codes.

## Syntax

`A = expression 1;B = expression 1:Plot (A, B, ‘Color’, ‘Color code’)`

## Explanation

In the above syntax, we use two different expressions, with the help of these two expressions we plot the graph, and that expression we specify by using the variable name that A and B as shown in the above syntax. After that we plot the function to draw the graph with the desired value that we mentioned in the expression, here we just call the variable A and B with color code. The color code parameter is used to specify the color that we want to use to draw the graph.

## How do color codes work in Matlab?

Now let’s see how color code works in Matlab as follows.

Basically, there are different ways to use color code as follows.

## First is that RGB value:

The first way we use color code in Matlab, here we can use RGB value for color code and it contains the three different elements and that specify the basic color combination such as red, green, and blue, and range of this element is that [0 1].

## For example:

Suppose we need to use black and red to plot the graph at that time we can use the following RGB value for that color as follows.[0 0 0] for black and [1 0 0] for red as per our requirement we can use any color code combination.

#### Now let’s see the second way is that short name:

This is a second way to import the color in Matlab, here instead of RGB value we can use short naming of color.

## For example:

Suppose we need to import the yellow and cyan color in Matlab at that time we can use the following short names of color as follows.

for yellow and c for cyan.

#### Now let’s see the third way is that long name:

In this method, we can use the full name of color instead of RGB and short name as per user requirement that we call long name.

## For example:

Suppose we need to plot the graph with magenta and green at that time we can use the same name of the color that magenta and green.

There is another way to use color code is that hexadecimal code combination.

## Examples

Now let’s see the different examples of color code in Matlab for better understanding as follows.

``````x = linspace(-3*pi,3*pi);
y = sin(x);
hex = '#FF00011';
colorcode = sscanf(hex(2:end),'%2x%2x%2x',[1 3])/255;
figure
plot(x, y, 'Color', colorcode)``````

## Explanation

In the above example we have two expressions for x-axis and y-axis as shown, after that, we specify the hexadecimal code that we want to use and that code we store into the hex variable. Notice here we use hexadecimal color code but we need to convert it into the RCG value as shown in the above example. The remaining part of this example is simple which means we plot the graph with color parameters. The find output of the above example we illustrated by using the following screenshot as follows.

Now let’s see how we can use the short names in Matlab as follows.

``````plot([1 2 3],'-b')
hold on
plot([3 2 1],'--r')
hold off``````

## Explanation

This is a very simple example of plotting the graph, here we plot a graph with two different lines such as dotted and solid with the different color combinations as shown in the above example. The find output of the above example we illustrated by using the following screenshot as follows.

Now let’s see another example of color code as follows.

`A=-200:1.2:200;B=A.^4-A.^1;plot(A,B,'b')`

## Explanation

This is another example of a short name color code; here suppose we need to plot the graph with blue color at that time we can use a b character instead of the blue word as shown in the above example. The find output of the above example we illustrated by using the following screenshot as follows.

Now let’s see how we can draw lines in triangle format as follows.

`A=-200:1.2:200;B=A.^4-A.^1;plot(A,B,'vm')`

## Explanation

Here we consider the same example, in this example we just add the v with the color code as shown and the direction of a triangle is downward. The find output of the above example we illustrated by using the following screenshot as follows.

Similarly, we can draw the line by using the triangle with upward as follows.

`A=-200:1.2:200;B=A.^4-A.^1;plot(A,B,'^m')`

## Explanation

In this example, we just change the color code ^m instead of vm as shown in the above example. The find output of the above example we illustrated by using the following screenshot as follows.

Now let’s see how we can use star patterns in line with the color code as follows.

`A=-200:1.2:200;B=A.^4-A.^1;plot(A,B,'*c')`

## Explanation

By using the above example we try to plot the line with star pattern here we just add the * symbol with color code here we use cyan color code as shown in the above example. The find output of the above example we illustrated by using the following screenshot as follows.

Similarly, we can use different shapes to plot the graph such as circle, plus, triangle, square and diamond, etc.