{ "metadata": { "name": "", "signature": "sha256:36d3a58a7f2f0e836b5c851fecb8963de3fc470a40602abd8eb765521fa2d56c" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![](https://bytebucket.org/davis68/resources/raw/f7c98d2b95e961fae257707e22a58fa1a2c36bec/logos/baseline_cse_wdmk.png?token=be4cc41d4b2afe594f5b1570a3c5aad96a65f0d6)](http://cse.illinois.edu/)\n", "\n", "\n", "# Plotting in MATLAB\n", "\n", "## Introduction\n", "\n", "This lesson introduces the basic features of MATLAB's plotting system, including a list of commonly required functions and their applications in a scientific computing context.\n", "\n", "\n", "### Contents\n", "- [Introduction](#intro)\n", "- [Plotting](#plot)\n", " - [Motivating Example](#motiv)\n", " - [Basic Functionality](#basics)\n", " - [Color](#color)\n", " - [Specific Functions](#specfn)\n", "- [Professional Plotting](#prof)\n", "- [References](#refs)\n", "- [Credits](#credits)\n", "\n", "\n", "---\n", "\n", "## Plotting\n", "\n", "\n", "### Motivating Example\n", "\n", "Let us consider patient inflammation data. This dataset records relative pain levels or inflammation in patients with arthritis.\n", "\n", " str = urlread('https://raw.githubusercontent.com/swcarpentry/matlab-novice-inflammation/gh-pages/data/inflammation-01.csv');\n", " str = strrep(str, ' ', ';');\n", " str = strrep(str, ',', ' ');\n", " str = strcat('[', str, ']');\n", " inflam = eval(str);\n", " \n", " \n", " plot(inflam);\n", " imagesc(inflam);\n", " \n", " \n", " avg_inflam = mean(inflam, 1);\n", " plot(avg_inflam);\n", " \n", " \n", " stdev = std(inflam, 1);\n", " plot(avg_inflam, avg_inflam+stdev, avg_inflam-stdev);\n", " t = 1:40;\n", " plot(t, avg_inflam, t, avg_inflam+stdev, t, avg_inflam-stdev);\n", " plot(t, avg_inflam, 'k-', ...\n", " t, avg_inflam+stdev, 'r--', ...\n", " t, avg_inflam-stdev, 'r--');\n", " \n", " \n", " subplot(1, 2, 1);\n", " plot(max(inflam, [], 1));\n", " ylabel('max')\n", " \n", " subplot(1, 2, 2);\n", " plot(min(inflam, [], 1));\n", " ylabel('min')\n", " \n", " % repeat the above with:\n", " ylim([0 20])\n", " \n", " \n", "\n", "### Basic Functionality\n", "\n", "`plot` displays coordinate pairs in a number of formats. It is the workhorse of 2D plotting in MATLAB. It has a simple format which has been replicated into other packages (such as Python's [MatPlotLib](http://matplotlib.org/)), so it is likely familiar or at least intuitive to you.\n", "\n", "There are a number of alternative display commands as well which tweak the output: `fill` and `errorbar`, for instance. The MatPlotLib developers maintain a [gallery](http://matplotlib.org/gallery.html) of examples with full source code.\n", "\n", "You have access to the [entire palette](http://matplotlib.org/api/colors_api.html) of modern systems as well.\n", "\n", " x = linspace(0, 1, 10001);\n", " y = cos(pi./x) .* exp(-2.*x);\n", " plot(x, y)\n", " \n", " plot(x, y, 'r--')\n", " \n", " plot(x, y, 'g-', 'LineWidth', 3)\n", " \n", " plot(x, y, 'LineWidth', 0.5, 'Color', [0.4 0.5 0.9])\n", " \n", " plot(x, y, 'LineWidth', 0.5, 'Color', [0.1 0.8 0.2])\n", " axis([0,1,-inf,inf])\n", "\n", "Arguments are found [in the documentation](http://www.mathworks.com/help/matlab/ref/plot.html#inputarg_LineSpec).\n", "\n", "###### Exercise\n", "\n", "- Plot the following equations over the domain $y \\in \\left[-1, 2\\right]$.\n", " - $y = f(x) = x^2 \\exp(-x)$\n", " - $y = f(x) = \\log x$\n", " - $y = f(x) = 1 + x^x + 3 x^4$\n", "\n", " x = linspace(-1,2,1000)\n", " y = x.^2.*log(x)\n", " plot(x,y)\n", "\n", "\n", "\n", "### Color\n", "\n", "MATLAB's color naming system is very basic. There are eight named colors, which can be referred to by their short or long names.\n", "\n", "| RGB Value | Short Name | Long Name |\n", "|:-:|:-:|:--|\n", "| [1 1 0] | y | yellow |\n", "| [1 0 1] | m | magenta |\n", "| [0 1 1] | c | cyan |\n", "| [1 0 0] | r | red |\n", "| [0 1 0] | g | green |\n", "| [0 0 1] | b | blue |\n", "| [1 1 1] | w | white |\n", "| [0 0 0] | k | black |\n", "\n", "The short versions are often used in combination with the `LineStyle` specification, one of `'-' | '--' | ':' | '-.' | 'none'`.\n", "\n", " x = linspace(0,1);\n", " hold on\n", " plot(x, x, 'r-')\n", " plot(x, x.^2, 'm--')\n", " plot(x, x.^3, 'b:')\n", " plot(x, x.^4, 'c-.')\n", " hold off\n", "\n", "The `hold` keyword causes a figure to persist until closed, meaning that new `plot` commands will display on the same figure rather than opening a new one.\n", "\n", "\n", "### Specific Functions\n", " \n", "- `errorbar`\n", "\n", " x = linspace(0, 10, 21);\n", " y = 10.*exp(-x);\n", " yerr = rand(1,length(x)) .* exp(-x./2);\n", " errorbar(x, y, yerr)\n", "\n", "\n", "- [`errorbarxy`](http://www.mathworks.com/matlabcentral/fileexchange/40221-plot-data-with-error-bars-on-both-x-and-y-axes)\n", " x = 1:10; \n", " xe = 0.5*ones(size(x)); \n", " y = sin(x); \n", " ye = std(y)*ones(size(x)); \n", " H=errorbarxy(x,y,xe,ye,{'ko-', 'b', 'r'}); \n", "\n", "\n", "- `fill`\n", " x = linspace(0, 2*pi, 1001);\n", " y = sin(2.*x.^2./pi);\n", " fill(x, y, 'b')\n", "\n", "\n", "- `fplot`\n", " fplot(@atanh, [-2*pi, 2*pi])\n", "\n", "\n", "- `plot3`\n", " t = 0:pi/50:10*pi;\n", " st = sin(t);\n", " ct = cos(t);\n", " figure\n", " plot3(st,ct,t)\n", "\n", "\n", "- `ezplot`\n", " ezplot('x^3 + x^2 - 4*x'); % explicit\n", " ezplot('x^2-y^4*exp(-y)'); % implicit\n", " ezplot('2*t','2/(1+t^2)',[0 10]); % parametric\n", "\n", "\n", "- `hist`\n", " rng default; % reproducible\n", " N = 1000;\n", " x = random('normal', 0, 1, N, 1);\n", " \n", " avg = mean(x);\n", " stdev = std(x);\n", " \n", " x_avg = ones(N,1)* avg;\n", " x_stdl = ones(N,1)*(avg-std);\n", " x_stdh = ones(N,1)*(avg+std);\n", " t = 1:N;\n", " \n", " plot(t, x, 'bx', ...\n", " t, x_avg, 'r-', ...\n", " t, x_avg+stdev, 'r--', ...\n", " t, x_avg-stdev, 'r--');\n", " title(sprintf('%d Random Gaussian Numbers', N));\n", " xlabel('$n$');\n", " ylabel('$U(n)$');\n", " \n", " hist(x,20);\n", " title(sprintf('Distribution of %d Random Gaussian Numbers', N));\n", " xlabel('$U(n)$');\n", " ylabel('Frequency of $U$');\n", "\n", "\n", "- `contour`\n", " function [Z] = func(X, Y)\n", " Z = X.*(1-X).*cos(4*pi*X).*sin(2*pi*sqrt(Y));\n", " end\n", " \n", " \n", " x = linspace(0,1);\n", " y = linspace(0,1);\n", " [X,Y] = meshgrid(x,y);\n", " Z = func(X,Y);\n", " contour(X,Y,Z);\n", " \n", " contourf(X,Y,Z);\n", " \n", " \n", " pts = rand(500,2);\n", " vals = func(pts(:,1), pts(:,2));\n", " \n", " mesh(X,Y,Z);\n", " hold on\n", " plot3(pts(:,1), pts(:,2), vals, 'k.')\n", " \n", " \n", " subplot(2, 3, 1);\n", " mesh(X,Y,Z);\n", " hold on\n", " plot3(pts(:,1), pts(:,2), vals, 'k.')\n", " title('original');\n", " \n", " subplot(2, 3, 2);\n", " grid_z0 = griddata(pts(:,1), pts(:,2), vals, X, Y, 'nearest');\n", " mesh(X,Y,grid_z0);\n", " title('nearest');\n", " \n", " subplot(2, 3, 3);\n", " grid_z0 = griddata(pts(:,1), pts(:,2), vals, X, Y, 'linear');\n", " mesh(X,Y,grid_z0);\n", " title('linear');\n", " \n", " subplot(2, 3, 4);\n", " grid_z0 = griddata(pts(:,1), pts(:,2), vals, X, Y, 'natural');\n", " mesh(X,Y,grid_z0);\n", " title('natural');\n", " \n", " subplot(2, 3, 5);\n", " grid_z0 = griddata(pts(:,1), pts(:,2), vals, X, Y, 'cubic');\n", " mesh(X,Y,grid_z0);\n", " title('cubic');\n", " \n", " subplot(2, 3, 6);\n", " grid_z0 = griddata(pts(:,1), pts(:,2), vals, X, Y, 'v4');\n", " mesh(X,Y,grid_z0);\n", " title('bilinear spline');\n", " \n", " \n", " subplot(2, 3, 1);\n", " contourf(X,Y,Z);\n", " hold on\n", " plot3(pts(:,1), pts(:,2), vals, 'k.')\n", " \n", " subplot(2, 3, 2);\n", " grid_z0 = griddata(pts(:,1), pts(:,2), vals, X, Y, 'nearest');\n", " contourf(X,Y,grid_z0);\n", " \n", " subplot(2, 3, 3);\n", " grid_z0 = griddata(pts(:,1), pts(:,2), vals, X, Y, 'linear');\n", " contourf(X,Y,grid_z0);\n", " \n", " subplot(2, 3, 4);\n", " grid_z0 = griddata(pts(:,1), pts(:,2), vals, X, Y, 'natural');\n", " contourf(X,Y,grid_z0);\n", " \n", " subplot(2, 3, 5);\n", " grid_z0 = griddata(pts(:,1), pts(:,2), vals, X, Y, 'cubic');\n", " contourf(X,Y,grid_z0);\n", " \n", " subplot(2, 3, 6);\n", " grid_z0 = griddata(pts(:,1), pts(:,2), vals, X, Y, 'v4');\n", " contourf(X,Y,grid_z0);\n", " \n", " \n", " subplot(1, 2, 1);\n", " colormap 'copper'\n", " grid_z0 = griddata(pts(:,1), pts(:,2), vals, X, Y, 'v4');\n", " mesh(X,Y,grid_z0);\n", " \n", " subplot(1, 2, 2);\n", " grid_z0 = griddata(pts(:,1), pts(:,2), vals, X, Y, 'v4');\n", " contourf(X,Y,grid_z0);\n", " \n", "[Various colormaps](http://www.mathworks.com/help/matlab/ref/colormap.html#inputarg_name) may be selected using `colormap 'name'` for a current figure.\n", "\n", "\n", "- `polar`\n", " ezpolar('3*cos(5*t)+1')\n", "\n", "- `ezsurf`\n", "\n", "- `quiver` (`streamline` and similar plots are also available)\n", " [X,Y] = meshgrid(-2:0.2:2);\n", " Z = X.^2.*exp(-X.^2 - Y.^2);\n", " [DX,DY] = gradient(Z,0.2,0.2);\n", " \n", " figure\n", " contour(X,Y,Z)\n", " hold on\n", " quiver(X,Y,DX,DY)\n", " hold off\n", "\n", "###### Exercise\n", "\n", "- Play around with some other functions in the previous examples.\n", "\n", "\n", "- Subplots\n", " - `subplot(X,Y,Z)` == `subplot(XYZ)` as shorthand\n", " - `figure; hold {on|off}`\n", "\n", "- `fill3` (polyhedra)\n", "\n", " [`polyhedra.m`](./polyhedra.m)\n", "\n", "Finally, we should mention that the MATLAB plotting system consists of a series of nested objects, from the figure to the axes and their properties. You can find out more about the advanced structure at [mathworks.com](http://www.mathworks.com/help/matlab/creating_plots/graphics-objects.html). You can use `gcf`, a persistent reference to the current figure, and `set` to change figure properties in bulk (particularly handy with many subplots):\n", "\n", " % Collect all axis handles.\n", " axes = findall(gcf,'type','axes');\n", " \n", " % Set the y-limits of all axes simultaneously.\n", " set(axes, 'ylim', [0 10]);\n", "\n", "- `animatedline` MATLAB recently (R2014b) added the `animatedline` function. This lets you illustrate the development of a data set over time in a flashy way.\n", "\n", " h = animatedline;\n", " axis([0,4*pi,-1,1])\n", "\n", " x = linspace(0,4*pi,1000);\n", " y = sin(x);\n", " for k = 1:length(x)\n", " addpoints(h,x(k),y(k));\n", " drawnow\n", " end\n", "\n", "\n", "---\n", "\n", "### Professional Plotting\n", "\n", "If you want to improve your plots from the default to make them publication-quality, all you need to do is use the Plot Editor interface and punch things up.\n", "\n", "Using standard commands, you can easily design publication-quality output. You can save the figures as high-resolution PNGs (or other file types). You can even use $\\LaTeX$ markup to yield mathematical formulae in labels, titles, and legends.\n", "\n", " % http://www.mathworks.com/matlabcentral/fileexchange/4913-chebyshevpoly-m\n", " cheby = zeros(9);\n", " for i = 0:8\n", " chebypoly = ChebyshevPoly(i);\n", " cheby(i+1,9-length(chebypoly)+1:9) = chebypoly;\n", " end\n", " \n", " fig = figure;\n", " x = linspace(-1,1,1001);\n", " for i = 0:8\n", " subplot(3,3,i+1);\n", " plot(x,polyval(cheby(i+1,:),x));\n", " xlabel('$x$','Interpreter','latex');\n", " ylabel(sprintf('$T_%d$',i),'Interpreter','latex');\n", " end\n", " \n", "\n", "- Output plots as vector images (`eps` (preferred) or `svg`) as well as raster (`png` (preferred), `gif`, or `jpg`). This allows you to generate new versions with more detail if necessary. Also have a clearly documented section of code and data for each image in order to reproduce images as well if necessary.\n", "\n", " saveas(fig, 'chebyshev', 'jpg');\n", " saveas(fig, 'chebyshev', 'png');\n", " saveas(fig, 'chebyshev', 'eps');\n", "\n", "\n", "- Experiment with the following $\\LaTeX$ labels in the `DisplayName` argument in the following code snippet. (They won't describe the figure, but whatever...)\n", "\n", " x = linspace(0, 2*pi, 101)\n", " plot(x, sin(x), 'DisplayName','$L_i$', 'LineWidth', 2)\n", " legend('show');\n", " set(legend,'Interpreter','latex','FontSize',24,'EdgeColor',[1 1 1]);\n", "\n", "\n", " - '$A_i^j (x)$'\n", " - '$(1 - x_0) \\cdot x^{x^x}$'\n", " - '$\\frac{x + y}{x^y}$'\n", " - '$\\int_0^\\infty \\exp(-x) dx$'\n", " - '$\\sum_{n=0}^{10} \\frac{x}{x-5}$'\n", " - sprintf('$\\pi = %f$',pi)\n", "\n", "\n", "- To get plots really looking sharp, you need to adjust the labels, font sizes, typefaces, and other plot properties for readability.\n", "\n", " x = linspace(0, 6, 201);\n", " y = zeros(8, 201);\n", " y(1,:) = besselj(0, x);\n", " y(2,:) = besselj(1, x);\n", " y(3,:) = bessely(0, x);\n", " y(4,:) = bessely(1, x);\n", " y(5,:) = besseli(0, x);\n", " y(6,:) = besseli(1, x);\n", " y(7,:) = besselk(0, x);\n", " y(8,:) = besselk(1, x);\n", " \n", " figure\n", " hold on\n", " plot(x, y(1,:), 'r-', 'LineWidth', 2, 'DisplayName', '$J_0(x)$');\n", " plot(x, y(2,:), 'r--', 'LineWidth', 2, 'DisplayName', '$J_1(x)$');\n", " plot(x, y(3,:), 'b-', 'LineWidth', 2, 'DisplayName', '$Y_0(x)$');\n", " plot(x, y(4,:), 'b--', 'LineWidth', 2, 'DisplayName', '$Y_1(x)$');\n", " plot(x, y(5,:), 'g-', 'LineWidth', 2, 'DisplayName', '$I_0(x)$');\n", " plot(x, y(6,:), 'g--', 'LineWidth', 2, 'DisplayName', '$I_1(x)$');\n", " plot(x, y(7,:), 'y-', 'LineWidth', 2, 'DisplayName', '$K_0(x)$');\n", " plot(x, y(8,:), 'y--', 'LineWidth', 2, 'DisplayName', '$K_1(x)$');\n", " \n", " title('Examples of Zeroth- and First-Order Bessel Functions')\n", " \n", " legend('show');\n", " set(legend,'Interpreter','latex','FontSize',24,'EdgeColor',[1 1 1],'Location','bestoutside');\n", " \n", " ylim([-1 2]);\n", " ylabel('$f(x)$','Interpreter','latex','FontSize',18)\n", " ylabel('$x$','Interpreter','latex','FontSize',18)\n", "\n", " There are a few situations when you may want your plots to be in black and white, such as publication in a journal or when your article may be photocopied. Here we set the same lines to monochrome equivalents.\n", "\n", " figure\n", " hold on\n", " plot(x, y(1,:), 'k-', 'LineWidth', 2, 'DisplayName', '$J_0(x)$');\n", " plot(x, y(2,:), 'k--', 'LineWidth', 2, 'DisplayName', '$J_1(x)$');\n", " plot(x, y(3,:), 'k.', 'LineWidth', 2, 'DisplayName', '$Y_0(x)$');\n", " plot(x, y(4,:), 'k:', 'LineWidth', 2, 'DisplayName', '$Y_1(x)$');\n", " plot(x, y(5,:), '-', 'LineWidth', 2, 'DisplayName', '$I_0(x)$', 'Color', [0.75,0.75,0.75]);\n", " plot(x, y(6,:), '--', 'LineWidth', 2, 'DisplayName', '$I_1(x)$', 'Color', [0.75,0.75,0.75]);\n", " plot(x, y(7,:), '.', 'LineWidth', 2, 'DisplayName', '$K_0(x)$', 'Color', [0.75,0.75,0.75]);\n", " plot(x, y(8,:), ':', 'LineWidth', 2, 'DisplayName', '$K_1(x)$', 'Color', [0.75,0.75,0.75]);\n", " \n", " title('Examples of Zeroth- and First-Order Bessel Functions')\n", " \n", " legend('show');\n", " set(legend,'Interpreter','latex','FontSize',24,'EdgeColor',[1 1 1],'Location','bestoutside');\n", " \n", " ylim([-1 2]);\n", " ylabel('$f(x)$','Interpreter','latex','FontSize',18)\n", " ylabel('$x$','Interpreter','latex','FontSize',18)\n", "\n", " Finally, once you have a plot you are pleased with, you can export the settings as a function file which will accept your data and reproduce a stylistically-matching plot for you on demand.\n", "\n", "\n", "- Prefer figures to tables. (Tabular data can be included in an appendix if necessary.) Data comprehension is much better when viewing graphical representations.\n", "\n", "- Additionally, accomodating color blindness is a common motivation for choosing a nonstandard palette. [ColorBrewer](http://colorbrewer2.org/#) presents color palettes with a number of filters such as print-friendliness and visibility for the color blind.\n", "\n", " You may also consider utilizing the [CUBEHELIX](http://www.mathworks.com/matlabcentral/fileexchange/43700-cubehelix-colormaps--beautiful--distinct--versatile-) palette in documents which may be printed or accessed in black and white[[citation](http://adsabs.harvard.edu/abs/2011BASI...39..289G)]; [[tutorial](http://www.mrao.cam.ac.uk/~dag/CUBEHELIX/)]. The CUBEHELIX algorithm generates color palettes which continuously increase (or decrease) in intensity regardless of the hue. They also stand up well in desaturated environments like lousy projectors in overbright rooms.\n", "\n", " [X,Y,Z] = peaks(30); \n", " surfc(X,Y,Z) \n", " colormap(cubehelix([],0.5,-1.5,1,1,[0.29,0.92])) \n", " axis([-3,3,-3,3,-10,5])\n", "\n", "Basically, the CUBEHELIX algorithm desaturates consistently through a given color space ([more info here](http://stackoverflow.com/a/15623251/1272411)).\n", "\n", " cubehelix_view\n", "\n", "![](http://i.stack.imgur.com/3Izz0.jpg)\n", "\n", "\n", "---\n", "\n", "## References\n", "\n", "- Brewer, Cynthia. [ColorBrewer](http://colorbrewer2.org/).\n", "- Waskon, Michael. [Choosing color palettes](http://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html).\n", "\n", "\n", "---\n", "\n", "## Credits\n", "\n", "Neal Davis and Lakshmi Rao developed these materials for [Computational Science and Engineering](http://cse.illinois.edu/) at the University of Illinois at Urbana\u2013Champaign. (This notebook is originally based on the Python MatPlotLib lesson.) The inflammation example is drawn from [Software Carpentry](http://software-carpentry.org/).\n", "\n", "\n", "This content is available under a [Creative Commons Attribution 4.0 Unported License](https://creativecommons.org/licenses/by/4.0/).\n", "\n", "[![](https://bytebucket.org/davis68/resources/raw/f7c98d2b95e961fae257707e22a58fa1a2c36bec/logos/baseline_cse_wdmk.png?token=be4cc41d4b2afe594f5b1570a3c5aad96a65f0d6)](http://cse.illinois.edu/)" ] } ], "metadata": {} } ] }