Specify Plot Colors - MATLAB & Simulink (2024)

Specify Plot Colors

MATLAB® creates plots using a default set of colors. The default colors provide a clean and consistent look across the different plots you create. You can customize the colors if you need to. Many plotting functions have an input argument such as c or colorspec for customizing the color. The objects returned by these functions typically have properties for controlling the color. The names of the arguments and properties can vary, but the values they accept typically follow a common pattern. Once you are familiar with the pattern, you can use it to modify a wide variety of plots.

The following examples use the bar and scatter functions to demonstrate the overall approach for customizing colors. For a complete list of valid color values for a specific plotting function, refer to the documentation for that function.

Types of Color Values

There are these types of color values:

  • Color Name or Short Name — Specify the name of a color such as "red" or "green". Short names specify a letter from a color name, such as "r" or "g".

  • RGB Triplet — Create a custom color by specifying a three-element row vector whose elements are the intensities of the red, green, and blue components of a color. The intensities must be in the range [0,1]. For example, you can specify a shade of pink as [1 0.5 0.8].

    Some function arguments that control color do not accept RGB triplets, but object properties that control color typically do.

  • Hexadecimal Color Code — Create a custom color by specifying a string or a character vector that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" all specify the same shade of orange.

    Some function arguments that control color do not accept hexadecimal color codes, but you can specify a hexadecimal color code using a name-value argument that corresponds to an object property. For example, scatter(x,y,sz,"MarkerFaceColor","#FF8800") sets the marker color in a scatter plot to orange.

This table lists all of the valid color names and short names with the corresponding RGB triplets and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
"red""r"[1 0 0]"#FF0000"

Specify Plot Colors- MATLAB & Simulink (1)

"green""g"[0 1 0]"#00FF00"

Specify Plot Colors- MATLAB & Simulink (2)

"blue""b"[0 0 1]"#0000FF"

Specify Plot Colors- MATLAB & Simulink (3)

"cyan" "c"[0 1 1]"#00FFFF"

Specify Plot Colors- MATLAB & Simulink (4)

"magenta""m"[1 0 1]"#FF00FF"

Specify Plot Colors- MATLAB & Simulink (5)

"yellow""y"[1 1 0]"#FFFF00"

Specify Plot Colors- MATLAB & Simulink (6)

"black""k"[0 0 0]"#000000"

Specify Plot Colors- MATLAB & Simulink (7)

"white""w"[1 1 1]"#FFFFFF"

Specify Plot Colors- MATLAB & Simulink (8)

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots. These colors do not have names associated with them.

RGB TripletHexadecimal Color CodeAppearance
[0 0.4470 0.7410]"#0072BD"

Specify Plot Colors- MATLAB & Simulink (9)

[0.8500 0.3250 0.0980]"#D95319"

Specify Plot Colors- MATLAB & Simulink (10)

[0.9290 0.6940 0.1250]"#EDB120"

Specify Plot Colors- MATLAB & Simulink (11)

[0.4940 0.1840 0.5560]"#7E2F8E"

Specify Plot Colors- MATLAB & Simulink (12)

[0.4660 0.6740 0.1880]"#77AC30"

Specify Plot Colors- MATLAB & Simulink (13)

[0.3010 0.7450 0.9330]"#4DBEEE"

Specify Plot Colors- MATLAB & Simulink (14)

[0.6350 0.0780 0.1840]"#A2142F"

Specify Plot Colors- MATLAB & Simulink (15)

Specify Color of a Bar Chart

Open Live Script

Create a red bar chart by calling the bar function and specifying the optional color argument as "red". Return the bar object as b, so you can customize other aspects of the chart later.

b = bar(1:10,"red");

Specify Plot Colors- MATLAB & Simulink (16)

Now, change the bar fill color and outline color to light blue by setting the FaceColor and EdgeColor properties to the hexadecimal color code,"#80B3FF".

Before R2019a, specify an RGB triplet instead of a hexadecimal color code. For example, b.FaceColor = [0.5 0.7 1].

b.FaceColor = "#80B3FF";b.EdgeColor = "#80B3FF";

Specify Plot Colors- MATLAB & Simulink (17)

Specify Marker Colors in a Scatter Plot

Open Live Script

Create a scatter plot of random numbers. Specify the marker size as 75 points, and use name-value arguments to specify the marker outline and fill colors. The MarkerEdgeColor property controls the outline color, and the MarkerFaceColor controls the fill color.

x = rand(1,100);y = rand(1,100);scatter(x,y,75,"MarkerEdgeColor","b", ... "MarkerFaceColor",[0 0.7 0.7])

Specify Plot Colors- MATLAB & Simulink (18)

Specify Colors in a Series of Plots

Open Live Script

There are two ways to create a series of plots:

  • Call a plotting function multiple times and use the hold function to retain the contents of the axes.

  • Pass a matrix containing multiple data series to the plotting function. The plot function has always accepted matrix inputs, and many other plotting functions also support matrix inputs.

To specify colors with either approach, call the desired plotting function with an output argument so you can access the individual plot objects. Then set properties on the plot object you want to change.

For example, create a scatter plot with 100-point filled markers. Call the scatter function with an output argument s1. Call the hold function to retain the contents of the axes, and then call the scatter function two more times with output arguments s2 and s3. The variables s1, s2, and s3 are Scatter objects.

figurex = 1:5;s1 = scatter(x,[6 3 9 10 7],100,"filled");hold ons2 = scatter(x,[16 13 19 20 17],100,"filled");s3 = scatter(x,[26 23 29 33 27],100,"filled");hold off

Specify Plot Colors- MATLAB & Simulink (19)

