In today’s lesson, we will discuss the following topics:
1. Adding Line Styles to Graphs, including Colors and Underlines
2. Adding Legends to Graphs
3. Adding Subscripts and Superscripts on Variables
4. Adding Accents on Variables
VIDEO COMING SOON!
Adding Line Styles to Graphs, including Colors and Underlines
MATLAB allows us to create different styles, colors, and types of lines when they are graphed. Here are common things we can specify on graphs:
(a) Line Width – To specify line width, add on ‘LineWidth’ at the end of the plotted variables in the brackets (eg. after x,y, use ‘LineWidth’) followed by a comma and the number of thickness you’d like. The default plot line is a width of about 0.5. Here I have made thicknesses of 1 and 4:
The graph looked like this:
(b) Line Colors – Here are the letters used for different colors:
Here is an example below – I have two vectors, x1 and y1 which I plot together. I add the letter for the color red which is ‘r’ from the table. You need to make sure it is in single quotes so the letter turns purple.
This is the graph it outputs – rather than being blue (the default color), it is now red.
(c) Underlines – Here are commands for different underlining styles:
(d) Marker Symbols and Line Style – Here are the characters for specifying different marker symbols and line styles. These will be what your line is made up of (eg. solid line, dashed line, line made of circles, line made of plus signs, etc).
Here is an example code, where I use the ‘–‘ dashed lines to indicate that I want a dashed line in the graph. It has to be in single quotes so that the dashes turn purple:
(e) Marker Edge Color and Face Color – This is to specify the color on the edge or the inside of the markers (eg. if you use circles to make up the line, you can fill in the middle or the edges with different colors). Use the words ‘MarkerEdgeColor’ and ‘MarkerFaceColor’, followed by a comma and the name of a color from the list of colors earlier in this post, to specify these. For example I have specified both the marker face and edge color here – I used ‘r’ after the Face Color and ‘g’ after the Edge Color, to indicate that the circles making up the line are filled with red but lined with green:
(g) Marker Size – When making marker specifiers, such as circles, they can be changed to larger or smaller circles (or whichever shape). We use the words ‘MarkerSize’ followed by the number (aka the size) wanted. Take a look at this example, where I used circle sizes of 4 and 7, so the green circles are larger than the black ones:
Adding Legends to Graphs
Legends are useful to add to graphs when there is more than one plot within the same graph. This way, it can be specified which line on the graph belongs to which plot.
Let’s take a look at an example where we have two lines in the same graph. Here I have two vectors, x1 and y1. I plot x1 with y1, then plot the same x1 with an equation y2.
To create legends, we use the ‘legend’ command. This is just the word “legend” before the titles you’d like to name your graphs, with single quotes around them (the words in the brackets will then turn purple as you can see in the last line of the code above). The order you name the titles in will attach to the graphs in the order you plot the graphs in.
For example, I plotted (x,y) first in the code above, so the first title within the brackets after ‘legend’ will be the title coresponding to the graph of (x,y):
By plotting them on the same graph, MATLAB plots them in two different colors, then uses the legend to specify which graph belongs to which color. The code above gives this graph as the output. Notice how the graph of x1 vs y1 is labeled as blue, and x1 vs y2 is labeled as red.
Adding Subscripts and Superscripts on Variables
Superscripts and subscripts in MATLAB can be useful to add to specify variables between one another, especially in graphs. Accents can be used to make letters not available directly on an English keyboard. These mainly come into play when writing axis labels or titles. To specify these in MATLAB, we need to do the following:
(a) Subscripts: Use underscores between the main variable and the subscript. This is what it would look like if these variables were used in a label or title:
(b) Superscripts: Use two underscores between the main variable and the superscript. This is what it would look like if these variables were used in a label or title:
(c) Both Subscript and Superscript: To add both a subscript and a superscript to a variable, use both one and two underscores, as explained in parts (a) and (b). Take a look at this example:
Adding Accents on Variables
Below is a list of common accents:
To add accents to a variable (and this works when you are trying to show the accents as a plot label or title), do the following steps (it isn’t very straight forward on the MATLAB website, but doing it this way works)
xlabel(sprintf(‘$\\bar{%c}$’, ‘x’), ‘Interpreter’, ‘LaTeX’);
ylabel(sprintf(‘$\\bar{%c}$’, ‘x’), ‘Interpreter’, ‘LaTeX’);
title(sprintf(‘$\\bar{%c}$’, ‘x’), ‘Interpreter’, ‘LaTeX’);
Use this exact same syntax, but change some of the code depending on what you need:
(a) The variable you want the accent to be on has to be at the end of the first brackets, so replace ‘x’ with your variable’s letter or letters
(b) Then instead of the word ‘bar’ before that, add in any other word from the list of accents above, to change the type of accent you make
On the outside of the brackets, you can choose to have ‘title’ ‘xlabel’ or ‘ylabel’ depending where you want to place the accent. The rest of the wording in the brackets must remain the same.
Let’s take a look at an example. Below I have created simple vectors x and y and plotted them. I created a title using the same syntax as above, but put ‘b’ instead of ‘x’.
Now in the output, this is the graph that comes up. You will notice the title has b with a bar on top (I circled it in black):
*References from mathworks.com