Control Automatic Selection of Colors and Line Styles in Plots - MATLAB & Simulink (2024)

Control Automatic Selection of Colors and Line Styles in Plots

When you plot multiple data sets together in the same axes, MATLAB® helps you to distinguish your data sets by varying their appearance. For example, when you plot multiple lines, MATLAB assigns each line a color from a set of colors according to their order of creation. If you create more lines than there are colors, MATLAB repeats the cycle of colors starting with the first color. You can change this behavior:

  • Specify different sets of colors and line styles — Create your own set of colors or line styles to further distinguish your data.

  • Specify the cycling order of colors and line styles — Specify whether to cycle through all line styles before, after, or at the same time as the colors.

  • Group your data by color or line style — Group related items together visually. For example, when plotting multiple sets of scattered points, you can associate each set of points with a fit line of the same color.

This topic demonstrates how to configure line and scatter plots, but the same concepts for controlling the cycling of colors (and possibly line styles) apply to many other plots, including bar, area, and stem plots. All of the examples in this topic set the axes properties after plotting. This sequence of steps is important, because most plotting functions reset many of the axes properties.

Note

If you want to specify a color or a line style for a specific item in a plot, see Specify Plot Colors and Specify Line and Marker Appearance in Plots.

Specify Different Sets of Colors and Line Styles

The colors MATLAB selects come from the axes ColorOrder property, which contains a three-column matrix of colors specified as RGB triplets. An RGB triplet is three-element vector containing the intensities of the red, green, and blue components of a color. The intensities must be in the range [0, 1].

If you plot multiple lines, the first line uses the first color in the ColorOrder matrix, the second line uses the second color, and so on. Eventually, the colors repeat if the plot has more lines than rows in the matrix. This code creates several line plots that use the default color order. The first line is the top-most line. Because the default ColorOrder matrix has seven rows, the colors repeat after the seventh line.

plot([9 10 11 12])hold onplot([8 9 10 11])plot([7 8 9 10])plot([6 7 8 9])plot([5 6 7 8])plot([4 5 6 7])plot([3 4 5 6])plot([2 3 4 5])plot([1 2 3 4])hold offlegend("Location","northeastoutside")

Control Automatic Selection of Colors and Line Styles in Plots- MATLAB & Simulink (1)

You can change the colors in either of two ways:

  • Set the ColorOrder property of the axes to a new matrix of RGB triplets.

  • Call the colororder function. This function accepts RGB triplets, color names, such as "red", and hexadecimal color codes (since R2019b). It also accepts any of several predefined palette names, such as "gem", "reef" or "meadow" (since R2023b).

Create a new matrix containing the RGB triplets for red, green, and blue. Then set the ColorOrder property to that matrix. The plot updates immediately with the new colors.

mycolors = [1 0 0; 0 1 0; 0 0 1];ax = gca; ax.ColorOrder = mycolors;

MATLAB also cycles through different line styles in addition to colors. By default, there is only one line style (a solid line). To specify additional line styles, set the LineStyleOrder property of the axes. For example, this code specifies three line styles. The updated plot cycles through all the colors with one line style before displaying the next line style.

mylinestyles = ["-"; "--"; "-o"];ax.LineStyleOrder = mylinestyles;

Control Automatic Selection of Colors and Line Styles in Plots- MATLAB & Simulink (3)

Specify Cycling Order of Colors and Line Styles

Since R2023a

When you use multiple colors and line styles, you can specify whether the plot cycles through all line styles before, after, or at the same time as the colors by setting the LineStyleCyclingMethod property. This property can have one of three values:

  • "aftercolor" — Cycle through the line styles after the colors. This value is the default.

  • "beforecolor" — Cycle through the line styles before the colors.

  • "withcolor" — Cycle through the line styles with the colors.

Plot four lines. Set the LineStyleOrder property to three line styles, and set the ColorOrder property to three colors by passing an array of three hexadecimal color codes to the colororder function. Then add a legend.

% Plot four linesplot([4 5 6 7])hold onplot([3 4 5 6])plot([2 3 4 5])plot([1 2 3 4])hold off% Set the line style order and color orderax = gca;ax.LineStyleOrder = ["-"; "--"; "-o"];colororder(["#8040E6";"#1AA640";"#E68000"])legend("Location","northeastoutside")

Control Automatic Selection of Colors and Line Styles in Plots- MATLAB & Simulink (4)

By default, the plot cycles through all the colors with the first (solid) line style before it displays the next (dashed) line style. Because there are three colors, only two of the line styles are used in the four-line plot.

If you want more of your lines to be distinct in both color and line style, use the "withcolor" option and specify an equal number of colors and line styles. For example, change the LineStyleCylingMethod of the preceding plot to "withcolor". The updated plot cycles through the colors and the line styles at the same time.

ax.LineStyleCyclingMethod = "withcolor";

Control Automatic Selection of Colors and Line Styles in Plots- MATLAB & Simulink (5)

