1

Problem: struggling to get subplots with Basemap and pandas group-by function. I've worked around several SO similar questions, the closest being: Trouble with basemap subplots and How to make grouper and axis the same length?. Nothing wrong when I plot individually, or manually set the axis (i.e ax = ax[0], ax [1] etc). I could set axis on a counter, but surely within the loop i, ax = ax[i] should work. Greatly appreciate any help or suggestions. Just can't seem to see it...

Desired outcome: Use the pandas group-by function to plot one basemap for each day, for the selected df column. In this sample, plot 4 basemaps (4 days) for T1. Here is the code so far (apologies, tried to cut excess off but leave enough to follow + txt):

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.mlab import griddata
import pandas as pd
from matplotlib.path import Path
from matplotlib.patches import PathPatch

"""grab data"""
colnames = ['Date','Day','Lat','Lon','Location','T1','T2']
data = pd.read_table('sampledata.txt',sep = '\,',header=0,names=colnames)

"""set up plot"""
ncol = 2 
nrow = 2
fig, ax = plt.subplots(nrows=nrow, ncols=ncol) 

"""define map extent"""
lonmin=31.034
latmin=-29.87226
lonmax=31.06757
latmax=-29.76993

"""Contour plot area"""
lon_l = 31.034458
lon_r = 31.063841
lat_top = -29.772105
lat_bot = -29.866729

"""transform lon / lat coordinates to map projection"""
data['projected_lon'], data['projected_lat'] = map(*(data.Lon.values, data.Lat.values))

"""grid data"""
numcols, numrows = 100, 100
xi = np.linspace(data['projected_lon'].min(), data['projected_lon'].max(), numcols)
yi = np.linspace(data['projected_lat'].min(), data['projected_lat'].max(), numrows)
xi, yi = np.meshgrid(xi, yi)

"""Set Boundary for clip"""
verts = [
        (3413.68264053, 12562.754945193),(2529.90577973,  10518.39901756),( 1836.04976419, 8720.5262622),
        ( 1511.02714665, 7828.73600079),(1164.54391838,  5708.6428411),(1262.72899238,  3495.89719006),
        (1504.13306445,  2376.68833083),(2119.70788849,  1417.91132045),(2828.7976018 ,  1093.77254193),
        (2687.91369608,  812.19595702), (1553.61478351,  738.21055305),(870.98945027,   2237.5816522 ),
        (515.94421668,   3930.99046256),(580.43724377,   6163.91186676),(682.62533323,   6802.055938  ),
        (771.02525829,   7354.17422723),(743.67131922,   7644.57787453), (107.19185881,   7843.3455092 ),   
        (142.88541346,   8263.93842556),(1078.47908569,  8510.110874  ), (1503.57709008,  9693.10793354),
        (2152.06559691,  11221.41133494),(2926.64909117, 12784.761329246),(3413.68264053, 12562.754945193),
        ]
codes = [Path.MOVETO, Path.LINETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.LINETO, Path.LINETO,Path.LINETO,Path.LINETO, Path.LINETO, Path.LINETO,
         Path.LINETO, Path.LINETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.LINETO, Path.LINETO,Path.LINETO,Path.LINETO,Path.LINETO, Path.CLOSEPOLY,
         ]
path = Path(verts, codes)
clip = Path(verts, codes)
clip = PathPatch(clip, transform=ax.transData)


""" Setup Plots """
for i, group in data.groupby(['Day']):
    plt.figure()
    group[i].plot(title=str(i),ax=ax[i])
    map = Basemap(projection='merc', lat_0=-29, lon_0=31,
              resolution = 'l', area_thresh = 0.1,
              llcrnrlon = lonmin, llcrnrlat = latmin ,
              urcrnrlon = lonmax, urcrnrlat =latmax  ,
              ax=ax[i])
    map.drawcoastlines()
    map.fillcontinents(color='burlywood')

    """interpolate on defined grid"""
    x, y, z = data['projected_lon'].values, data['projected_lat'].values, data['T1'].values
    zi = griddata(x, y, z, xi, yi,interp='linear')

    """contour plot + clip"""
    con = map.contourf(xi, yi, zi)
    clip = PathPatch(clip, transform=ax[i].transData)
    for contour in con.collections:
            contour.set_clip_path(clip)

    """Map Extras"""
    map.drawmapboundary(fill_color = 'white')
    map.scatter(
       data['projected_lon'],
       data['projected_lat'],
       color='#545454',
       edgecolor='#ffffff',
       vmin=zi.min(), vmax=zi.max(), zorder=4)

    """Finally Plot all figures generated"""
    plt.show()

