xdenisx

make_scatter

Sep 9th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import psycopg2
  5. from mpl_toolkits.basemap import Basemap
  6. from matplotlib.patches import Polygon
  7.  
  8. def plot_poly(lon1,lat1,lon2,lat2,out_path):
  9.     def draw_screen_poly( lats, lons, m):
  10.         x, y = m( lons, lats )
  11.         xy = zip(x,y)
  12.         poly = Polygon( xy, facecolor='red', alpha=0.4 )
  13.         plt.gca().add_patch(poly)
  14.  
  15.     lats = [ lat1, lat2, lat2, lat1 ]
  16.     lons = [ lon1, lon1, lon2, lon2 ]
  17.  
  18.     plt.clf()
  19.     m = Basemap(projection='npstere',boundinglat=73,lon_0=0,resolution='l')
  20.     m.drawcoastlines()
  21.     m.drawmapboundary()
  22.     draw_screen_poly( lats, lons, m )
  23.    
  24.     plt.savefig(out_path, dpi=150, bbox_inches='tight', pad_inches=0)
  25.     #plt.show()
  26.  
  27. def plot_data(x,y,out_path,title,icor):
  28.     plt.clf()
  29.     fig = plt.figure()
  30.     ax1 = fig.add_subplot(111)
  31.     ## the data
  32.     N=1000
  33.     #x = np.random.randn(N)
  34.     #y = np.random.randn(N)
  35.  
  36.     ## left panel
  37.     ax1.scatter(x,y,color='blue',s=3,edgecolor='none')
  38.     #ax1.set_aspect(1./ax1.get_data_ratio()) # make axes square
  39.     plt.axis((0, 700, 0, 700))
  40.     # Line
  41.     ax1.plot([0,700],[0,700],color='black', alpha=0.8, linewidth=0.5)
  42.     ax1.set_xlabel('Insitu data [km/month]', fontsize=10)
  43.     ax1.set_ylabel('TOPAZ rean. data [km/month]', fontsize=10)
  44.     ax1.set_title(title)
  45.     # Axis
  46.     ax1.set_xlim(0,700)
  47.     ax1.set_ylim(0,700)
  48.     # Corr coef
  49.     ax1.text(0.03, 0.95, 'Correlation: %3.1f' % icor, horizontalalignment='left', verticalalignment='center', transform=ax1.transAxes, fontsize=10)
  50.  
  51.     """
  52.    ## right panel
  53.    ax2 = fig.add_subplot(122)
  54.    props = dict(alpha=0.5, edgecolors='none' )
  55.  
  56.    handles = []
  57.    colors = ['blue', 'green', 'magenta', 'cyan']
  58.    for color in colors:
  59.        x = np.random.randn(N)
  60.        y = np.random.randn(N)
  61.        s = np.random.randint(50,200)
  62.        handles.append(ax2.scatter(x, y, c=color, s=s, **props))
  63.  
  64.    ax2.set_ylim([-5,11])
  65.    ax2.set_xlim([-5,11])
  66.  
  67.    ax2.legend(handles, colors)
  68.    ax2.grid(True)
  69.    ax2.set_aspect(1./ax2.get_data_ratio())
  70.    """
  71.     #plt.show()
  72.     plt.savefig(out_path, dpi=150, bbox_inches='tight', pad_inches=0)
  73.  
  74. def get_and_plot(lon1,lat1,lon2,lat2,title,out_path):
  75.     conn = psycopg2.connect(database='gis', user='gisuser')
  76.     curs = conn.cursor()
  77.     table_name = 'comparison'
  78.  
  79.     curs.execute("""SELECT date, mag_insitu, mag_model, st_AsText(geom) FROM %s WHERE st_within ( %s.geom, st_geomfromtext('POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))') );""" % (table_name,table_name,lon1,lat1,lon1,lat2,lon2,lat2,lon2,lat1,lon1,lat1) )
  80.  
  81.     x_insitu = np.array([])
  82.     y_model = np.array([])
  83.     for row in curs.fetchall():
  84.         i_date = row[0]
  85.         i_mag_insitu = row[1]
  86.         x_insitu = np.append(x_insitu, float(i_mag_insitu))
  87.         i_mag_model = row[2]
  88.         y_model = np.append(y_model, float(i_mag_model))
  89.         #print i_mag_insitu, i_mag_model
  90.     conn.commit()
  91.     conn.close()
  92.  
  93.     corr = np.corrcoef(x_insitu, y_model)
  94.     cor = corr[0][1]
  95.     print 'Correlation: %s' % cor
  96.     plot_data(x_insitu, y_model, out_path, title, cor)
  97.  
  98. try:
  99.     # FS
  100.     get_and_plot(-30,75,0,85,'Fram Strait','plots/fs.png')
  101.     plot_poly(-30,75,0,85,'plots/map_fs.png')
  102.    
  103.     # Arctic
  104.     get_and_plot(-180,60,180,90,'Arctic','plots/arctic.png')
  105.     #plot_poly(-179,60,179,90,'plots/map_arctic.png')
  106.    
  107.     # Laptev and Siberian area
  108.     get_and_plot(120,75,16,85,'Laptev and Siberain area','plots/laptev.png')
  109.     plot_poly(120,75,16,85,'plots/map_laptev.png')
  110.    
  111.     # Beaufort area
  112.     get_and_plot(-150,75,180,80,'Beaufort area','plots/bea.png')
  113.     plot_poly(-150,75,-180,80, 'plots/map_bea.png')
  114.    
  115.     # Transpolar drift area
  116.     get_and_plot(-60,85,160,90,'Transpolar Arctic drift area','plots/transpolar.png')
  117.     plot_poly(0,85,160,90,'plots/map_transpolar.png')
  118. except:
  119.     print 'Error!'
Advertisement
Add Comment
Please, Sign In to add comment