2. Plot Formatting

MATLAB’s plotting functions have become very popular, and yet there are some basic deficiencies. The main problems (and some ways to correct when applicable) are listed below. How to address them using set_plot is also shown.

  1. When saved to PDF, graphics are always printed to a standard letter-sized sheet of paper.

    • A traditional fix to this is to save to EPS and then convert the graphic to PDF later.
    • Because MATLAB does a better job of creating EPS (Encapsulated PostScript) than PDF (Portable Document Format), this can be a good idea anyway.
    • This is always corrected when set_plot() is called.
  2. Graphics are normally saved to very large figures, with the result that text is difficult to read when scaled down to appropriate dimensions for publication.

    • This is very challenging to fix properly.
    • Many set_plot FigureStyles correct this automatically.
    • It can be set explicitly by setting the 'Width' key.
  3. It is difficult to change the fonts to Serif style for publications that prefer figure text to be in that form.

    • In set_plot, the 'FontStyle' key allows easy access to this.
  4. The color schemes are often inappropriate for conversion to grayscale.

    • The set_plot package provides extensive tools for setting colors. In the simplest case, setting the 'ColorStyle' key to 'gray' will produce grayscale graphics directly.
  5. Once a graphics object (e.g., a line, a contour map, etc.) is created, it’s difficult to find the handle again if it needs to be changed.

    • If set_plot() is called with an output, it will collect all of the handles found in the figure into categories.

2.1. Inputs

The basic usage of the set_plot() function is to give the function a figure handle and a list of options. For example, the following creates a contour plot and then applies some formatting to it.

>> h_f = figure
>> contour(peaks(50))
>> set_plot(h_f, 'FigureStyle', 'journal')
_images/peaks-journal.png

It is possible to specify as many key name value pairs as you would like.

>> set_plot('FigureStyle', 'journal', 'ContourStyle', 'fancy')
_images/peaks-fancy.png

Furthermore, if you have a series of figures to which you want to apply the same (or similar) settings, there is another convenient input format.

>> keys.FigureStyle = 'journal';
>> keys.ContourStyle = 'fancy';
>> keys.ColorMap = 'reverse-jet';
>> keys.FontStyle = 'presentation';
>> set_plot(h_f, keys)
_images/peaks-keys.png

2.2. Cascading Styles

The set_plot packages uses a system of cascading styles (similar to CSS) in which some of the format specifiers are children of others. From the previous examples, you may have figured out that 'FigureStyle' is the parent style that sets a general theme for the figure. In fact, there is no setting that is not affected by 'FigureStyle'. However, if you specify a value for a key that is a child of 'FigureStyle', the manually selected value overrides the one set by 'FigureStyle'. As a concrete example, the command

>> set_plot('FigureStyle', 'journal', 'ContourStyle', 'fancy')
_images/peaks-fancy.png

uses a 'ContourStyle' of 'fancy', not the default 'FigureStyle', 'journal' value of 'pretty'.

2.3. Examples

2.3.1. Plotting Lines

In an attempt to make lines in a plot more distinguishable, many set_plot() commands change the appearance of lines automatically. For example the 'journal' FigureStyle changes lines to alternate among solid lines, dashed lines, and dot-dashed lines. The 'color' FigureStyle makes all of the lines solid and instead varies the colors.

However, this behavior is not always desirable, especially if there is a data set that is represented by data points and not a continuous curve. Consider the following example.

>> x1 = linspace(0, 1, 101);
>> x2 = linspace(0, 1, 8);
>> hold('on')
>> plot(x1, sinc(x1))
>> plot(x1, 5*x1*(0.75-x1)
>> plot(x2, sinc(x2) - (x2-0.5).^2, '^')
_images/lines-plain.png

Note that since the three were plotted independently, they all default to the default style ('b-') except where explicitly overridden. An application of set_plot() to this gives the following.

>> set_plot('FigureStyle', 'fancy', 'Width', 3.1)
_images/lines-fancy.png

All of the MATLAB line objects are found, but only lines that have a 'LineStyle' (that is, other than 'none') are affected. As a result, any data series that are plotted without connecting lines are ignored. In addition, the lines are given alternating colors according to the value of the set_plot parameter ColorSequence.

Note

Prior to set_plot version 0.9.0, all data series were affected, such that the blue triangles would have been joined using the code from the preceding example.

Furthermore, since set_plot() defaults to leaving things unchanged, it is possible to change a low-level plot format setting with a single command.

>> set_plot('LineWidth', 2)
_images/lines-clean.png

Note

Prior to set_plot version 0.9.0, this format key was called 'PlotLineWidth'. For compatibility, newer versions still recognize this longer key name. The parameter 'PlotLineStyle' is recognized for similar reasons.

However, if you have put hard work into manually picking the style for your lines, and you would still like to utilize the other features of set_plot(), there is a solution for that as well.

>> plot(x1, x1, 'Color', [0.4,0,0.8], 'LineWidth', 2)
>> plot(x1, 4*x1.*(1-x1), 'Color', [0.8,0.6,0.1], 'LineWidth', 2)
>> set_plot('FigureStyle','journal', 'PlotStyle','current', 'ColorStyle','current')
_images/polys-clean.png

Note

The PlotStyle must be set to 'current' to keep the current line styles and widths, and the ColorStyle controls the colors of the lines. In some previous versions of set_plot, the ColorStyle portion of the command may not be necessary, and this may require changes to your code.

2.3.2. Text and Legends

Whenever set_plot() finds a text object that contains text delimited in '$' characters, it automatically interprets those text portions using the 'latex' interpreter. Consider the example from above with three data series and add some text to it.

>> xlabel('Length, $x$')
>> ylabel('Amplitude, $\hat{A}_i$')
>> legend('Theory, 'Lower bound, $A_L$', 'Experiment')
>> h = set_plot(h_f, 'FigureStyle','journal', 'PlotStyle','fancy');
_images/legend-bad.png

Although the text in this example looks quite nice, the legend position is clearly non-ideal. Unfortunately, set_plot() has no capability to automatically place the legend, but it can help with moving it.

>> p = get(h.legend, 'Position');
>> p(1) = 0.7;
>> p(2) = 0.6;
>> set(h.legend, 'Position', p)
_images/legend-fixed.png

Of course, moving the legend manually is also acceptable. The final example shows how to change the basic text to a sans serif theme, although text pieces that are interpreted using 'latex' are not affected.

>> set_plot('FontStyle', 'sans-serif')
_images/legend-sans.png

This example shows why it is advisable to have all of the legend entries use the same interpreter.

2.3.3. Working with Color Bars

The set_plot package is set up to work with the MATLAB colorbar object. The following example shows some typical formatting.

>> [x, y] = meshgrid(linspace(-1, 1, 101));
>> contourf(x, y, sin(2*pi*x) + sin(2*pi*y));
>> colorbar
>> set_plot('FigureStyle','journal', 'ContourStyle','fill');
_images/cbar-journal.png
>> set_plot('FigureStyle','fancy', 'ContourStyle','fill')
_images/cbar-fancy.png

2.3.4. Outputs

As demonstrated in the legend example above, it is possible to call set_plot() with outputs (although no output will be produced when set_plot() is called with no output; i.e., it won’t change the variable of ans). The output collects all of the figure, line, text, and any other handles that it can find. This can be convenient for changing the graphics after set_plot() has been called—especially when writing a script to create graphics. The following example demonstrates this output on a fairly busy example.

>> x = linspace(0, 1, 51)
>> contour(x, x, peaks(51))
>> hold('on')
>> plot(x, 0.1 + x.*(2-x)/2)
>> colorbar
>> keys.FigureStyle  = 'journal';
>> keys.ContourStyle = 'smooth';
>> keys.LineWidth    = 2;
>> keys.ColorStyle   = 'current';
>> keys.ColorMap     = 'g';
>> h = set_plot(keys)
h =
      figure: 1
        axes: 173.0149
       label: [1x1 struct]
       title: 216.0088
        text: [7x1 double]
        line: 190.0125
     contour: [1x1 struct]
    colorbar: 191.0125
_images/output-contour.png

As one demonstration of how this can be useful, suppose you realize after creating the graphic that you used the wrong data for the plot’s y-coordinates. No problem!

>> set(h.line, 'YData', x.^2)
_images/output-altered.png