Coding Ref

How to plot colors in MATLAB

How to plot colors in MATLAB

In MATLAB, the plot function is used to create a graph of data. One of the optional arguments to the plot function is the Color` property, which specifies the color of the plotted data. The color can be specified using a string representing a color name (e.g. 'red', 'green', etc.), a string representing a color code (e.g. '#FF0000' for red), or a three-element RGB vector (e.g. [1 0 0] for red).

Here are some examples of how to use the Color property to specify the color of a plotted data series:

% Create a vector of x values
x = 0:0.1:10;

% Plot a sine wave in red
plot(x, sin(x), 'Color', 'red')

% Plot a cosine wave in green
plot(x, cos(x), 'Color', 'green')

% Plot a tangent wave in blue
plot(x, tan(x), 'Color', 'blue')

% Plot an exponential wave in magenta (using a color code)
plot(x, exp(x), 'Color', '#FF00FF')

% Plot a logarithmic wave in yellow (using an RGB vector)
plot(x, log(x), 'Color', [1 1 0])

In addition to specifying the color of a plotted data series, you can also use the colormap function to create a custom color map for a figure. A color map is a set of colors that are used to represent the data in a plot. The colormap function takes a matrix of RGB values as an argument, and it sets the color map for the current figure to the specified colors.

Here is an example that creates a custom color map and applies it to a figure:

% Create a matrix of RGB values for a custom color map
colormap_data = [0 0 1;   % blue                 0 1 0;   % green                 1 0 0];  % red

% Set the colormap for the current figure
colormap(colormap_data)

% Create a vector of x values
x = 0:0.1:10;

% Plot a sine wave
plot(x, sin(x))

% Add a colorbar to the figure
colorbar

Conclusion

In MATLAB, the plot function is used to create a graph of data. One of the optional arguments to the plot function is the Color` property, which specifies the color of the plotted data. The color can be specified using a string representing a color name (e.g. 'red', 'green', etc.), a string representing a color code (e.g. '#FF0000' for red), or a three-element RGB vector (e.g. [1 0 0] for red).

You'll also like

Related tutorials curated for you

    Plot markers in MATLAB

    How to plot a line in MATLAB

    How to create a 3D plot in MATLAB

    How to create a surface plot in MATLAB

    How to plot a legend in MATLAB

    MATLAB linspace

    How to plot points in MATLAB

    MATLAB subplot

    How to create a scatter plot in MATLAB

    How to plot colors in MATLAB

    How to plot a Bode in MATLAB