How to Make a Multicolor Line in MATLAB
Learn how to plot lines in MATLAB that contain multiple colors using the patch function. This ability will allow you to vary the color of your plots to make them more visually interesting as well as more clearly illustrative of your data. For further learning on colormaps, a different colorful method of visualizing data, check out the video “How to Use Colormaps in MATLAB.”
Published: 28 Oct 2022
Hello, and welcome back to another MATLAB video. Today, we'll be talking about plotting multicolored lines in MATLAB. The ability to create multicolored lines and plots not only makes more visually interesting plots, but can also allow the user to more clearly communicate data and analysis. Before we get into how to plot a multi-colored line, let's define the relationship between multicolored lines and color maps, which may seem to be the same thing at first glance.
Multicolored lines are a single line created using a color map, which could be a default or custom color map to emphasize trends within that single line. To learn more about color maps and see more examples, check out the MATLAB color maps video, linked in the description. OK, now let's dive into how to create multicolored lines.
The key to this process is the patch function. Patch typically allows you to plot color field polygonal regions. Its inputs are x, y, and c, with c determining the polygon colors. To make a multi color line with patch, make the following adjustments. First add an entry to the end of x and y that is a NaN, or not a number.
Patch wants to make a shape. But we want to make a line. So we add the NaN to get rid of the shape's return value. This causes Patch to create a line, not a closed polygon. Then by setting the edge color property to interp, the plot interpolates the colors between vertices. So each line segment is a spectrum of color. After making these adjustments, you have successfully plotted a multi color line.
Once you have created such a line, you can adjust the aesthetics of it to enhance its significance to a viewer. For lines with data markers, you can set the marker color to flat to have it be one consistent color value, emphasizing each data point on the plot. Plus, add a color bar to the figure to clearly illustrate the range of colors in the plot and what the various colors represent.
In this video, we have discussed the off the beaten path method to plot a multicolor line in MATLAB. Now that we know the tricks of how to make multicolor lines in MATLAB plots, you can express more clarity in your data's trends and more colorful vibrancy in your plot's appearances. Thanks for watching. And I'll see you in another video.