© Michael Paluszek and Stephanie Thomas 2020
M. Paluszek, S. ThomasMATLAB Recipeshttps://doi.org/10.1007/978-1-4842-6124-8_3

3. Visualization

Michael Paluszek1   and Stephanie Thomas2
(1)
Princeton, NJ, USA
(2)
Princeton Junction, NJ, USA
 

MATLAB provides extensive capabilities for visualizing your data. You can produce 2D plots, 3D plots, and animations; view images; and create histograms, contour and surface plots, as well as other graphical representations of your data. You are probably familiar with making simple 2D plots with lines and markers, and pie and bar charts, but you may not be aware of the additional possibilities made available by the MATLAB low-level routines that underpin the frequently used functions like plot. There are also interactive capabilities for editing plots and figures and adding annotations before printing or exporting them.

MATLAB excels in scientific visualization and in engineering visualization of 3D objects. Three-dimensional visualization is used to visualize data that is a function of two parameters, for example, the height on the surface of the Earth, or to visualize objects. The former is used in all areas of science and engineering. The latter is particularly useful in the design and simulation of any kind of machine including robots, aircraft, automobiles, and spacecraft.

Three-dimensional visualization of objects can be further divided into engineering visualization and photo-realistic visualization. The latter helps you understand what an object looks like and how it is engineered. When the inside of an object is considered, we move into the realm of solid modeling which is used for creating models suitable for the manufacturing of the object. Photo-realistic rendering focuses on the interaction of light with the object and the eye. MATLAB does provide some capabilities for lighting and camera interaction but does not provide true photo-realistic rendering.

The main plotting routines are organized into several categories in the command-line help:
graphics

– Low-level routines for figures, axes, lines, text, and other graphics objects.

graph2d

– Two-dimensional graphs like linear plots, log scale plots, and polar plots.

graph3d

– Three-dimensional graphs like lines, meshes, and surfaces; control of color, lighting, and the camera.

specgraph

– Specialized graphs, the largest category. Special 2D graphs like bar and pie charts and histograms, contour plots, special 3D plots, volume and vector visualization, image display, movies, and animation.

The online help has an entire top-level section devoted to graphics, including plots, formatting and annotation, images, printing and saving, graphics objects and performance, and major changes to plotting internals that occurred in R2014b.

A good command of these functions allows you to create very sophisticated graphics as well as to adapt them to different publication media, whether you need to adjust the dimensions, color, or font attributes of your plot. In this chapter, we will present recipes that cover what you need to know to use MATLAB graphics effectively. We don’t have space to discuss every available plotting routine, and that is well covered in the available help, but we will cover the basic functionality and provide recipes for common usage.

3.1 Plotting Data Interactively from the MATLAB Desktop

Problem

You would like to plot data in your workspace but aren’t sure of the best method for visualizing it.

Solution

You can use the PLOTS tab in the MATLAB desktop to plot data directly by selecting variables in the Workspace display as shown in Figure 3.1. You select from a variety of plot options, and MATLAB automatically only shows you those which are applicable to the selected data set.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig1_HTML.jpg
Figure 3.1

PLOTS tab with plot icon ribbon.

How It Works

Let’s create some sample data to demonstrate this interactive capability, which is a fairly new feature in MATLAB. We’ll start with some trigonometric functions to create sample data that oscillates.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figa_HTML.gif

We now have two vector variables available in the workspace. Select the PLOTS tab in the desktop as shown in Figure 3.1, then select the y variable in the Workspace display. The variable will appear on the far left of the PLOTS tab area, and various plot icons in the ribbon will become active: plot, bar, area, pie, and so on. Note the radio buttons on the far left for either reusing the current figure for the plot or creating a new figure.

Close all open figures with a close all and click the plot icon to create a new figure with a simple 2D plot of the data. Note that clicking the icon results in the plot command being printed to the command line:

../images/335353_2_En_3_Chapter/335353_2_En_3_Figb_HTML.gif

The data is printed with linear indices along the x axis, as shown in Figure 3.2.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig2_HTML.jpg
Figure 3.2

Linear plot of trigonometric data.

You simply click another plot icon to replot the data using a different function, and again the function call will be printed to the command line. The plot icons that are displayed are not all the plots available, but simply the default favorites from among all the many options; to see more icons, click the pop-up arrow at the right of the icon ribbon. The available plot types are organized by category, and there is a Catalog button that you can press to bring up a dedicated plot catalog window with the documentation for each function.

To plot our data y against our input theta, you need to select both variables in the workspace view. They will both be displayed in the plot ribbon with a button shown to reverse their order. Now click an area plot to get a plot with the angle on the x axis as shown in Figure 3.3.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig3_HTML.jpg
Figure 3.3

Parametric area plot of trigonometric data.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figc_HTML.gif

Note how this time the x-axis range is from 0 to 4π as expected.

You can annotate the plot interactively with arrows and text, add subplots, change line properties, and more using the Plot Edit toolbar and Figure Palette window shown in Figure 3.4. These are available from the View menu of the figure window and by clicking the “Show Plot Tools” button in the standard Figure Toolbar. For example, using the plot tools, we can select the axes, double-click it to open the property editor, type in an X Label, and turn on grid lines. We can add another subplot, plot the values of theta against linear indices, and then change the plot type to a stem plot, all from this window. See Figure 3.4.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig4_HTML.jpg
Figure 3.4

Plot of trigonometric data in the Figure Palette.

The same changes can be made programmatically as will be shown in the following recipes. In fact, you can generate code from the Figure Palette, and MATLAB will create a function with all the commands necessary to replicate your figure from your data. The Generate Code command is under the File menu of the window. This allows you to interactively create a visualization that works with some example data and then programmatically adapt it to your toolbox. MATLAB calls the new autogenerated function createfigure. You can see the use of the following functions: figure, axes, box, hold, ylabel, xlabel, title, area, stem, and annotation.

createfigure.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figd_HTML.gif

../images/335353_2_En_3_Chapter/335353_2_En_3_Fige_HTML.gif

Note that this code did not in fact use the subplot function, but rather the option to specify the exact axes location in the figure with the 'OuterPosition' property. Note also how the units of the axes position and of the annotations are between 0 and 1, that is, normalized. This is in fact an option for axes, as can be seen by the following call using gca to get the handle to the current axes:

../images/335353_2_En_3_Chapter/335353_2_En_3_Figf_HTML.gif

Using other units may be helpful for certain applications, but normalized units are always the default.

There are additional interactive buttons in the Figure Toolbar we should mention:
  • Zoom in

  • Zoom out

  • Hand tool – Move an object in the plane of the figure

  • Rotate tool – Rotate the view

  • Data cursor

  • Brush/select data

  • Colorbar

  • Legend

The hand and rotate tools are very helpful with 3D data. The data cursor displays the values of a plot point right in the figure. The brush highlights a segment of data using a contrast color of your choosing using the colors pop-up. The colorbar and legend buttons serve as on/off switches.

3.2 Incrementally Annotate a Plot

Problem

You need to annotate a curve in a plot at a subset of points on the curve.

Solution

Use the text function to annotate the plot.

How It Works

We will call text within a for loop in AnnotatePlot. Use sprintf to create the text for the annotations, which gives you control over the formatting of any numbers. In this case, we will use %d for integer display. linspace creates an evenly spaced index array into the data to give us the selected points to annotate, in this case, five points. linspace is used to produce evenly spaced points.

AnnotatePlot.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figg_HTML.gif

Note that we passed the index array j directly to the loop index k. Figure 3.5 shows the annotated plot. We create a three-dimensional straight line to annotate.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig5_HTML.jpg
Figure 3.5

Annotated three-dimensional plot.

3.3 Create a Custom Plot Page with Subplot

Problem

You need multiple plots of your data for a particular application, and as you rerun your script, they are cluttering your screen and hogging memory. We often create many dozens of plots as we work on our commercial toolboxes.

Solution

Create a single plot with several subplots on it so you only need one figure to see the results of one run of your application.

How It Works

The subplot function allows you to create a symmetric array of plots in a figure in two dimensions. You generate an m-by-n array of small axes which are spaced in the figure automatically. A good example is a 3D trajectory with views from different angles. We can create a plot with a 2 x 2 array of axes, with the 3D plot in the lower left-hand corner and views from each direction around it. The function is QuadPlot. It has a built-in demo creating the figure in Figure 3.6.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig6_HTML.jpg
Figure 3.6

QuadPlot using subplot for axes placement.

Note that you must use the size of your axes array, in this case (2,2), in each call to subplot.

QuadPlot.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figh_HTML.gif

../images/335353_2_En_3_Chapter/335353_2_En_3_Figi_HTML.gif

In the latest versions of MATLAB, you can easily access figure and axes properties using field names. For instance, let’s get the figure generated by the demo using gcf, then look at the children, which should include our four subplots.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figj_HTML.gif

Note that the titles of our axes are helpfully displayed. If you wanted to add additional objects or change the properties of the axes, you could access the handles this way. Or, you might want to provide the handles as an output for your function. You can also make a subplot in a figure the current axes just by calling subplot again with the array size and ID.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figk_HTML.gif

3.4 Create a Heat Map

Problem

You would like to create a heat map from data. A heat map shows the variation of magnitude using color in a two-dimensional image.

Solution

You can create a heat map using the heatmap function.

How It Works

We’ll create a random set of data and two cell arrays for the x and y names.

HeatMapDemo.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figl_HTML.gif

heatmap generates the map from the data shown in Figure 3.7.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig7_HTML.jpg
Figure 3.7

A heat map from random data.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figm_HTML.gif

3.5 Create a Plot Page with Custom-Sized Axes

Problem

You would like to group some plots together in one figure but not as evenly spaced subplots.

Solution

You can create custom-sized axes using the 'OuterPosition' property of the axes, placing them anywhere in the figure you wish.

How It Works

We’ll create a custom figure with two plots, one spanning the width of the figure and a second smaller axes. This will leave room for a block of descriptive text, which might describe the figure itself or display the results. In order to make the plots more interesting, we will add markers and text annotations using num2str.

The function is PlotPage shown in Figure 3.8. Using 'OuterPosition' for the axes instead of 'Position' means the limits will include the axes labels, so we can use the full range of the figure from 0 to 1 (normalized units). Figure 3.8 shows the resulting figure.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig8_HTML.jpg
Figure 3.8

PlotPage with custom-sized plots.

PlotPage.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Fign_HTML.gif

../images/335353_2_En_3_Chapter/335353_2_En_3_Figo_HTML.gif

3.6 Plotting with Dates

Problem

You want to plot data as a function of time using dates on the x axis.

Solution

Access the tick labels directly using handles for the axis, or use datetick with serial date numbers.

How It Works

First, we will manually specify the tick labels. You plot the data as a function of index and then replace the x labels with strings of your choice, in this case specific months. For example, we will plot power consumption of a home in kilowatt hours (kWh). Note how we set the xlim, xtick, and xticklabel properties using set after generating the plot. The limits are set to [0 13] instead of [1 12] to accommodate the width of the bars. Figure 3.9 shows plotting with month labels.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig9_HTML.jpg
Figure 3.9

Plotting with manual month labels.

PlottingWithDates.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figp_HTML.gif

If you are plotting data against complete dates, you can also use MATLAB’s serial date numbers, which can be automatically displayed as tickmarks using datetick. You can convert between calendar dates and serial numbers using datestr, datenum, and datevec. A date vector is the six-component date as [year month day hour minute second]. So, for instance, let’s assign our data in the preceding example to actual dates in the year 2014. The default date tickmarks will show months just like in our manual example, but for demonstration purposes, we specify a format including the year: 'mmmyy'. Figure 3.10 shows plotting with serial dates.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig10_HTML.jpg
Figure 3.10

Plotting using datetick with serial dates.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figq_HTML.gif

Note that the ticks themselves are no longer one per month; if you want to specify them manually, you now need to use date numbers. We have printed out the properties using get to show the XTicks used.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figr_HTML.gif

MATLAB’s serial date numbers do not correspond to other serial date formats like Julian date. MATLAB simply counts days from Jan-1-0000, so the year 2000 starts at a serial number of 2000*365 = 730,000. The following quick example demonstrates this as well as using now to get the current date:

../images/335353_2_En_3_Chapter/335353_2_En_3_Figs_HTML.gif

3.7 Generating a Color Distribution

Problem

You want to assign colors to markers or lines in your plot.

Solution

Specify the HSV components algorithmically from around the color wheel and convert to RGB.

How It Works

ColorDistribution chooses n colors from around the color wheel. The colors are selected using the hue component of HSV, with a full range from 0 to 1. Parameters allow the user to separately specify the saturation and value for all the colors generated. You could alternatively use these components to select a variety of colors of one hue.

Reducing the saturation (sat) lightens the colors while remaining on the same “spoke” of the color wheel. A saturation of 0 produces all grays. The value (val) keeps the ratio between RGB components remain the same, but lowering the magnitude makes colors darker, for example, [1 0.85 0] and [0.684 0.581 0]. See Figure 3.11.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig11_HTML.jpg
Figure 3.11

Original lines and lines with a color distribution with values and saturation of 1.

ColorDistribution.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figt_HTML.gif

Figure 3.11 plots a color distribution.

3.8 Visualizing Data over 2D or 3D Grids

Problem

You need to perform a calculation over a grid of data and view the results.

Solution

The function meshgrid produces grids over x and y that can be used for calculations and subsequently input to surf. This is also useful for contour and quiver plots.

How It Works

Our solution is in GridVisualization.m. First, you define the vectors in x and y that define your grid. You can perform your calculations in a for loop or in a vectorized function. The vectors do not have to be physical dimensions; indeed, in general, they are quite different quantities involved in a parametric study. The classic example is an exponential function of two variables, which is viewed as a surface in Figure 3.12.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig12_HTML.jpg
Figure 3.12

3D surface generated over a 2D grid.

GridVisualization.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figu_HTML.gif

The generated matrices are square and consist of the input vector replicated in the correct dimension. You could achieve the same result by hand using repmat, but meshgrid eliminates the need to remember the details.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figv_HTML.gif

For fun, we can plot contours of the data as well. We can use the gradient function to calculate the slope and plot this using quiver. This uses meshgrid that returns a 2D mesh from x and y vectors. Figure 3.13 shows a contour plot.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig13_HTML.jpg
Figure 3.13

3D surface visualized as contours.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figw_HTML.gif

You can also generate a 3D grid and compute data over the volume, for a fourth dimension. In order to view this extra data over the volume, you can use slice. This uses interpolation to draw slices at any location along the axes you specify. If you want to see the exact planes in your data, you can use pcolor, surf, or contour in individual figures. quiver3 can be used to plot arrows in 3D space. We are going to generate five slices at three different x values and at two different z values. The result is shown in Figure 3.14.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig14_HTML.jpg
Figure 3.14

3D volume with slices.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figx_HTML.gif

3.9 Generate 3D Objects Using Patch

Problem

You would like to draw a 3D box.

Solution

You can create a 3D box using the patch function.

How It Works

The patch function in MATLAB uses vertices and faces to define an area in two or three dimensions. The vertex list is an n-by-3 array specifying the vertex locations. The faces array is an n by m array where m is the number of vertices per polygon. The faces array contains the row indices for the vertices. We usually set m to 3 since all graphics engines eventually reduce polygons to triangles. We draw a box in BoxPatch shown in the following. Generally, when drawing a physical object, we set axis to equal so that the aspect ratio is correct. patch has many properties. In this case, we just set the color of the faces to gray using RGB. The edge color, which can also be specified, is black by default. The view(3) call sets the camera to a position with equal x, y, and z values. rotate3d on lets us move the camera around. This is very handy for inspecting the model. Each line in face is the three vertex elements that form a triangle face. Figure 3.15 show a box generated with patch.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig15_HTML.jpg
Figure 3.15

Box generated using patch.

BoxPatch.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figy_HTML.gif

3.10 Working with Light Objects

Problem

You would like to illuminate the 3D box drawn in the previous recipe.

Solution

You can create ambient or directed light objects using the light function. Light objects affect both patch and surface objects, which are created by surf, mesh, pcolor, fill, fill3, and patch.

How It Works

The main properties for working with light objects are color, style, position, and visible. The style may be infinite, with the light shining in parallel rays from a specified direction, or local, with a point source shining in all directions. The position property has a different meaning for each of these styles. PatchWithLighting adds a local light to the box script. We modify the box surface properties using material to get different effects.

PatchWithLighting.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figz_HTML.gif

Figure 3.16 shows dull and metal material with the same lighting. The lighting produced by MATLAB is limited by being an OpenGL lighting. Modern 3D graphics use textures and shaders for photo-realistic scene lighting. You also cannot generate shadows in MATLAB. The one on the right has a somewhat sharper color gradient at the corner.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig16_HTML.jpg
Figure 3.16

Box illuminated with a local light object. The left box has “dull” material. The one on the right has “metal.”

The dull, shiny, and metal settings for material set the patch properties to produce these effects. We can easily print the effects to the command line using get.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figaa_HTML.gif

