JISAO computer helps

MATLAB graphics helps



This page contains figures and short scripts to plot with Matlab. Please contribute scripts and figures for your and other people's reference. The html file is /home/disk/margaret2/jisao/computer_helps/MATLAB_graphics.html . Please let David Warren know if the write permissions are not correct for you to do this. Thank you , Todd Mitchell.


Beginning with version 2.1 of the mapping toolbox (so matlab 7.x), worldlo, worldhi worldmtx and worldmtxmed are no longer a part of the toolbox, and they have been replaced by "shape files" landareas.shp, worldlakes.shp, worldrivers.shp, and worldcities.shp. For consistency, the usalo and usahi files are now available as usastatelo.shp, usastatehi.shp, conus.mat, and greatlakes.mat. The funcion "shaperead" function will read these files. To learn more, go to page 18 of the linked PDF file: PDF Examples of how to modify the following scripts is given in the PDF file.

  • Dan Vimont's mapping toolbox routines
  • continent with gray continent and country outlines
  • shaded continent with contour field
  • writing lots of text outside of the plot
  • Land and ocean masks
  • controlling shading intervals and colors
  • Making movies
  • Plotting an image
    
    

    load worldlo  % A geography data base
    worldlo  % Provides information on what is in the data base
    figure
    axesm( 'eqdcylin','MapLatLimit',[ -40 10 ], 'MapLonLimit', [ -85 -30 ]+360 )
    h = displaym( POline );     % POlitical boundaries and coastlines as lines.
    set( h(1), 'Color', ones(1,3)*0.7 );  % Make the borders and coasts gray.
    set( h(2), 'Color', ones(1,3)*0.7 );
    framem
    
    % Plots some circles and Xs for station locations
    h1 = plotm( raob, 'ko', 'MarkerSize', 10 );
    h2 = plotm( sonet, 'kx', 'MarkerSize', 10 );
    
    
    Todd Mitchell, October 2000.
    
    
    

    I have done this without the toolbox functions.  I'm sure that it can
    be done with the toolbox, but I can't easily tell you how.  If you
    know, please add this information.
    
    % Read in an elevation data set.  Several reside at
    % /home/disk/tao/data/elevation/
    
    clf
    % I shade the continents with "landmask" which was written by Alexis
    % Lau.  I'm sure that the MATLAB toolbox also does this, but I don't
    % know how.  To use Alexis' routine, typing for following into your
    % MATLAB session should work.
    /home/disk/tao/bantzer-work/matlab/lau/link_alexis_scripts.m
    
    landmask( ones(1,3)*0.9 );
    hold on
    
    % The following routines requires the field plotted (the elevations)
    % to be a two-dimensional matrix, and longitude and latitude
    % information ("xmat" and "ymat", respectively) to be of the 
    % same dimensions.  I want to contour the 200 and 1500m elevations.
    [ c, h ] = contour( xmat, ymat, elevmat, [200 1500], 'k-' );
    % Each line segment is described with a "handle".  Change
    % the specification of each line segment so that the lines are gray
    % and thick.
    for ih = 1: length(h)
    if get( h(ih), 'UserData' )==200
    set( h(ih), 'Color', ones(1,3)*0.7 )
    else
    set( h(ih), 'Color', ones(1,3)*0.5, 'LineWidth', 0.4 );
    end
    end
    
    % Plot station locations as circles.
    plot( raob(:,2), raob(:,1), 'ko', 'MarkerSize', 5 );
    
    Todd Mitchell, October 2000
    
    
    How to write lots of text outside of the plot.

    This is not a mapping toolbox issue, but it is very useful!

    axes('position',[0,0,1,1]);  % Define axes for the text.
    % In this case, the axes are for the entire page.
    % Write some text: 
    htext = text(.5,0.1,'blah, blah, blah', 'FontSize', 14  );
    % Specify that the coordinates provided above are for the middle
    % of the text string: 
    set(htext,'HorizontalAlignment','center');
    set(gca,'Visible','off');
    
    axes('position',[0.25,0.3,0.5,0.5]);  % Define axes for the plot or map.
    plot( randn(10,1) );  % Plot something.
    
    Thanks to Meghan Cronin of PMEL for showing me this. Todd Mitchell, November 2000.
    Land and ocean masks using the mapping toolbox.

    % Define something to plot (a matrix of the latitudes).
    xgrid = [ 2.5: 5: 360 ]' ;
    ygrid = [ 87.5: -5: -87.5 ]' ;
    nx = length(xgrid)
    ny = length(ygrid)
    xval = ones(ny,1)*(xgrid');
    yval = ygrid*ones(1,nx);
    
    % Ocean mask
    % Define a map projection, in this case cylindrical.  Specify domain.
    axesm('eqdcylin','MapLatLimit',[ 20 80 ], 'MapLonLimit', [ -170 -50 ]+360 )
    % Shade the field of interest.
    contourfm( yval, xval, yval ); shading flat
    % Apply the mask.
    h3 = displaym(worldlo('oceanmask'));
    set(h3, 'Facecolor', [ 1 1 1 ] );
    set(h3, 'Edgecolor', 'none' );
    caxis( [ -87.5 87.5 ] )
    colorbar
    

    % To mask out everything, but the contiguous U.S., you need to % do a few more things. % There are several matlab supplied data sets: % worldlo, worldhi, oceanlo, usalo, and usahi; and each of these % is comprised of several data structures. To find out the % structures, type either "help data_set_name" or just "data_set_name" % and it will show you the structure names. You will need the data % structures in order to mask out states, countries, and selected % geographical features (the Great Lakes). % For identifying countries to mask, you need "worldlo" (or maybe % "worldhi" would do) load worldlo POpatch % loads the "POpatch" structure from the "worldlo" data set. POtags = {POpatch.tag}; canada = POpatch(find(strcmpi(POtags,'canada'))); greenland = POpatch(find(strcmpi(POtags,'greenland'))); mexico = POpatch(find(strcmpi(POtags,'mexico'))); cuba = POpatch(find(strcmpi(POtags,'cuba'))); % Similarly, to obtain the structure for Alaska load usalo state statetags = {state.tag}; alaska = state(find(strcmpi(statetags,'alaska'))); % Now you can, mask out states and countries: h = displaym([canada, greenland, mexico, cuba, alaska]); set(h, 'FaceColor', [1 1 1 ], 'EdgeColor', [1 1 1 ]);

    % As a bonus, one of the data sets has a Great Lakes structure. load usalo greatlakes g = displaym(greatlakes); set(g,'Facecolor',[0 0 1],'EdgeColor',[0 0 1]); % Land mask clf axesm('eqdcylin','MapLatLimit',[ 20 80 ], 'MapLonLimit', [ -170 -50 ]+360 ) contourfm( yval, xval, yval ); shading flat h3 = displaym(worldlo('POpatch')); set(h3, 'Facecolor', [193 153 103]/256 ); set(h3, 'Edgecolor', 'none' ); caxis( [ -87.5 87.5 ] ) colorbar

    Todd Mitchell, December 2000.
    
    
    Controlling shading intervals and colors.

    
    
    Matlab is quite happy to choose shading intervals and color scheme for you. With a little work, however, you can actually specify the colors and shading intervals yourself.

    
    % The key is that you have to specify the shading intervals in
    % "contourf" (the "f" is for fill), and then you have to restrict the 
    % colormap to include only the colors that you want.
    
    % Define a colormap of 7 colors.  Colors are defined by triplets of
    % numbers for red, green, and blue.  Setting the values to NaN means
    % do not shade.
    b = [ ones(1,3)*NaN; 230 255 255; 160 240 255; 80 180 255; ...
      30 110 250; 10 50 200; 10 50 120 ]/255;
    colormap(b)
    
    % 8 contours are defined.
    [ c, h ] = contourf( xgrid, ygrid, temp, [ 50: 50: 350 ] );
    axis( [ 280 330 -25 15 ] );   % Only plot northern South America.
    % Matlab will draw both the shading and annoying contours.
    % The following little loop will turn off the contours
    % by setting their color to "none."
    for icnt = 1: length(h)
    set( h(icnt), 'EdgeColor', 'none' )
    end
    % The remainder of the script is to delineate the coastline.
    % I read in an elevation data set.
    hold on
    [ c2, h2 ] = contour( x1, y1, elev2, [ -10000: 10000: 10000 ] );
    % Draw the coastline as black.  The default seems to be white.
    for icnt = 1: length(h2)
    set( h2(icnt), 'EdgeColor', zeros(1,3) );
    end
    colorbar( 'vert' )  % Draw a colorbar.
    
    Todd Mitchell, January 2001.
    
    
    Making movies

    "help movie" and "help getframe" will give you all of the details. The following is just the essential code.

    for time = first: last
    clf
    plot the data
    m(time) = getframe;
    end
    To show the movie: movie( m, [ #_times_to_show_movie first:last ], frames_per_second )

    Todd Mitchell, January 2002.


    
    
    Controlling shading intervals and colors.

    
    
    Matlab is quite happy to choose shading intervals and color scheme for you. With a little work, however, you can actually specify the colors and shading intervals yourself.

    
    % The key is that you have to specify the shading intervals in
    % "contourf" (the "f" is for fill), and then you have to restrict the 
    % colormap to include only the colors that you want.
    
    % Define a colormap of 7 colors.  Colors are defined by triplets of
    % numbers for red, green, and blue.  Setting the values to NaN means
    % do not shade.
    b = [ ones(1,3)*NaN; 230 255 255; 160 240 255; 80 180 255; ...
      30 110 250; 10 50 200; 10 50 120 ]/255;
    colormap(b)
    
    % 8 contours are defined.
    [ c, h ] = contourf( xgrid, ygrid, temp, [ 50: 50: 350 ] );
    axis( [ 280 330 -25 15 ] );   % Only plot northern South America.
    % Matlab will draw both the shading and annoying contours.
    % The following little loop will turn off the contours
    % by setting their color to "none."
    for icnt = 1: length(h)
    set( h(icnt), 'EdgeColor', 'none' )
    end
    % The remainder of the script is to delineate the coastline.
    % I read in an elevation data set.
    hold on
    [ c2, h2 ] = contour( x1, y1, elev2, [ -10000: 10000: 10000 ] );
    % Draw the coastline as black.  The default seems to be white.
    for icnt = 1: length(h2)
    set( h2(icnt), 'EdgeColor', zeros(1,3) );
    end
    colorbar( 'vert' )  % Draw a colorbar.
    
    Todd Mitchell, January 2001.
    
    
    Plotting an image

    Sometimes you want a plot where you can pick off data values for individual cells.

    "pcolor" is awful, but you can modify the plot with a lot of work to make it serviceable. In the following I had a table where values range from 0 to 5. I wanted 0 to plotted as white and 5 to be red.

    
    pcolor( [ yr1:yr2 ], [ 1: 62 ], temp' );
    h2 = colorbar( 'vert' )
    set( h2, 'YTickLabel', '' );
    colormap( [ ones(1,3); flipud( prism(5) ) ] );
    set( gca, 'TickDir', 'out' )
    set( get(gca,'Children'), 'EdgeColor', 'none' );  % turns off the grid
    set( gca, 'YLim', [ 0 63 ] );
    set( gca, 'YTick', [ 0:5:60 ] );
    set( gca, 'XLim', [ 1848 2012 ] );
    set( gca, 'XTick', [ 1850:10:2010 ] );
    set( gca, 'XTickLabel', [ '1850'; '    '; '1870'; ...
      '    '; '1890'; '    '; '1910'; '    '; '1930'; ...
      '    '; '1950'; '    '; '1970'; '    '; '1990'; ...
      '    '; '2010'; '    ' ] );
    hold on
    for xval = 1850:10:2010
    h = plot( [ xval xval ], [ 1 62 ], 'k-' );
    set( h, 'Color', ones(1,3)*0.8 );
    end
    for yval = 5:5:60
    h = plot( [ 1850 2010 ], [ yval yval ], 'k-' );
    set( h, 'Color', ones(1,3)*0.8 );
    end
    ylabel( 'station index', 'FontName', 'Times', 'FontSize', 14 );
    title( '# of June through October months contributing (0-5)', ...
      'FontName', 'Times', 'FontSize', 18 );
    
    
    Todd Mitchell, January 2008
    
    

    Todd Mitchell mitchell@atmos.washington.edu
    January 2008
    JISAO computer helps