Change the color of the second Scatter object to a shade of purple.

s2.MarkerFaceColor = [0.7 0 1];

Specify Plot Colors- MATLAB & Simulink (20)

The scatter function also supports matrix inputs (since R2021a), so you can create the same plot by passing a matrix and returning a vector of objects.

figurex = 1:5;y = [6 3 9 10 7; 16 13 19 20 17; 26 23 29 33 27];s = scatter(x,y,100,"filled");

Specify Plot Colors- MATLAB & Simulink (21)

To change the color of the second data series in this case, access the second Scatter object by indexing into s.

s(2).MarkerFaceColor = [0.7 0 1];

Specify Plot Colors- MATLAB & Simulink (22)

See Also

Functions

  • scatter | bar | validatecolor

Properties

  • Scatter Properties | Bar Properties

Related Topics

  • Change Color Scheme Using a Colormap
  • Control Automatic Selection of Colors and Line Styles in Plots
Specify Plot Colors
- MATLAB & Simulink (2024)

FAQs

Specify Plot Colors - MATLAB & Simulink? ›

To specify colors with either approach, call the desired plotting function with an output argument so you can access the individual plot objects. Then set properties on the plot object you want to change. For example, create a scatter plot with 100-point filled markers.

How to specify line style and colours in a graph in MATLAB? ›

Specify Combinations of Colors, Line Styles, and Markers

Create a plot with a red dashed line and circular markers by specifying the linespec argument as '--or' . For this combination, '--' corresponds to a dashed line, 'o' corresponds to circular markers, and 'r' corresponds to red.

How do you change the color palette in MATLAB plot? ›

Change Colors After Plotting

Create a vector of x-coordinates and a matrix of y-coordinates. Then plot the coordinates. Change the colors of the plot by passing four hexadecimal color codes to the colororder function. You can also specify one of several named color palettes.

How do you specify a color and marker in MATLAB? ›

Specify Marker Size and Color

Customize the markers by setting these properties using name-value pair arguments with the plot function: MarkerSize - Marker size, which is specified as a positive value. MarkerEdgeColor - Marker outline color, which is specified as a color name or an RGB triplet.

How do you change the color of a line plot? ›

Setting the line color

By default, the plt. plot() function plots a blue line. However, you can change the line color by passing a color parameter to the function. The color parameter can take a string representing the color name or a hexadecimal code.

How to plot color contour in MATLAB? ›

To draw the contour lines at specific heights, specify levels as a vector of monotonically increasing values. To draw the contours at one height ( k ), specify levels as a two-element row vector [k k] . contourf(___, LineSpec ) specifies the style and color of the contour lines.

How to plot multiple graphs in MATLAB with different colors? ›

thisIndex = ceil(row/5); thisColor = plotColors(thisIndex, :); plot(thisX, thisY, '-', 'Color', thisColor, 'LineWidth', 2); hold on; % Leave plots up so we'll see all of them at the end.

How to plot random color in MATLAB? ›

Syntax. colors = rand (n, m); colororder(colors); This function will create a random matrix of n by m order of RGB values between 0 and 1. Then, this colors value is used as an argument in the function “colororder” to define color of our different plot lines.

How do I change the color of a box plot in MATLAB? ›

For a custom color, specify an RGB triplet or a hexadecimal color code. An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1] , for example, [0.4 0.6 0.7] .

How to specify color in MATLAB plot? ›

RGB Triplet — Create a custom color by specifying a three-element row vector whose elements are the intensities of the red, green, and blue components of a color. The intensities must be in the range [0,1] . For example, you can specify a shade of pink as [1 0.5 0.8] .

How to change Simulink canvas color in MATLAB? ›

To change a background color in a system, open the system. On the Format tab, select a color from the Background menu. You can select a color from the menu or click Custom Color to define your own color. To add background images to your model, use image annotations.

What is the default color in MATLAB plot? ›

Default Colors in 2D Graphs

In the past, each new plot command would start with the first color (blue) and you would have to manually change the color.

How do you specify scatter color in MATLAB? ›

scatter( x , y , sz , c ) specifies the circle colors. You can specify one color for all the circles, or you can vary the color. For example, you can plot all red circles by specifying c as "red" . scatter(___, "filled" ) fills in the circles.

How do you fill a marker with a default color in MATLAB? ›

Accepted Answer

You can use the "MarkerFaceColor" and "Color" properties of the plotted line or lines in order to fill in the markers with the same color as the default marker edge color. h = plot(x, y, 'o'); set(h, {'MarkerFaceColor'}, get(h,'Color'));

How to detect color in MATLAB? ›

In the model, double-click on the Reference Color block to open the block mask. 4. In the block mask, set the Constant value field to the vector [ 58, 68, 117 ] to detect the color purple in the image. The vector value represents the red, green, and blue components of the color for detection (e.g. [R, G, B] ).

How to plot multiple lines in MATLAB? ›

Plot Multiple Lines

Use the figure command to open a new figure window. You can plot multiple lines using the hold on command. Until you use hold off or close the window, all plots appear in the current figure window.

How to plot with different colors in gnuplot? ›

Gnuplot can change the color of a line or point based on the values of the data. As usual, there are three ways to do this: explicitly (using the rgb keyword), by indexed lookup (using an integer index), and using a gradient (with the palette keyword).

How do I change the color of one bar in a bar plot in MATLAB? ›

Control Individual Bar Colors

Set the FaceColor property of the Bar object to 'flat' so that the chart uses the colors defined in the CData property. By default, the CData property is prepopulated with a matrix of the default RGB color values. To change a particular color, change the corresponding row in the matrix.

Top Articles
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 6225

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.