Open In App

Automatically Plot Different Color Lines in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB handles the colors of plots on its own as it has the functionality to do so. What this means is that when a user plots multiple lines in a plot, MATLAB gives each line a different color based on some calculations that are made by MATLAB itself. However, if one wishes to change those colors, MATLAB provides that option as well. In this article, we shall see with an example how MATLAB plots different color lines on its own and how we can change the same automatically. 

MATLAB Controlled Different Color Lines:

Let us plot multiple sin and cos curves to see how MATLAB handles the colors of these curves.

Example 1:

Matlab




% MATLAB code for plot
% X-coordinates
x = linspace(-1,1.5);
y1 = sin(x);
y2 = cos(x);
  
figure
plot(x,y1,x,y2)
hold on
plot(sin(x))
plot(sin(x*2))
plot(cos(x))
plot(cos(x*2))
hold off


Output:

 

The ‘hold on’ parameter is used to plot the different curves in same plot and the LineWidth parameter in plot commands widens the curves for better visibility of curves. In the above code, we are plotting 4 curves

  • sin(x)
  • sin(x2)
  • cos(x)
  • cos(x2)

As it can be seen that MATLAB handled the color mapping for these curves without any errors. This is true for as many curves as there are possible RGB color combinations. Now we will see how we can set MATLAB to automatically plot different color lines based by taking the colororder from a user-defined code.

Automatically Plot Different Color Lines:

We will use the colormap property of MATLAB axes to plot different colors using uniformly distributed random numbers. For this, we need to create an n-by-3 array of random numbers in range [0,1] as this is the RGB value range. Then we shall pass this as argument to colororder() for defining colors of our n plots.

Syntax:

colors = rand([n 3])

colororder(colors)

Example 2:

Matlab




% MATLAB code for plot different colour line 
% x-data 
x=linspace(-1,1);    
  
% Random color combinations
colors = rand([100 3]);
  
% Adding colors to color order
colororder(colors)
  
% Plotting plots with new colororder
hold on
plot(sin(x),LineWidth=3)
plot(sin(x*2),LineWidth=3)
plot(cos(x),LineWidth=3)
plot(cos(x*2),LineWidth=3)
hold off
  
% Adding legend
legend


Output:

 

Explanation:

Here, the colors array is an array of random numbers in the range of 0 – 1. The three columns in the array define the R, G, and B color code values for a row, which is used as the color for one plot line. 

Note: This method of generating colors will always result in different colors for plots as the RGB color value is defined by random numbers, which are random. This randomness of this method makes the choice of colors for plot lines automated by MATLAB. The benefit of this method is that every time the above code is run, it will produce a new color combination for our plot lines as the colororder is defined by random numbers.



Last Updated : 22 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads