Just Learn Code

Mastering MATLAB’s Plotting Techniques: Line Styles Markers and Colors

Plotting and visualizing data are crucial tasks in data analysis, and MATLAB provides a variety of tools to help users create meaningful and informative plots. In this article, we will be discussing some of the ways to differentiate plots using line styles, markers, and colors, as well as how to plot multiple sine waves in MATLAB.

Using Line Styles, Markers, and Colors to Differentiate Plots in MATLAB

When plotting multiple lines on the same graph, it is crucial to ensure that each line can be easily differentiated from the others. One way to achieve this is by using different line styles.

MATLAB provides four primary line styles: solid, dashed, dotted, and dash-dot. To use a specific line style, simply add the name of the style as an argument to the plot function.

For example:

“`

x = 1:10;

y1 = sin(x);

y2 = cos(x);

plot(x, y1, ‘–‘, x, y2, ‘:’)

“`

In this example, we use a dashed line style for `y1` and a dotted line style for `y2`. We can also change the width of the lines using the `LineWidth` property.

For example:

“`

plot(x, y1, ‘–‘, ‘LineWidth’, 2)

“`

The above command will plot `y1` with a dashed line style and a width of 2. Markers can also be used to distinguish between different lines.

MATLAB provides various marker types, such as circle, asterisk, plus sign, and diamond. To specify a marker, add the marker type as an argument to the plot function.

For example:

“`

plot(x, y1, ‘o’, x, y2, ‘^’)

“`

Here, we use a circle marker for `y1` and a triangle pointing upwards marker for `y2`. We can also change the properties of markers, such as the edge color, face color, and size.

For example:

“`

plot(x, y1, ‘o’, ‘MarkerEdgeColor’, ‘red’, ‘MarkerFaceColor’, ‘green’,… ‘MarkerSize’, 10)

“`

This command will plot `y1` with a circle marker, a red edge color, a green face color, and a size of 10.

Combining line styles and markers can also help differentiate plots further. For example:

“`

plot(x, y1, ‘-o’, x, y2, ‘–^’)

“`

Here, `y1` is plotted with a solid line style and circle markers, while `y2` is plotted with a dashed line style and triangle pointing upwards markers.

Finally, changing line colors is another way to differentiate between lines. MATLAB supports various line colors, including red, blue, green, and yellow.

To specify a line color, simply add the color name as an argument to the plot function. For example:

“`

plot(x, y1, ‘r’, x, y2, ‘g’)

“`

Here, `y1` is plotted with a red line color, while `y2` is plotted with a green line color.

Plotting Multiple Sine Waves in MATLAB

In many cases, we might need to plot multiple sine waves on the same graph. One way to achieve this is by using different phases for each sine wave.

To do this, we first create an array of phases and then use them to generate the sine waves. For example:

“`

phases = [0, pi/4, pi/2];

x = 1:10;

for i = 1:length(phases)

y(:, i) = sin(x + phases(i));

end

plot(x, y)

“`

Here, we create an array of three phases and then loop through them to generate three sine waves. We then plot all three sine waves on the same graph using the `plot` function.

We can differentiate between the sine waves using the techniques discussed in the previous section. For example, we can use a solid line style for the first sine wave, a dashed line style for the second sine wave, and a dotted line style for the third sine wave:

“`

plot(x, y(:,1), ‘-‘, x, y(:,2), ‘–‘, x, y(:,3), ‘:’)

“`

We can also differentiate between the sine waves using markers:

“`

plot(x, y(:,1), ‘o’, x, y(:,2), ‘^’, x, y(:,3), ‘s’)

“`

We can further differentiate between the sine waves using colors:

“`

plot(x, y(:,1), ‘r’, x, y(:,2), ‘g’, x, y(:,3), ‘b’)

“`

Adding labels to plots using the `leg

end` function also helps to make the plots more informative and easy to understand. For example:

“`

plot(x, y(:,1), ‘r’, x, y(:,2), ‘g’, x, y(:,3), ‘b’)

leg

end(‘Phase = 0’, ‘Phase = pi/4’, ‘Phase = pi/2’)

“`

Here, we use the `leg

end` function to add labels to each sine wave, making it easier for the reader to understand the plot.

Conclusion

In this article, we have discussed various ways to differentiate plots in MATLAB using line styles, markers, and colors. We have also demonstrated how to plot multiple sine waves on the same graph, differentiate them using line styles, markers, and colors, and add labels using the `leg

end` function. By using these techniques, we can create informative and easy-to-understand plots that effectively communicate our data.

3) Changing Line Width in MATLAB

When creating plots in MATLAB, changing the line width can be an effective way to enhance the visual impact of the graph. By adjusting the line width, we can highlight important data points and emphasize certain tr

ends. To change the line width in MATLAB, we can use the `LineWidth` property.

The syntax for changing the line width is as follows:

“`

plot(x, y, ‘LineWidth’, 2)

“`

In this example, we use a line width of 2 for the plot. Another way to change the line width is through the `set` function.

We can change the line width of a plot object using the following command:

“`

set(plot_object, ‘LineWidth’, 3)

“`

In this example, we use a line width of 3 for the plot object. We can also change the line width for specific lines in a plot.

For example:

“`

x = 1:10;

y1 = sin(x);

y2 = cos(x);

h = plot(x, y1, x, y2);

set(h(1), ‘LineWidth’, 2)

“`

Here, we plot `y1` and `y2` and then adjust the line width of `y1` to 2.

Examples of Changing Line Width in MATLAB

Let’s take a look at some examples to see how changing the line width can enhance a plot. Example 1: Line Graph

Suppose we have sales data for a company over the past year.

We can use a line graph to visualize this data, with x and y-axes representing time and sales, respectively. “`

x = 1:12;

y = randi([500 1000], [1 12]);

plot(x, y);

title(‘Monthly Sales’);

xlabel(‘Month’);

ylabel(‘Sales ($)’);

“`

The above code generates a simple line graph with default line width.

We can enhance this plot by increasing the line width to, say, 2, to make the graph more readable and aesthetically pleasing. “`

plot(x, y, ‘LineWidth’, 2);

title(‘Monthly Sales’);

xlabel(‘Month’);

ylabel(‘Sales ($)’);

“`

Here, we have increased the line width to 2, creating a more prominent effect on the graph.

Example 2: Scatter Plot

Suppose we have data on the relationship between temperature and humidity for a given area. We can use a scatter plot to show how temperature and humidity interact.

“`

temp = [68 72 78 82 87 91 94];

humidity = [60 56 48 40 30 20 15];

scatter(temp, humidity);

title(‘Temperature-Humidity Relationship’);

xlabel(‘Temperature (F)’);

ylabel(‘Humidity (%)’);

“`

The above code generates a scatter plot with default line width. We can increase the line width of the scatter plot to 1.5 to make it more visually appealing.

“`

scatter(temp, humidity, ‘LineWidth’, 1.5);

title(‘Temperature-Humidity Relationship’);

xlabel(‘Temperature (F)’);

ylabel(‘Humidity (%)’);

“`

Here, we increase the line width to 1.5, making the scatter plot points bolder and easier to see.

4) Combining Line Styles and Markers in MATLAB

In addition to changing line widths, combining line styles and markers can also enhance the visual appeal of MATLAB plots. In some cases, combining these two elements can help us to better illustrate our data and convey important information.

To combine line styles and markers, we can use the same syntax as we did when working with them separately. For example, we can create a plot that combines a solid line style with circle markers like so:

“`

x = 1:10;

y = sin(x);

plot(x, y, ‘-o’);

“`

In this example, we use a combination of `-` and `o` to create a solid line style with circle markers.

We can also adjust marker properties such as color and size, and line properties such as color and width, as we discussed earlier.

Examples of Combining Line Styles and Markers in MATLAB

Let’s take a look at some examples of how to combine line styles and markers in MATLAB. Example 1: Multiple Data Series

Suppose we have three sets of data that represent the estimated sales growth for three different product lines.

We can use a line graph to plot these data series, and differentiate each line using a different line style and marker combination. “`

x = 1:12;

y1 = randi([500 1000], [1 12]);

y2 = randi([400 900], [1 12]);

y3 = randi([350 800], [1 12]);

plot(x, y1, ‘-o’, x, y2, ‘:s’, x, y3, ‘–^’);

leg

end(‘Product 1’, ‘Product 2’, ‘Product 3’);

title(‘Estimated Sales Growth’);

xlabel(‘Month’);

ylabel(‘Sales ($)’);

“`

In this example, we use a solid line with a circle marker for `y1`, a dotted line with a square marker for `y2`, and a dashed line with a triangle pointing upwards marker for `y3`. Adding a leg

end helps to differentiate between the three data series. Example 2: Scatter Plot

Suppose we have data on the number of hours students sp

end studying per week against their grades. We can use a scatter plot to show the relationship between these two variables, and differentiate between different groups using different markers.

“`

hours = [10 12 8 9 6 7 4 3 1];

grades = [70 80 63 71 51 64 47 38 23];

group = [1 2 1 1 2 2 1 2 1];

colors = {‘r’, ‘b’};

markers = {‘o’, ‘s’};

for i = 1:max(group)

idx = (group == i);

scatter(hours(idx), grades(idx), ‘+’, … colors{i}, markers{i}, ‘LineWidth’, 2);

hold on;

end

title(‘Study Hours vs Grades’);

xlabel(‘Study Hours (hrs/wk)’);

ylabel(‘Grades (out of 100)’);

leg

end(‘Group 1’, ‘Group 2’);

“`

In this example, we use a red circle marker for group 1 and a blue square marker for group 2. Combining the markers with colors helps us differentiate between the two groups in the scatter plot.

Conclusion

When creating plots in MATLAB, changing the line width and combining line styles and markers can be effective ways to enhance the visual appeal of the graph and highlight important data points. By incorporating these techniques in our MATLAB code, we can create more compelling and informative plots that effectively communicate our data.

5) Changing Marker Properties in MATLAB

Markers can be a useful tool when creating plots in MATLAB. They can be used to highlight certain data points, distinguish between different data sets, and add visual interest to a graph.

In MATLAB, there are several marker properties that we can adjust to create the desired effect.

Changing Marker Edge Color in MATLAB

One way to customize the appearance of markers in MATLAB is by changing the color of their edges. We can use the `MarkerEdgeColor` property to do this.

The syntax for changing the marker edge color is as follows:

“`

plot(x, y, ‘o’, ‘MarkerEdgeColor’, ‘red’);

“`

In this example, we use a red color for the marker edges. We can also change the marker edge color using the `set` function.

For example:

“`

set(plot_object, ‘MarkerEdgeColor’, ‘blue’);

“`

In this example, we change the marker edge color of a specific plot object to blue.

Changing Marker Face Color in MATLAB

In addition to changing the edge color of markers, we can also change the color of their faces. This can help make the markers more visually appealing and better differentiate between data sets.

We can use the `MarkerFaceColor` property to do this. The syntax for changing the marker face color is as follows:

“`

plot(x, y, ‘o’, ‘MarkerFaceColor’, ‘green’);

“`

In this example, we use a green color for the marker faces.

We can also change the marker face color using the `set` function. For example:

“`

set(plot_object, ‘MarkerFaceColor’, ‘yellow’);

“`

In this example, we change the marker face color of a specific plot object to yellow.

Changing Marker Size in MATLAB

Another way to customize the appearance of markers in MATLAB is by changing their size. We can use the `MarkerSize` property to do this.

The syntax for changing the marker size is as follows:

“`

plot(x, y, ‘o’, ‘MarkerSize’, 10);

“`

In this example, we use a marker size of 10. We can also change the marker size using the `set` function.

For example:

“`

set(plot_object, ‘MarkerSize’, 15);

“`

In this example, we change the marker size of a specific plot object to 15.

Examples of Changing Marker Properties in MATLAB

Let’s take a look at some examples to see how changing marker properties can enhance a plot. Example 1: Scatter Plot

Suppose we have data on the relationship between population and GDP for several countries.

We can use a scatter plot to visualize this relationship, with the size and color of the markers representing different continents. “`

pop = [1439 1373 327 1393 126 65 207];

gdp = [2197 1531 677 345 487 51 131];

continents = {‘Asia’, ‘Asia’, ‘Europe’, ‘Asia’, ‘Europe’, ‘Africa’, ‘Africa’};

colors = {‘red’, ‘red’, ‘blue’, ‘red’, ‘blue’, ‘green’, ‘green’};

sizes = [200, 150, 100, 150, 100, 50, 50];

for i = 1:length(pop)

scatter(pop(i), gdp(i), sizes(i), colors{i}, ‘filled’);

hold on;

end

title(‘Population-GDP Relationship’);

xlabel(‘Population (millions)’);

ylabel(‘GDP (billions)’);

leg

end(‘Asia’, ‘Europe’, ‘Africa’);

“`

In this example, we use different marker sizes and colors to differentiate between the continents represented in our data set. We use a red color and larger size for Asia, a blue color and medium size for Europe, and a green color and smaller size for Africa.

Example 2: Line Graph

Suppose we have data on the daily high and low temperature at a given location, over the course of a week

Popular Posts