
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Automatically Plot Different Color Lines in MATLAB
The different color lines in a plot allows us to plot and distinguish multiple data sets on a same plot area. MATLAB provides various built-in functions to assign different colors to different plot lines automatically. To automatically control the color of plot lines, we can use the following two MATLAB functions:
Automatically Plot Different Color Lines using Hold Option
In MATLAB programming, we can use the "hold" option to automatically plot different color lines.
Syntax
hold on;
The following MATLAB program explain the use of "hold" function to plot lines of different color.
Example
% MATLAB program to demonstrate automatically plot different color lines using "hold" option. % Create a sample vector of data x = 0:1:360; % Angle in degrees y = sind(x); % sine values z = cosd(x); % cosine values figure; % Plot the data sets plot(x, y, x, z); % Make the current plot active by using "hold on" command hold on; plot (sind(x)); plot (cosd(x)); % Add a legend legend("Location", "north");
Output
Explanation
In this MATLAB program, we start by creating data sets "x", "y", and "z". After that we use the "plot" function to plot the graph. Next, we use the "hold on" command to keep the current plot active and allow subsequent plots to be overlaid, which ensures that each plot line will be plotted in a different color. Finally, we call the "legend" function to add a legend to provide information about different colored lines.
Automatically Plot Different Color Lines using "rand" Function
MATLAB has the "rand" function that can be used to generate a random color order and set it to different plot lines.
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.
The following MATLAB program demonstrates the use of rand function to automatically plot different color lines.
Example
% MATLAB program to demonstrate automatically plot different color lines using "rand" function. % Create a random 5 x 3 matrix of RGB values ranging between 0 and 1. colors = rand(5, 3); % Set the color order for plots colororder(colors); % Create a sample data to plot x = 0:1:360; y1 = sind(x); y2 = cosd(x); % Plot x, y1, and y2 using the plot function figure; plot(x, y1, x, y2); % Add a legend legend;
Output
Conclusion
In this MATLAB program, we first use the "rand" function to create a random 5 x 3 matrix of RGB values ranging between 0 and 1 and stores in a variable "colors". In this random RGB matrix, each row specifies a different color. Then, we call the "colororder" function with "colors" as the argument to define the color order of the plots.
After that we write the plot code and call the "plot" function to plot two lines of different colors taken from the above created color order. Finally, we call the "legend" function to add a legend.
This is how we can automatically plot different color lines in MATLAB.