Sample text file:

Date,Day,Lat,Lon,Location,T1,T2
27-Feb-15,1,-29.86198,31.04568,A1,7,4
27-Feb-15,1,-29.86151,31.0472,A2,4,22
27-Feb-15,1,-29.86098,31.04907,A3,9,24
27-Feb-15,1,-29.85482,31.04243,A4,2,17
27-Feb-15,1,-29.8547,31.04394,A5,2,29
27-Feb-15,1,-29.8544,31.04598,A6,10,27
27-Feb-15,1,-29.8416,31.03864,A7,8,11
27-Feb-15,1,-29.84162,31.04041,A8,1,13
27-Feb-15,1,-29.84161,31.04219,A9,1,14
27-Feb-15,1,-29.83229,31.03868,A10,14,53
27-Feb-15,1,-29.83246,31.04055,A11,8,10
27-Feb-15,1,-29.83256,31.04251,A12,1,13
27-Feb-15,1,-29.82418,31.03922,A13,46,71
27-Feb-15,1,-29.82448,31.04116,A14,4,53
27-Feb-15,1,-29.82461,31.04331,A15,7,5
27-Feb-15,1,-29.81028,31.04301,A16,39,29
27-Feb-15,1,-29.81083,31.0449,A17,12,15
27-Feb-15,1,-29.81114,31.0467,A18,2,9
27-Feb-15,1,-29.79983,31.04648,A19,30,15
27-Feb-15,1,-29.80046,31.04805,A20,37,38
27-Feb-15,1,-29.80078,31.04983,A21,4,23
27-Feb-15,1,-29.78945,31.05083,A22,11,63
27-Feb-15,1,-29.79008,31.05287,A23,5,47
27-Feb-15,1,-29.79047,31.05408,A24,4,15
27-Feb-15,1,-29.78094,31.05577,A25,6,37
27-Feb-15,1,-29.78131,31.05677,A26,13,41
27-Feb-15,1,-29.78178,31.05827,A27,12,46
27-Feb-15,1,-29.77251,31.06032,A28,14,64
27-Feb-15,1,-29.77316,31.0618,A29,5,36
27-Feb-15,1,-29.77348,31.06291,A30,12,45
27-Feb-15,1,-29.86373,31.05944,A31,0,0
27-Feb-15,1,-29.865902,31.058159,A32,0,0
27-Feb-15,1,-29.810559,31.035082,A33,0,0
27-Feb-15,1,-29.866335,31.049232,A34,7,4
28-Feb-15,2,-29.86198,31.04568,A1,3,2
28-Feb-15,2,-29.86151,31.0472,A2,1,2
28-Feb-15,2,-29.86098,31.04907,A3,2,2
28-Feb-15,2,-29.85482,31.04243,A4,62,490
28-Feb-15,2,-29.8547,31.04394,A5,1,1
28-Feb-15,2,-29.8544,31.04598,A6,5,28
28-Feb-15,2,-29.8416,31.03864,A7,1,23
28-Feb-15,2,-29.84162,31.04041,A8,1,46
28-Feb-15,2,-29.84161,31.04219,A9,1,6
28-Feb-15,2,-29.83229,31.03868,A10,12,44
28-Feb-15,2,-29.83246,31.04055,A11,1,18
28-Feb-15,2,-29.83256,31.04251,A12,1,18
28-Feb-15,2,-29.82418,31.03922,A13,6,25
28-Feb-15,2,-29.82448,31.04116,A14,1,19
28-Feb-15,2,-29.82461,31.04331,A15,4,34
28-Feb-15,2,-29.81028,31.04301,A16,6,55
28-Feb-15,2,-29.81083,31.0449,A17,9,52
28-Feb-15,2,-29.81114,31.0467,A18,9,28
28-Feb-15,2,-29.79983,31.04648,A19,33,20
28-Feb-15,2,-29.80046,31.04805,A20,29,20
28-Feb-15,2,-29.80078,31.04983,A21,21,20
28-Feb-15,2,-29.78945,31.05083,A22,9,20
28-Feb-15,2,-29.79008,31.05287,A23,1,11
28-Feb-15,2,-29.79047,31.05408,A24,1,14
28-Feb-15,2,-29.78094,31.05577,A25,4,6
28-Feb-15,2,-29.78131,31.05677,A26,1,42
28-Feb-15,2,-29.78178,31.05827,A27,1,10
28-Feb-15,2,-29.77251,31.06032,A28,22,82
28-Feb-15,2,-29.77316,31.0618,A29,65,30
28-Feb-15,2,-29.77348,31.06291,A30,1,8
28-Feb-15,2,-29.86373,31.05944,A31,24,39
28-Feb-15,2,-29.865902,31.058159,A32,24,39
28-Feb-15,2,-29.810559,31.035082,A33,70,17000
28-Feb-15,2,-29.866335,31.049232,A34,3,2
01-Mar-15,3,-29.86198,31.04568,A1,1,1
01-Mar-15,3,-29.86151,31.0472,A2,1,1
01-Mar-15,3,-29.86098,31.04907,A3,1,1
01-Mar-15,3,-29.85482,31.04243,A4,1,2
01-Mar-15,3,-29.8547,31.04394,A5,1,5
01-Mar-15,3,-29.8544,31.04598,A6,1,1
01-Mar-15,3,-29.8416,31.03864,A7,1,1
01-Mar-15,3,-29.84162,31.04041,A8,1,1
01-Mar-15,3,-29.84161,31.04219,A9,1,1
01-Mar-15,3,-29.83229,31.03868,A10,4,10
01-Mar-15,3,-29.83246,31.04055,A11,1,5
01-Mar-15,3,-29.83256,31.04251,A12,1,2
01-Mar-15,3,-29.82418,31.03922,A13,6,26
01-Mar-15,3,-29.82448,31.04116,A14,3,7
01-Mar-15,3,-29.82461,31.04331,A15,1,1
01-Mar-15,3,-29.81028,31.04301,A16,70,70
01-Mar-15,3,-29.81083,31.0449,A17,70,70
01-Mar-15,3,-29.81114,31.0467,A18,90,70
01-Mar-15,3,-29.79983,31.04648,A19,27,210
01-Mar-15,3,-29.80046,31.04805,A20,54,600
01-Mar-15,3,-29.80078,31.04983,A21,90,70
01-Mar-15,3,-29.78945,31.05083,A22,27,160
01-Mar-15,3,-29.79008,31.05287,A23,45,250
01-Mar-15,3,-29.79047,31.05408,A24,53,580
01-Mar-15,3,-29.78094,31.05577,A25,19,70
01-Mar-15,3,-29.78131,31.05677,A26,37,180
01-Mar-15,3,-29.78178,31.05827,A27,60,400
01-Mar-15,3,-29.77251,31.06032,A28,6,28
01-Mar-15,3,-29.77316,31.0618,A29,1,32
01-Mar-15,3,-29.77348,31.06291,A30,6,38
01-Mar-15,3,-29.86373,31.05944,A31,3,6
01-Mar-15,3,-29.865902,31.058159,A32,3,6
01-Mar-15,3,-29.810559,31.035082,A33,120,30
01-Mar-15,3,-29.866335,31.049232,A34,1,1
02-Mar-15,4,-29.86198,31.04568,A1,12,11
02-Mar-15,4,-29.86151,31.0472,A2,8,5
02-Mar-15,4,-29.86098,31.04907,A3,6,3
02-Mar-15,4,-29.85482,31.04243,A4,14,14
02-Mar-15,4,-29.8547,31.04394,A5,16,13
02-Mar-15,4,-29.8544,31.04598,A6,3,4
02-Mar-15,4,-29.8416,31.03864,A7,37,27
02-Mar-15,4,-29.84162,31.04041,A8,10,7
02-Mar-15,4,-29.84161,31.04219,A9,9,7
02-Mar-15,4,-29.83229,31.03868,A10,200,30
02-Mar-15,4,-29.83246,31.04055,A11,25,11
02-Mar-15,4,-29.83256,31.04251,A12,52,23
02-Mar-15,4,-29.82418,31.03922,A13,400,43
02-Mar-15,4,-29.82448,31.04116,A14,360,70
02-Mar-15,4,-29.82461,31.04331,A15,420,62
02-Mar-15,4,-29.81028,31.04301,A16,1000,110
02-Mar-15,4,-29.81083,31.0449,A17,1100,100
02-Mar-15,4,-29.81114,31.0467,A18,900,120
02-Mar-15,4,-29.79983,31.04648,A19,6300,170
02-Mar-15,4,-29.80046,31.04805,A20,4800,190
02-Mar-15,4,-29.80078,31.04983,A21,4700,110
02-Mar-15,4,-29.78945,31.05083,A22,1000,68
02-Mar-15,4,-29.79008,31.05287,A23,18,7
02-Mar-15,4,-29.79047,31.05408,A24,21,6
02-Mar-15,4,-29.78094,31.05577,A25,,
02-Mar-15,4,-29.78131,31.05677,A26,20,2
02-Mar-15,4,-29.78178,31.05827,A27,15,10
02-Mar-15,4,-29.77251,31.06032,A28,27,14
02-Mar-15,4,-29.77316,31.0618,A29,34,18
02-Mar-15,4,-29.77348,31.06291,A30,22,6
02-Mar-15,4,-29.86373,31.05944,A31,800,200
02-Mar-15,4,-29.865902,31.058159,A32,800,200
02-Mar-15,4,-29.810559,31.035082,A33,2400,600
02-Mar-15,4,-29.866335,31.049232,A34,12,11
Community
  • 1
  • 1