Note that the AmbientStrength is 0.3 for all the material settings listed earlier. If you want to see the effect of only your light objects without ambient light, you have to manually set this to zero. In Figure 3.17, we have set the ambient strength to zero and applied the shiny material.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig17_HTML.jpg
Figure 3.17

Shiny box with ambient lighting removed (AmbientStrength set to 0) and a different camera viewpoint.

MATLAB has a lighting function to control the lighting model with four settings: none, Gouraud, Phong, and flat. Gouraud interpolates the lighting across the faces and gives the most realistic effect. Note that setting the lighting to Gouraud for our box sets the FaceLighting property to gouraud but the EdgeLighting to none, which will give a different effect than in our script earlier where the edge lighting was also set to Gouraud via its property. Flat lighting gives each entire face a uniform lighting, as in Figure 3.18, where we set the view to (-50,30) and the lighting to flat.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig18_HTML.jpg
Figure 3.18

Shiny box with flat lighting.

The MATLAB recommendations are to use flat lighting for faceted objects and Gouraud lighting for curved objects. The easiest way to compare these is to create a sphere, which is simple using the sphere function and generating a surface. This is done in the following SphereLighting. The infinite light object shines from the x axis. See Figure 3.19 for the resulting plots.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig19_HTML.jpg
Figure 3.19

Sphere illuminated with an infinite light object. The left sphere has flat lighting. The one on the right has Gouraud.

SphereLighting.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figab_HTML.gif

In addition to a sphere function, MATLAB also provides cylinder and ellipsoid.

3.11 Programmatically Setting the Camera Properties

Problem

You would like to have a camera in your scene that can be pointed.

Solution

Use the MATLAB cam functions. These provide the same functionality as the buttons in the camera toolbar, but with repeatability and the ability to pass in variables for the parameters. We demonstrate this in the script PatchWithCamera.m.

How It Works

We make two boxes in the scene. One is scaled and displayed from the other by 5 in x. We use the MATLAB functions camdolly, camorbit, campan, camzoom, and camroll to control the camera. We put all of these functions in the PatchWithCamera.m script and provide examples of two sets of parameters. Note that without lighting, the edges disappear.

PatchWithCamera.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figac_HTML.gif

../images/335353_2_En_3_Chapter/335353_2_En_3_Figad_HTML.gif

../images/335353_2_En_3_Chapter/335353_2_En_3_Figae_HTML.gif

Additional functions for interacting with the scene camera include campos and camtarget, which can be used to set the camera position and target. This can be used to image one object from the vantage point of another. camva sets the camera view angle, so you can model a real camera’s field of view. camup specifies the camera “up” vector or the direction of the top of the frame.

../images/335353_2_En_3_Chapter/335353_2_En_3_Fig20_HTML.jpg
Figure 3.20

Boxes with different camera parameters.

3.12 Display an Image

Problem

You would like to draw an image.

Solution

You can read in an image directly from an image file and draw it in a figure window. MATLAB supports a variety of formats including GIF, JPG, TIFF, PNG, and BMP. Our solution is in the script ReadImage.m.

How It Works

We read in a black and while image using imread and display it using imagesc. imagesc scales the color data into the colormap. It is necessary to apply the grayscale colormap; otherwise, you’ll get the colors in the default colormap. In parula, this is blue and yellow.

ReadImage.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figaf_HTML.gif

Figure 3.21 shows the mug first using pcolor, which creates a pseudocolor plot of a matrix, which is really a surf with the view looking down from above. To highlight this fact, we added a colorbar. Then on the right, the image is drawn using imagesc with a gray colormap. Observe that imagesc has changed the direction of the axes so that the image appears right-side up. Both plots have axes with tickmarks.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig21_HTML.jpg
Figure 3.21

Mug displayed using pcolor and imagesc.

MATLAB has another image display function called imshow, which is considered the fundamental image display function. This optimizes the figure, axes, and image object properties for displaying an image. If you have the Image Processing toolbox, imtool extends imshow with additional features. Notice how the image is displayed without the axes box. This function scales and selects the gray colormap automatically. Figure 3.22 shows the use of imshow
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig22_HTML.jpg
Figure 3.22

Mug displayed using imshow, with color limits applied on the right.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figag_HTML.gif