Group Your Data by Color or Line Style

Since R2020a

To group related lines (or other plot objects) together visually, set the SeriesIndex property of each object that you want to receive the same visual treatment to the same number. The SeriesIndex property indexes into the ColorOrder and LineStyleOrder arrays according to the value of the LineStyleCyclingMethod property. Thus, different objects with the same SeriesIndex value use the same color (and line style, if applicable).

For example, plot two sets of 50 scattered points.

x = 1:50;meas1 = 0.25*x + randn(1,50);scat1 = scatter(x,meas1);hold onmeas2 = 0.5*x + randn(1,50) + 5;scat2 = scatter(x,meas2);

Control Automatic Selection of Colors and Line Styles in Plots- MATLAB & Simulink (6)

Calculate a fit line for each set of points using the polyfit and polyval functions. Then add each fit line to the plot.

% Calculate fit lines for each set of measurementsp1 = polyfit(x,meas1,1);y1_fit = polyval(p1,x);p2 = polyfit(x,meas2,1);y2_fit = polyval(p2,x);% Plot fit linesfitline1 = plot(x,y1_fit);fitline2 = plot(x,y2_fit);hold off

Control Automatic Selection of Colors and Line Styles in Plots- MATLAB & Simulink (7)

The scatter plots and fit lines are all different colors. The reason is that MATLAB selects a new color from the ColorOrder matrix for each new plot you add to the axes.

Match the color of each fit line with its associated scatter plot. Set the SeriesIndex property of each line to the same value that the associated scatter plot has.

fitline1.SeriesIndex = scat1.SeriesIndex;fitline2.SeriesIndex = scat2.SeriesIndex;

Control Automatic Selection of Colors and Line Styles in Plots- MATLAB & Simulink (8)

This relationship persists even if you change the colors in the ColorOrder matrix. For example, change the colors to purple and green by calling the colororder function.

colororder([0.5 0.25 0.90; 0.10 0.65 0.25])

Control Automatic Selection of Colors and Line Styles in Plots- MATLAB & Simulink (9)

See Also

Functions

  • plot | scatter | gca | colororder | validatecolor

Properties

  • Axes Properties | Line Properties | Scatter Properties

Related Topics

  • Specify Plot Colors
  • Specify Line and Marker Appearance in Plots

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Control Automatic Selection of Colors and Line Styles in Plots- MATLAB & Simulink (10)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Control Automatic Selection of Colors and Line Styles in Plots
- MATLAB & Simulink (2024)
Top Articles
myGwork ¦ LGBTQ+ Jobs ¦ Pharmacist - Sign On Bonus Available 🏳️‍🌈: ROCKY MOUNT, NC
Sara Lauer on LinkedIn: #fathersday #faycoenterprises
Ucsf Ilab
Which Universal Life Option Has A Gradually
Oklahoma Dam Generation Schedule
Haunted Mansion Showtimes Near Amc Classic Marion 12
Tinyzonehd
T-Mobile SW 56th Street & SW 137th Ave | Miami, FL
Craigslist Folkston Ga
Does Publix Pharmacy Accept Sunshine Health
Edward Scissorhands 123Movies
Grand Rapids Herald-Review Obituaries
Honda Accord 2012 gebraucht - AutoUncle
Members Mark Ham Cooking Instructions Recipes with ingredients,nutritions,instructions and related recipes
Keci News
Ktbs Payroll Login
Five Guys Calorie Calculator
Loceryl NAIL LACQUER
Acuity Eye Group - La Quinta Photos
Rosekellyppv
Food King El Paso Ads
Craigslist Quad Cities
Tyrone's Unblocked Games Basketball
Education (ED) | Pace University New York
Hose Woe Crossword Clue
Logisticare Transportation Provider Login
Mmastreams.com
Operation Carpe Noctem
Lily Spa Roanoke Rapids Reviews
Fototour verlassener Fliegerhorst Schönwald [Lost Place Brandenburg]
On-Campus Student Employment
The Legend of Maula Jatt | Rotten Tomatoes
Directions To Truist Bank Near Me
Managementassistent directie Wonen
Busty Bruce Lee
Arsenal’s Auston Trusty: Inspired by Ronaldinho, World Cup dreams and Birmingham loan
Theatervoorstellingen in Roosendaal, het complete aanbod.
Craigslist For Port Huron Michigan
Business Banking Online | Huntington
Thotsbay New Site
Indium Mod Fabric
Htmp Hilton
Showbiz Waxahachie Bowling Hours
10439 Gliding Eagle Way Land O Lakes Fl 34638
Csulb Atlas
Craigslist For Pets For Sale
Bonbast قیمت ارز
Houses For Sale 180 000
Equine Trail Sports
Jersey Mike's Subs: 16 Facts About The Sandwich Chain - The Daily Meal
13364 Nw 42Nd Street
Navy Qrs Supervisor Answers
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 5770

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.