Clint
  • 347
  • 1
  • 4
  • 10
  • Tell us what the desired and actual outcomes are! (maybe it would be obvious if we did all the cut-and-paste, but we're lazy). – cphlewis Apr 20 '15 at 17:28
  • Hi cphlewis, the intention is to several basemap plots - one for each day of data, for a specific dataframe column (in this case T1 or T2). I would like the groupby function to group by Date, or datetime. Here I eventually sorted out the data into Day 1, Day 2 etc. trying to plot one basemap for each Day. The sample data should result in 4 basemap plots, each showing T1 contourf plots. Eventually I'll extend to more days on the real data. Hope this makes sense. – Clint Apr 20 '15 at 18:43
  • Yup; make it as short as possible and put it in the original post. Until someone solves it, I'd just make individual plots and create a LaTeX or HTML document to arrange them into a page. – cphlewis Apr 20 '15 at 18:49
  • Thanks cphlewis, I've added a brief 'desired outcome'. Although I can manually plot one at a time, the data set has 16 days, with 20 df.columns...(Ts) – Clint Apr 20 '15 at 19:08
  • Ach, that's a lot of code. I notice one shrieker: you're using map as a variable name? Don't; it's a reserved word in the language and wierd things will happen. – cphlewis Apr 20 '15 at 20:15
  • `data['projected_lon'], data['projected_lat'] = map(*(data.Lon.values, data.Lat.values))` is just wrong, I think. I don't think you should call `plt.Figure()` every time you go through your i-loop. *Really* minimize this example, not just Frankenstein it out of other code, and you'll probably fix it yourself. – cphlewis Apr 20 '15 at 20:21

0 Answers0