带有pandas groupby数据问题的底图子图

时间:2015-04-20 09:35:36

标签: python pandas matplotlib matplotlib-basemap

问题:努力使用Basemap和pandas group-by函数获取子图。我已经解决了几个类似的问题,最接近的是:Trouble with basemap subplotsHow to make grouper and axis the same length?。单独绘制或手动设置轴(即ax = ax [0],ax [1]等)时没有错。我可以在计数器上设置轴,但肯定在循环i中,ax = ax [i]应该可以工作。非常感谢任何帮助或建议。似乎无法看到它......

所需结果:使用pandas group-by函数为选定的df列绘制每天的一个底图。在此示例中,为T1绘制4个底图(4天)。 这是迄今为止的代码(道歉,试图削减多余的部分,但留下足以遵循+ 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()

示例文本文件:

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

0 个答案:

没有答案