Simple 2-D Plot Manipulation (Plotting in Two Dimensions) (MATLAB) Part 2

Creating Supporting Text and Legends

In the last section of this topic we will explore some of the new interactive ways MATLAB lets us edit our plots. You will see just how quick and easy it is to edit plots in the plot editing mode. Although convenient for quick edits, it is still necessary to understand how to add text to your plots using the specific text commands. You have already seen how to add text to the x-axis, y-axis, and at the top for a title using xlabel, ylabel, and title. Although these are sure to be the most common commands you will use when creating plots, MATLAB provides you with additional means for adding supporting text in any arbitrary position to your graph using the text command. We will also show you how you can place text with your mouse using gtext. Finally you will learn how the legend command can quickly add a legend to your multi-line plots.

Before we begin with the built-in functions of MATLAB, we will present a quick diversion to a handy tool called sidetext. The sidetext function provides a simple means to placing text at the right side of the axes with its orientation identical to that created with ylabel. The sidetext function uses handle graphics to manipulate the position and orientation of the string you provide. Once you have downloaded sidetext and placed it in your working directory, try the following example:


which produces the graph shown in Figure 3.23.

SIDETEXT places a string on the right side of the axis.

Figure 3.23 SIDETEXT places a string on the right side of the axis.

Now change the scale of the axes with


and the graph will be that shown in Figure 3.24. The important point to note here is that changing the scale of the axes will not affect the position of the text created with sidetext since it positions the text relative to the axes.

SIDETEXT is unaffected by axes scale.

Figure 3.24 SIDETEXT is unaffected by axes scale.

You can place text interactively with the mouse by passing a string to the function gtext. After this command is issued, the mouse pointer will change from the standard arrow to a crosshair when the pointer is in the Figure Window. Position the crosshairs over the location in the figure where you wish to place the text and press either the mouse button or a key on your keyboard. The text string will appear left justified and vertically on top of the data point that was selected. For example, create a graph and type


Now, scale the axes to something other than what is currently shown in your figure. Notice that this text string changes its location relative to the axes border, but not relative to the data point that was selected. This function is useful when you have completed a graph and want to add a few additional lines of text. Also as you will learn in the last section of this topic, the current version of MATLAB lets you add text in the plot editing mode. However, if you are creating multiple plots, a fair number of text strings, or just want to automate this process in your MATLAB program, most likely you’ll find that the text function is better suited for these types of tasks.

The text function is both a high- and low-level graphics function that can be used to add character strings to a graph. For now, we’ll look at it as a high-level text placement command.

The most elementary way that text can be added to the current graph is with text(x,y, ‘text’) where the data point (x,y) corresponds to a location in the current axes. As an example, let’s have MATLAB draw a line plot and label the maximum data point as such.


This script will create the plot and text shown in Figure 3.25. Here we have added the 0.5 to the max_y_value variable so that the text will not overlap the line. By default, the text string will be placed left justified and vertically centered on the (x,y) data point that is provided. The addition of the 0.5 can be avoided by passing properties to the text function to keep the text vertically above the data point.

Using the text function to add text to your plot.

Figure 3.25 : Using the text function to add text to your plot.

The text function can also be used in a manner very similar to the way plot is used. For instance, if you want to create a scatter plot of the percent change in the consumer price index versus unemployment between 1965 and 1980, you can type


will create the plot shown in Figure 3.26.

Scatter Plot with text labels.

Figure 3.26 Scatter Plot with text labels.

You might have been wondering how to add a block of text, i.e., multiple lines of text to labels and titles. Perhaps it occurred to you that you could create multiple lines by repeated use of the text command, but then you would be faced with a kind of trial and error approach in order to get the location of your text to look right. Fortunately MATLAB provides a way to accomplish this without resorting to such manual methods. All of the built-in MATLAB text functions will accept a cell array of strings where each string contains the text for each line. This code for example,


will place the three lines of text wherever you position the mouse pointer in the figure. If you know exactly where you want to place the block of text, you could use


The final text placement command we will discuss is this section is the legend command. This function creates a legend of the line types that you have used in the current graph and associates these line types with the text strings that you pass to it. The order in which the lines are created is the order in which they are associated with the legend strings. For example,


will produce the result shown in Figure 3.27.

Creating a figure legend.

Figure 3.27 Creating a figure legend.

If you are not sure in which order the lines were created or you want only a few of the lines put into a legend you can use this form of the legend function:


This is probably the safest way to insure that when you create the legend the text string is correctly associated with the line you wanted. Implementing this for the previous example, we see that the legend command is replaced with


If you do not like the position that was automatically chosen by the legend function, you can use the mouse to click and drag the legend to a location of your choice.

Text Placement

There are several ways to place text in a position relative to the figure instead of the axes. For instance, you may wish to have a calendar date always located in the lower right-hand corner of the figure, even when you are displaying multiple axes, or subplots (see the following section). One method you can use is to create invisible axes that cover the entire figure space. Then place text within the invisible axes where location (0,0) is the lower left-hand corner and (1,1) is the upper right-hand corner of the figure. If you use this technique we recommend that, until you learn more about graphic objects and their handles, you create the invisible axes and the specially placed text after the rest of your plot looks the way you want it. The following code will produce the plot shown in Figure 3.28.





Placing text using invisible axes.

Figure 3.28 Placing text using invisible axes.

The problem alluded to above is that if you were to subsequently add another plot with


you end up plotting to the invisible axes as shown in Figure 3.29. This happens since plot commands always apply to the most recently created axes, unless you take advantage of handle graphics.

Problems arise when adding to a plot after using the invisible axes method.

Figure 3.29 Problems arise when adding to a plot after using the invisible axes method.

This plot looks bad because the text that we placed earlier moved to new locations when the invisible axes limits automatically scaled to accommodate the limits of the new data.

A way we can overcome the restrictions of this method is to normalize the position of the current axes to the figure and then place text in normalized units. With this approach it does not matter when you generate the text as long as there is at least one plot on the screen. This means that after you have created a plot with some specially placed text, you can then, for example, add more lines to the plot without affecting the text positions or without worrying about plotting to an invisible axis. To make this process easier we can create a new function, norm2fig, which will return the normalized text positions.


This function will let us quickly generate the text positions we want for the previous example to get the results shown in Figure 3.30.

To duplicate this result, first plot the sine, be sure that hold is on, then do the following:





Using the normalized position method of text placement.

Figure 3.30 Using the normalized position method of text placement.

Next post:

Previous post: