% Laboratory in Oceanography – Data Visualization in Matlab % SMAST, UMass Dartmouth % % Graphics in Matlab lecture 1 % MAR 599 % Spring 2009 % % Daniel Birch % dbirch@umassd.edu %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% HANDLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Definition: A HANDLE is a number that identifies a graphics object. %Handles are used to get and set object properties. %Use handles to manipulate figures, axes, lines, labels, titles, and colorbars. % %Examples of using handles: clc; clear all; close all; figHandle = figure%Create a figure gcf%gcf is a function that takes no arguments and %returns the handle of the current figure get(figHandle)%List the object's properties and their current values get(figHandle, 'color')%Look at a single property set(figHandle, 'COLoR', 'rEd');%Property names are NOT case-sensitive. %The first argument of get/set is always a handle L = 10; x = linspace(0,L,101);%101 linearly spaced points between 0 and L (inclusive) y = cos(2*pi*x/L); lineHandle = plot(x,y,'k-') get(lineHandle) set(lineHandle, 'marker')%See all possible values of the property 'marker' set(lineHandle, 'marker', 's') axesHandle = gca%gca is a function that takes no arguments and %returns the handle of the current axes get(axesHandle) set(axesHandle, 'color', [1, .5, 0]) labelHandle = xlabel('x') get(axesHandle, 'xlabel') get(labelHandle) hold on; plot(x, rand(size(x)), 'r-'); allLines = findobj(gca, 'type', 'line')%What's the difference between using %gca and gcf here? set(allLines, 'color', 'blue') get(0, 'children')%0 is the root. This line returns the handles to all figures %Guess what the following line does: set(get(get(get(0, 'children'), 'currentaxes'), 'xlabel'), 'string', 't') whos %See that handles are just doubles sqrt(axesHandle) %You can do anything to a handle that you can do to any %other double (but I don't know why you'd want to). %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%% SETTING DEFAULTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %REMARK: Store default graphics properties in m-files to save time and to %make figure appearances consisten. % clc; close all; clear all; %paper_defaults.m %%% PAPER DEFAULTS %%%%%%%%%%%% COLORS %%%%%%%%%%%%%%%%%%%%%%%%%%%%% set(0, 'defaultaxescolor', 'white'); set(0, 'defaultaxesxcolor', 'black'); set(0, 'defaultaxesycolor', 'black'); set(0, 'defaultaxeszcolor', 'black'); set(0, 'defaulttextcolor', 'black'); set(0, 'defaultfigurecolor', [0.75, 0.75, 0.75]); set(0, 'defaultaxescolororder', [0,0,0]); %%%%%%%%%%%% LINE WIDTHS %%%%%%%%%%%%%%%%%%%%%% set(0, 'defaultaxeslinewidth', .7); set(0, 'defaultpatchlinewidth', .7); set(0, 'defaultlinelinewidth', 1); %%%%%%%%%%%% TICK MARKS %%%%%%%%%%%%%%%%%% set(0, 'defaultaxesxminortick', 'on'); set(0, 'defaultaxesyminortick', 'on'); set(0, 'defaultaxestickdir', 'out'); set(0, 'defaultaxesticklength', [0.02, 0.05]);%originally [0.01 0.025] %%%%%%%%%%%%% FONTS %%%%%%%%%%%%%%%%%%%%%%%%%% set(0, 'defaultAxesFontSize', 14); set(0, 'defaultAxesFontName', 'Times'); set(0, 'defaultAxesFontWeight', 'normal'); set(0, 'defaultTextFontName', 'Times'); set(0, 'defaultTextFontSize', 14); set(0, 'defaultTextFontWeight', 'normal'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% disp('Paper defaults set.'); L = 10; x = linspace(0,L,1001); y = cos(2*pi*x/L); figure; plot(x, y); title('This was made with paper defaults'); xlabel('{\itx}'); ylabel('{\ity}'); %presentation_defaults.m %%% PRESENTATION DEFAULTS %%%%%%%%%%%%% COLORS %%%%%%%%%%%%%%%%%%%%% set(0, 'defaultaxescolor', 'white'); set(0, 'defaultaxesxcolor', 'black'); set(0, 'defaultaxesycolor', 'black'); set(0, 'defaultaxeszcolor', 'black'); set(0, 'defaulttextcolor', 'black'); set(0, 'defaultfigurecolor', 'white'); set(0, 'defaultaxescolororder', [0,0,1]); %%%%%%%%%%%%% LINE WIDTHS %%%%%%%%%%%%%%%% set(0, 'defaultaxeslinewidth', 2); set(0, 'defaultlinelinewidth', 3); set(0, 'defaultpatchlinewidth', 1); %%%%%%%%%%%%% TICK MARKS %%%%%%%%%%%%%%%%% set(0, 'defaultaxesxminortick', 'on'); set(0, 'defaultaxesyminortick', 'on'); set(0, 'defaultaxestickdir', 'in'); set(0, 'defaultaxesticklength', [0.02, 0.05]);%originally [0.01 0.025] %%%%%%%%%%%%% FONTS %%%%%%%%%%%%%%%%%%%%%% set(0, 'defaultAxesFontName', 'Helvetica'); set(0, 'defaultAxesFontSize', 16); set(0, 'defaultAxesFontWeight', 'bold'); set(0, 'defaultTextFontName', 'Helvetica'); set(0, 'defaultTextFontSize', 16); set(0, 'defaultTextFontWeight', 'bold'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% disp('Presentation defaults set.'); figure; plot(x, y); title('This was made with presentation defaults'); xlabel('{\itx}'); ylabel('{\ity}'); %HINT: Make a paper_defaults.m and a presentation_defaults.m and save them %in your matlab directory %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%% TYPES OF FIGURES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %REMARK: Every figure should have a point. % %REMARK: Choose the right type of figure for the data you're presenting %and the point you're trying to make. % %REMARK: Many readers will only read your paper's abstract and look at %the figures. %% %EXAMPLE: Imagine you wish to introduce two time series that you will be %analyzing. You want to show the variables measured; the length of the %time series; and the magnitudes, fluctuations, and phases of the %variables. Then use plotyy. %%%%%%%%%%%%%%%%%%%%%%%%% PLOTYY %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clc; close all; clear all; t = now - linspace(0,30,1001); DO = 3 + 2 * sin(2*pi*t) + .05 * randn(size(t)) - (t - t(1)) * 0.03; Temp = 15 + 2 * sin(2*pi*(t-1/24)) + 2 * randn(size(t)) + (t - t(1)) * 0.1; [AX,H1,H2] = plotyy(t, DO, t, Temp); linkaxes(AX, 'x'); xlim([min(t), max(t)]); %work on AX(1) set(H1, 'color', 'red'); set(AX(1), 'ycolor', 'red'); axes(AX(1)); ylabel('Dissolved Oxygen (\mug/l)'); set(AX(1), 'xtick', []); %work on AX(2) set(H2, 'color', 'black'); set(AX(2), 'ycolor', 'black'); axes(AX(2)); xlabel('Date'); ylabel(['Temperature (' char(176) 'C)']); set(AX(2),'xticklabel', datestr(get(gca, 'xtick'), 'mm/dd')); %save the figure hgsave('do_series.fig') %save as a .fig to allow future modifications print -depsc2 fig.do_series.eps;%save as an eps file for publication %or figureName = 'fig.do_series2.eps'; print('-depsc2', figureName);%another way to save as an eps file %REMARK: Always check the .eps file. It can be very different from what's %shown in the matlab figure window.