Not all images use the full depth available; for instance, this mug image has a minimum value of 30 and a maximum of 250. imshow allows you to set the color limits of the image directly, and the pixels will be scaled accordingly. We can darken the image by increasing the lower color limit and brighten the image by lowering the upper color limit.

3.13 Graph and Digraph

Problem

We have a stochastic process for which we want a graphical representation.

Solution

Use graph and digraph in the script RandomWalk.m.

How It Works

Generate a transition matrix showing the probability of transition from one state to a second state.

The code in RandomWalk.m creates a digraph, graph, and a random walk. The first part creates a transition matrix.

RandomWalk.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figah_HTML.gif

When we run RandomWalk at the command line we get the below output:

../images/335353_2_En_3_Chapter/335353_2_En_3_Figai_HTML.gif

The next part of RandomWalk creates a digraph shown in Figure 3.23 and a graph shown in Figure 3.24.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig23_HTML.jpg
Figure 3.23

Digraph for the random walk.

../images/335353_2_En_3_Chapter/335353_2_En_3_Fig24_HTML.jpg
Figure 3.24

Digraph for the random walk.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figaj_HTML.gif

../images/335353_2_En_3_Chapter/335353_2_En_3_Figak_HTML.gif

The random walk based on the transition matrix is shown in Figure 3.25.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig25_HTML.jpg
Figure 3.25

The random walk. The lines show the connections between the nodes in the random walk. All possible paths are shown.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figal_HTML.gif

3.14 Adding a Watermark

Problem

You have a lot of great graphics in your toolbox, and you would like them to be marked as having been created by your company. Alternatively, or additionally, you may want to mark images with a version number or date of the software that generated them.

Solution

You can use low-level graphics functions to add a textual or image watermark to figures that you generate in your toolbox. The tricky part is adding the items to the figure at the correct time so they are not overridden.

How It Works

The best way to add watermarks is to make a special axis for each text or image item you want to add. You turn the axis box off so all that you see is the text or image. In the first example, we add an icon and text to the lower left-hand corner of the plot. We add a color for the edge around the text so that it is nicely delineated. This is shown in Figure 3.26 using the Watermark.m function. In the example, we set the hard copy inversion to off, so that when we print the figure, we will get a gray background – this makes it easier to see in the book.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig26_HTML.jpg
Figure 3.26

Company watermark.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figam_HTML.gif

Watermark.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figan_HTML.gif

../images/335353_2_En_3_Chapter/335353_2_En_3_Figao_HTML.gif

As an additional example, we added text along the left- and right-hand sides of a figure using text rotation in the function DraftMark.m. We gave the text a light color. This marks the figure as a draft. We create a blank figure and axis before adding the draft mark, as shown in Figure 3.27.
../images/335353_2_En_3_Chapter/335353_2_En_3_Fig27_HTML.jpg
Figure 3.27

Draft watermark.

../images/335353_2_En_3_Chapter/335353_2_En_3_Figap_HTML.gif

Draftmark.m

../images/335353_2_En_3_Chapter/335353_2_En_3_Figaq_HTML.gif

../images/335353_2_En_3_Chapter/335353_2_En_3_Figar_HTML.gif

If you want to get very fancy, you could draw objects across the front of the figure and give them transparency, but it has to be fill or patch objects; text cannot be given transparency.

Summary
In this chapter, we reviewed key features of MATLAB visualization, from basic plotting to 3D visualization including objects and lighting. We demonstrated accessing figure and axes handles and setting properties programmatically, as well as using the interactive tools for figures. Creating helpful visualization routines is a key part of any toolbox. MATLAB provides excellent data management routines, including for large grids of data, and many options for colorization. Table 3.1 lists the code developed in the chapter.
Table 3.1

Chapter Code Listing

File

Description

AnnotatePlot

Add text annotations evenly spaced along a curve

BoxPatch

Generate a cube using patch

ColorDistribution

Demonstrate a color distribution for an array of lines

DraftMark

Add a draft marking to a figure

GridVisualization

Visualize data over 2D and 3D grids

PatchWithCamera

Generate two cubes using patch and point a camera at the scene

PatchWithLighting

Add lighting to the cube patch

PlotPage

Create a plot page with several custom plots in one figure

PlottingWithDates

Plot using months as the x label

QuadPlot

Create a quad plot page using subplot

ReadImage

Draw a JPEG image in a figure multiple ways

SphereLighting

Create and light a sphere

Watermark

Add a watermark to a figure

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.147.124