ArcGIS shape file plot in Matlab

Tian's Hydro Life

ArcGIS shapes such as points and polygon, or even raster files can be plotted in Matlab. Here is an example of reading and plotting HUC4 polygons in Matlab.

First if read in the shape file that’s generated in ArcGIS

%read in shapefile
huc = shaperead('C:HUC4.shp','UseGeoCoords', true, 'BoundingBox', [lonlim', latlim']);

then we get a struct like this, in which most of the columns are attribute table variables, with the first few columns showing the geometrical information about each polygon.


Sometimes the vertices are too many for each polygon, which makes the plotting process extremely slow. Here I am using a tool called DecimatePoly to reduce the total number of vertices 10 times less without making noticeable changes on the shape.

Next we will define the color of the polygon by one of the variables in the attribute table using makesymbolspec:

faceColors = makesymbolspec('Polygon',{'INDEX',[1 lenS],'FaceColor',color_att}); %lenS is the number of polygon %color_att…

View original post 87 more words


Change Jupyter Python notebook default start-up folder

  • Open cmd (or Anaconda Prompt) and run jupyter notebook --generate-config.

This writes a file to C:\Users\username\.jupyter\jupyter_notebook_config.

  • Search for the following line: #c.NotebookApp.notebook_dir = ''

Replace by c.NotebookApp.notebook_dir = '/the/path/to/home/folder/'

** Make sure you uncommented this line,

** Make sure no indentation at the beginning of the line.

** Make sure you use forward slashes ‘/’ in your path and use the full path /home/user/ instead of ~/ for your home directory,



MAY052015,FA,KA32,2015-06-11 11:00:00,P1,NA,NA,NA
APR032014,FA,KA32,2014-04-07 14:00:00,P2,NA,NA,0.04
APR032014,FA,KA32,2014-04-28 10:44:00,P2,NA,NA,0.13
APR032014,FA,KA32,2014-05-05 11:15:00,P2,NA,NA,0.1
APR032014,FA,KA32,2014-05-12 11:29:00,P2,NA,NA,0.14
APR032014,FA,KA32,2014-05-15 15:59:00,P2,NA,NA,NA
APR032014,FA,KA32,2014-05-16 07:44:00,P2,NA,NA,0.5
APR032014,FA,KA32,2014-05-19 09:14:00,P2,NA,NA,0.12
DEC162014,FA,KA32,2014-12-31 12:00:00,P2,NA,NA,0.12

How do I sort first by the 5th field then by date ? First, it will sort 5th column (p%d) and then 4th column (time).

$ sort -t',' -gk5.2n -gk4n data.file.txt


  • -t defines delimiter, in this case, comma
  • -gk5.2n : Sort the 5th field numerically. The option -k is -key, -n is  numerically, 5.2n tells the program to sort the 2nd character of the 5th column.

Run Python in Unix

The environment variable PYTHONPATH is actually only added to the list of locations Python searches for modules. You can print out the full list in the terminal like this:

python -c "import sys; print sys.path"

Or if want the output in the UNIX directory list style (separated by :) you can do this:

python -c "import sys; print ':'.join(x for x in sys.path if x)"

Which will output something like this:

/python2.7/lib-old:/usr/lib/python2.7/lib- dynload:/usr/local/lib/python2.7/dist-

Python Plotting: Broken Axis

ax = plt.subplot(1, 1, 1) 

# Divide the plot into 1 * 9 grids
gs = GridSpec(1, 9)

# Adjust margins of subplots
gs.update(hspace=0.4,wspace=0.1, right=0.8, left=0.1, bottom=0.1, top=0.1)

# Four subplots share the same frame 
ax1 = plt.subplot(gs[0, 0],sharey=ax)
plt.ylabel('Temp. ($^{o}C$)', fontsize=14., labelpad=5)
ax1.plot_date(DailyObs['2003'].index.to_pydatetime(), DailyObs['2003'], 'b-', label='Obs', linewidth=3, alpha=0.8)
ax1.plot_date(DailySim['2003'].index.to_pydatetime(), DailySim['2003'], 'r-', label='Sim', linewidth=3, alpha=0.8) 

ax2 = plt.subplot(gs[0, 1],sharey=ax)
ax2.plot_date(DailyObs['2003'].index.to_pydatetime(), DailyObs['2003'], 'b-', label='Obs', linewidth=3, alpha=0.8)
ax2.plot_date(DailySim['2003'].index.to_pydatetime(), DailySim['2003'], 'r-', label='Sim', linewidth=3, alpha=0.8) 

# remove the axis spine

ax1.yaxis.tick_left() #hide the right tick
ax2.tick_params(axis='y', which='both',left='off',right='off',labelright='off',labelleft='off')
# arguments to pass plot, just so we don't keep repeating them
d = 0.02
kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False)
ax1.plot((1-d,1+d),(-d,+d), **kwargs) # top-left diagonal
ax1.plot((1-d,1+d),(1-d,1+d), **kwargs) # bottom-left diagonal

kwargs.update(transform=ax2.transAxes) # switch to the bottom axes
ax2.plot((-d,d),(-d,+d), **kwargs) # top-right diagonal
ax2.plot((-d,d),(1-d,1+d), **kwargs) # bottom-right diagonal