How To Change Color Of Graph In Matlab

In MATLAB, altering the color of your graph elements is fundamental for enhanced data visualization and clearer communication of results. Whether you're presenting findings to colleagues, publishing research, or simply analyzing data for your own understanding, color plays a critical role in highlighting trends, distinguishing datasets, and conveying meaning. The following describes several approaches to changing the color of lines, bars, and other graphical objects within MATLAB.
Changing Line Colors
The simplest way to change the color of a line in a MATLAB plot is directly within the plot function itself.
If you use: plot(x, y), you'll get the default line color.
Must Read
Instead, specify a color using a character code:
plot(x, y, 'r') % Creates a red line
plot(x, y, 'g') % Creates a green line
plot(x, y, 'b') % Creates a blue line
plot(x, y, 'k') % Creates a black line
Or use a three-element RGB vector:
plot(x, y, 'Color', [0.5 0 0.5]) % Creates a purple line
plot(x, y, 'Color', [0 0.7 0.9]) % Creates a cyan-like blue line
You can also use hexadecimal color codes:
plot(x, y, 'Color', '#A2142F')
To modify the color of an existing line, first obtain its handle. The plot function returns the handle of the line object.
h = plot(x, y);
set(h, 'Color', 'm'); % Changes the line to magenta
Or, using dot notation (more modern approach):

h = plot(x, y);
h.Color = 'c'; % Changes the line to cyan
Practical Tip: Consistent Color Schemes
Develop a consistent color scheme for your plots, especially when presenting data professionally. This makes it easier for your audience to understand and compare different graphs. Consider using online tools like Adobe Color or Coolors to generate harmonious color palettes.
Changing Bar Colors
For bar plots, the approach is similar to line plots. Use the bar function to create a bar plot.
bar(x, y) % Creates a bar plot with default colors
To change the bar color, you can set the FaceColor property:
bar(x, y, 'FaceColor', 'r'); % Creates red bars
You can also use RGB vectors or hexadecimal codes for the FaceColor:
bar(x, y, 'FaceColor', [0.2 0.8 0.2]); % Creates light green bars
To change the color of an existing bar plot, get its handle and modify the FaceColor property:

h = bar(x, y);
set(h, 'FaceColor', 'y'); % Changes the bars to yellow
Or, with dot notation:
h = bar(x, y);
h.FaceColor = 'k'; %Change the bars to black
The edge color can be modified too, using EdgeColor property.
h = bar(x, y);
h.EdgeColor = 'blue'; %Change the edges to blue
Practical Tip: Highlighting Specific Bars
In a bar plot, you might want to highlight specific bars to draw attention to them. Iterate over the handles of the bars and change their colors individually. This technique is helpful when illustrating significant differences or outliers in your data. You will probably need to use loops.
Changing Colors in Scatter Plots
For scatter plots, you can control the color of the markers using the scatter function.
scatter(x, y) % Creates a scatter plot with default colors
To change the marker color, use the 'MarkerFaceColor' property:

scatter(x, y, 'MarkerFaceColor', 'b'); % Creates blue markers
You can also use RGB vectors or hexadecimal codes:
scatter(x, y, 'MarkerFaceColor', [1 0.5 0]); % Creates orange markers
The 'MarkerEdgeColor' property allows changing the color of the marker's border.
scatter(x, y, 'MarkerEdgeColor', 'red');
For more complex scenarios where each data point has a different color, you can pass a vector of color values to the scatter function's third argument.
c = rand(size(x)); % Creates a vector of random color values
scatter(x, y, [], c); % Creates a scatter plot with varying marker colors
colorbar; % Adds a colorbar to show the color mapping
The colorbar function is crucial here, as it displays the mapping between the color values and the actual colors used in the scatter plot.
Practical Tip: Representing Data Dimensions with Color
Use color to represent an additional dimension of your data in a scatter plot. For instance, if you have data with X, Y coordinates and a corresponding value for each point, use the color of the markers to visually represent the magnitude of that value. This technique can reveal patterns and correlations that might not be immediately apparent from the X and Y coordinates alone.

Changing Colors of Multiple Plots on Same Axes
When you have multiple plots on the same axes, it is important to differentiate them clearly using distinct colors. MATLAB automatically cycles through a default set of colors, but you can override this behavior using the methods previously mentioned.
plot(x1, y1, 'r', x2, y2, 'b', x3, y3, 'g'); % Each plot has a different color
To have greater control and flexibility, plot each dataset separately and assign colors programmatically.
plot(x1, y1, 'Color', [0.1 0.4 0.7]);
hold on; % Prevents the next plot from clearing the current axes
plot(x2, y2, 'Color', [0.8 0.2 0.2]);
plot(x3, y3, 'Color', [0.3 0.7 0.3]);
hold off;
Practical Tip: Clear Color Contrast
Ensure that the colors you choose for multiple plots on the same axes have sufficient contrast, especially for readers who may have color vision deficiencies. Avoid using similar shades of colors next to each other. Use a colorblindness simulator or consult color schemes designed for accessibility.
Application in Daily Life/Work
The ability to change graph colors in MATLAB has wide-ranging applications in various fields. In engineering, it helps visualize sensor data, highlighting anomalies or critical thresholds. In finance, it aids in presenting market trends and portfolio performance. In scientific research, it is essential for illustrating experimental results and comparing different datasets. Furthermore, even in everyday tasks such as budgeting or tracking personal fitness goals, clear and visually appealing graphs with customized colors can make data more understandable and actionable.
Checklist: Changing Graph Colors in MATLAB
- Identify the graphical object: Determine if you're working with a line plot, bar plot, scatter plot, or another type of plot.
- Obtain the handle: If the plot already exists, get its handle using the appropriate function (e.g.,
plot,bar,scatter). - Set the color property: Use the appropriate property (e.g.,
Color,FaceColor,MarkerFaceColor) to set the desired color. - Choose a color representation: Select a color using a character code (e.g., 'r', 'g', 'b'), an RGB vector (e.g., [0.5 0 0.5]), or a hexadecimal code (e.g., '#A2142F').
- Consider accessibility: Ensure that your color choices provide sufficient contrast and are accessible to individuals with color vision deficiencies.
- Maintain consistency: Use a consistent color scheme across multiple plots for better readability and professional presentation.
