Advertisement
Guest User

Untitled

a guest
Dec 28th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.79 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib
  3. matplotlib.use('Agg')
  4. from scipy.interpolate import griddata
  5. from mpl_toolkits.basemap import Basemap, maskoceans
  6. import matplotlib.pyplot as plt
  7. from numpy.random import seed
  8. import shapefile as shp
  9. from matplotlib.collections import LineCollection
  10. from matplotlib import cm
  11. # Set figure size
  12. plt.figure(figsize=(15,15), dpi=80)
  13. # Define map bounds
  14. xMin, xMax = 2.5, 8.0
  15. yMin, yMax = 50.6, 53.8
  16. # Create map
  17. m = Basemap(projection='merc',llcrnrlon=xMin,llcrnrlat=yMin,urcrnrlon=xMax,urcrnrlat=yMax,resolution='h')
  18. m.drawmapboundary(fill_color='#d4dadc',linewidth=0.25)
  19. # m.drawcoastlines(linewidth=0.5,color='#333333')
  20. # Load data
  21. y = [54.325666666667,52.36,53.269444444444,55.399166666667,54.116666666667,53.614444444444,53.491666666667,53.824130555556,52.918055555556,54.03694,52.139722,52.926865008825,54.853888888889,52.317222,53.240026656696,52.642696895243,53.391265948394,52.505333893732,52.098821802977,52.896643913235,52.457270486008,53.223000488316,52.701902388132,52.0548617826,53.411581103636,52.434561756559,52.749056395511,53.123676213651,52.067534268959,53.194409573306,52.27314817052,51.441334059998,51.224757511326,51.990941918858,51.447744494043,51.960667359998,51.969031121385,51.564889021961,51.857593837453,51.449772459909,51.658528382201,51.196699902606,50.905256257898,51.497306260089,yMin,yMin,yMax,yMax]
  22. x = [2.93575,3.3416666666667,3.6277777777778,3.8102777777778,4.0122222222222,4.9602777777778,5.9416666666667,2.9452777777778,4.1502777777778,6.04167,4.436389,4.7811453228565,4.6961111111111,4.789722,4.9207907082729,4.9787572406902,5.3458010937365,4.6029300588208,5.1797058644882,5.383478899702,5.5196324030324,5.7515738887123,5.8874461671401,5.8723225499118,6.1990994508938,6.2589770334531,6.5729701105864,6.5848470019087,6.6567253619722,7.1493220605216,6.8908745111116,3.5958241584686,3.8609657214986,4.121849767852,4.342014,4.4469005114756,4.9259216999194,4.9352386335384,5.1453989235756,5.3770039280214,5.7065946674719,5.7625447234516,5.7617834850481,6.1961067840608,xMin,xMax,xMin,xMax]
  23. z = [4.8,5.2,5.8,5.4,5,5.3,5.4,4.6,5.8,6.3,4.8,5.4,5.3,4.6,5.4,4.4,4.1,5.5,4.5,4.2,3.9,3.7,4.2,3.2,4,3.8,2.7,2.3,3.4,2.5,3.7,5.2,2.9,5.1,3.8,4.4,4.2,3.9,3.8,3.2,2.6,2.8,2.4,3.1]
  24. avg = np.average(z)
  25. z.extend([avg,avg,avg,avg])
  26. x,y = m(x,y)
  27. # target grid to interpolate to
  28. xis = np.arange(min(x),max(x),2000)
  29. yis = np.arange(min(y),max(y),2000)
  30. xi,yi = np.meshgrid(xis,yis)
  31. # interpolate
  32. zi = griddata((x,y),z,(xi,yi),method='cubic')
  33. # Decide on proper values for colour bar (todo)
  34. vrange = max(z)-min(z)
  35. mult = 2
  36. vmin = min(z)-(mult*vrange)
  37. vmax = max(z)+(mult*vrange)
  38. # Draw contours
  39. cs = m.contour(xi, yi, zi, 5, linewidths=0.25, colors='k')
  40. cs = m.contourf(xi, yi, zi, 5,vmax=vmax,vmin=vmin,cmap=plt.get_cmap('jet'))
  41. # Plot seas from shapefile
  42. sf = shp.Reader(r'/DIR_TO_SHP/shapefiles/northsea')
  43. shapes = sf.shapes()
  44. print shapes[0].parts
  45. records = sf.records()
  46. ax = plt.subplot(111)
  47. for record, shape in zip(records,shapes):
  48.     lons,lats = zip(*shape.points)
  49.     data = np.array(m(lons, lats)).T
  50.     print len(shape.parts)
  51.     if len(shape.parts) == 1:
  52.         segs = [data,]
  53.     else:
  54.         segs = []
  55.         for i in range(1,len(shape.parts)):
  56.             index = shape.parts[i-1]
  57.             index2 = shape.parts[i]
  58.             segs.append(data[index:index2])
  59.         segs.append(data[index2:])
  60.     lines = LineCollection(segs,antialiaseds=(1,),zorder=3)
  61.     lines.set_facecolors('#abc0d3')
  62.     lines.set_edgecolors('red')
  63.     lines.set_linewidth(0.5)
  64.     ax.add_collection(lines)
  65. # Plot France from shapefile
  66. sf = shp.Reader(r'/DIR_TO_SHP/shapefiles/FRA_adm0')
  67. shapes = sf.shapes()
  68. records = sf.records()
  69. ax = plt.subplot(111)
  70. for record, shape in zip(records,shapes):
  71.     lons,lats = zip(*shape.points)
  72.     data = np.array(m(lons, lats)).T
  73.     if len(shape.parts) == 1:
  74.         segs = [data,]
  75.     else:
  76.         segs = []
  77.         for i in range(1,len(shape.parts)):
  78.             index = shape.parts[i-1]
  79.             index2 = shape.parts[i]
  80.             segs.append(data[index:index2])
  81.         segs.append(data[index2:])
  82.     lines = LineCollection(segs,antialiaseds=(1,))
  83.     lines.set_facecolors('#fafaf8')
  84.     lines.set_edgecolors('#333333')
  85.     lines.set_linewidth(0.5)
  86.     ax.add_collection(lines)
  87. # Plot Belgium from shapefile
  88. sf = shp.Reader(r'/DIR_TO_SHP/shapefiles/BEL_adm0')
  89. shapes = sf.shapes()
  90. records = sf.records()
  91. ax = plt.subplot(111)
  92. for record, shape in zip(records,shapes):
  93.     lons,lats = zip(*shape.points)
  94.     data = np.array(m(lons, lats)).T
  95.     if len(shape.parts) == 1:
  96.         segs = [data,]
  97.     else:
  98.         segs = []
  99.         for i in range(1,len(shape.parts)):
  100.             index = shape.parts[i-1]
  101.             index2 = shape.parts[i]
  102.             segs.append(data[index:index2])
  103.         segs.append(data[index2:])
  104.     lines = LineCollection(segs,antialiaseds=(1,))
  105.     lines.set_facecolors('#fafaf8')
  106.     lines.set_edgecolors('#333333')
  107.     lines.set_linewidth(0.5)
  108.     ax.add_collection(lines)
  109. # Plot Germany from shapefile
  110. sf = shp.Reader(r'/DIR_TO_SHP/shapefiles/DEU_adm0')
  111. shapes = sf.shapes()
  112. records = sf.records()
  113. ax = plt.subplot(111)
  114. for record, shape in zip(records,shapes):
  115.     lons,lats = zip(*shape.points)
  116.     data = np.array(m(lons, lats)).T
  117.     if len(shape.parts) == 1:
  118.         segs = [data,]
  119.     else:
  120.         segs = []
  121.         for i in range(1,len(shape.parts)):
  122.             index = shape.parts[i-1]
  123.             index2 = shape.parts[i]
  124.             segs.append(data[index:index2])
  125.         segs.append(data[index2:])
  126.     lines = LineCollection(segs,antialiaseds=(1,))
  127.     lines.set_facecolors('#fafaf8')
  128.     lines.set_edgecolors('#333333')
  129.     lines.set_linewidth(0.5)
  130.     ax.add_collection(lines)
  131. # Finish plot
  132. plt.axis('off')
  133. plt.savefig("test2.png",bbox_inches='tight',pad_inches=0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement