Matplotlib: use 2 datasets different length datasets to plot x and y
Question: I've been trying to use file gages.txt for x axis values and file data.rei for
y axis values. I've run into an error because the two file are not the same
length. I want to plot a separate graph for each time the `pestID` in
`gages.txt` matches the Name in `data.rei`.

Here is an excerpt of gages.txt

gage date pestID Measurement(cfd) weight group 06459175 10/1/1993 devfl1
12788474.59 1.40309E-06 devflux 06459175 11/1/1993 devfl2 12208086.39
1.40309E-06 devflux 06459175 12/1/1993 devfl3 13559062.49 1.40309E-06 devflux
06459175 1/1/1994 devfl4 12419465.45 1.40309E-06 devflux 06459175 2/1/1994
devfl5 12070242.32 1.40309E-06 devflux 06459175 3/1/1994 devfl6 14298632.14
1.40309E-06 devflux 06459175 4/1/1994 devfl7 13348094.29 1.40309E-06 devflux
06459175 5/1/1994 devfl8 13164766.46 1.40309E-06 devflux 06459175 6/1/1994
devfl9 12737079.24 1.40309E-06 devflux 06459175 7/1/1994 devfl10 12663994.86
1.40309E-06 devflux 06459175 8/1/1994 devfl11 13164849.87 1.40309E-06 devflux
06459200 10/1/1966 devfl253 17304667.25 1.20897E-06 devflux 06459200 11/1/1966
devfl254 16790039.95 1.20897E-06 devflux 06459200 12/1/1966 devfl255
13414046.27 1.20897E-06 devflux 06459200 1/1/1967 devfl256 13146007.51
1.20897E-06 devflux 06459200 2/1/1967 devfl257 15104020.28 1.20897E-06 devflux
06459200 3/1/1967 devfl258 16573573.51 1.20897E-06 devflux 06459200 4/1/1967
devfl259 18090091.13 1.20897E-06 devflux 06459200 5/1/1967 devfl260
18112268.35 1.20897E-06 devflux 06459200 6/1/1967 devfl261 16365348.96
1.20897E-06 devflux 06459200 7/1/1967 devfl262 16490349.44 1.20897E-06 devflux
06459200 8/1/1967 devfl263 16167208.44 1.20897E-06 devflux 06459200 9/1/1967
devfl264 15875425.16 1.20897E-06 devflux 06776500 7/1/1961 devfl6725
27784610.2 7.33613E-07 devflux 06776500 8/1/1961 devfl6726 27008782.61
7.33613E-07 devflux 06776500 9/1/1961 devfl6727 27727258.45 7.33613E-07
devflux 06776500 10/1/1961 devfl6728 30051668.13 7.33613E-07 devflux 06776500
11/1/1961 devfl6729 28593805.65 7.33613E-07 devflux 06776500 12/1/1961
devfl6730 20188155.91 7.33613E-07 devflux 06776500 1/1/1962 devfl6731
18106275.83 7.33613E-07 devflux 06776500 2/1/1962 devfl6732 19852941.78
7.33613E-07 devflux 06776500 3/1/1962 devfl6733 26060013.78 7.33613E-07
devflux

Here is excerpt of data.rei:

Name Group Measured Modelled Residual Weight pdwl1 pdwls 2083.620 2089.673
-6.052805 9.4067000E-04 pdwl2 pdwls 2186.748 2199.771 -13.02284 8.9630800E-04
pdwl3 pdwls 2150.983 2160.259 -9.275730 9.1121100E-04 pdwl4 pdwls 2133.283
2142.970 -9.686504 9.1877100E-04 pdwl5 pdwls 2241.741 1769.331 472.4097
8.7432100E-04 pst_1 devwls 2191.200 2094.658 96.54200 1.000000  
pst_2 devwls 2194.160 2094.070 100.0900 1.000000  
pst_3 devwls 2190.790 2093.375 97.41500 1.000000  
pst_4 devwls 2191.700 2092.671 99.02900 1.000000  
pst_5 devwls 2188.260 2092.739 95.52100 1.000000  
devfl1 devflux 1.2788475E+07 1.2199410E+07 589064.6 1.4030900E-06 devfl2
devflux 1.2208086E+07 1.2044727E+07 163359.4 1.4030900E-06 devfl3 devflux
1.3559062E+07 1.1423958E+07 2135104. 1.4030900E-06 devfl4 devflux
1.2419465E+07 1.1141419E+07 1278046. 1.4030900E-06 devfl5 devflux
1.2070242E+07 1.0925833E+07 1144409. 1.4030900E-06 devfl10673 devflux
1.5491064E+07 1.0987895E+08 -9.4387886E+07 3.3832500E-07 devfl10674 devflux
1.4034349E+07 1.0585104E+08 -9.1816691E+07 3.3832500E-07 devfl10675 devflux
1.8542658E+07 1.0808722E+08 -8.9544562E+07 3.3832500E-07 devfl10676 devflux
2.6080914E+07 1.1146742E+08 -8.5386506E+07 3.3832500E-07 devfl10677 devflux
2.7600680E+07 1.1286638E+08 -8.5265700E+07 3.3832500E-07 devfl10678 devflux
5.7568459E+07 1.2289897E+08 -6.5330511E+07 3.3832500E-07 devfl10679 devflux
7.9939784E+07 1.2019735E+08 -4.0257566E+07 3.3832500E-07 devfl10772 devflux
5.8896718E+07 1.3656509E+08 -7.7668372E+07 3.3832500E-07 devfl10773 devflux
9.1145662E+07 1.3911792E+08 -4.7972258E+07 3.3832500E-07 devfl10774 devflux
7.6386027E+07 1.3618379E+08 -5.9797763E+07 3.3832500E-07 devfl10775 devflux
8.6729650E+07 1.5717141E+08 -7.0441760E+07 3.3832500E-07 devfl10776 devflux
1.3065667E+08 1.5286262E+08 -2.2205948E+07 3.3832500E-07

The basic loop i want to do is "if data['Name'] == gages['pestID'], then plot
gages['date'] on x-axis and data['Measured'] on y axis]"

Here is my script:

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    data = np.genfromtxt('data.rei', dtype=None, names=True, skip_header=6)
    gages = np.genfromtxt('gages.txt', dtype=None, names=True, 
        delimiter=('\t'), autostrip=True, usecols=(0, 1, 2))
    
    font = {'size'   : 10,}
    
    #-----Dev BFs__________________________________
    #plt.rc('axes', color_cycle=['r'])
    #for gages['gage'] in gages:
    if gages['pestID'] == data['Name']:
        plt.scatter(gages['date'], data['Measured'],gages['date'], data['Modelled'])
        plt.legend('Measured','Modelled')
        #plt.plot([0,4000],[0,4000])
        plt.xlabel('date', fontdict=font)
        plt.ylabel('flux (cfd)', fontdict=font)
        plt.title.gages(['gage'], fontdict=font) 
        #plt.xlim(1000,4000)
        #plt.ylim(-2000,4000)
        plt.show()
    else:
        print 'no match'
    

Here is the error:

    
    
    ValueError  Traceback (most recent call last)
    C:\Program Files\Enthought\Canopy\App\appdata\canopy-1.1.0.1371.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
        174             else:
        175                 filename = fname
    --> 176             exec compile(scripttext, filename, 'exec') in glob, loc
        177     else:
        178         def execfile(fname, *where):
    
    C:\From_LT017_old D drive\Projects\ELM\FY14\python\elm3_1-4 devFlux plot from rei.py in <module>()
         13 #plt.rc('axes', color_cycle=['r'])
         14 #for gages['gage'] in gages:
    ---> 15 if gages['pestID'] == data['Name']:
         16     plt.scatter(gages['date'], data['Measured'],gages['date'], data['Modelled'])
         17     plt.legend('Measured','Modelled')
    
    ValueError: shape mismatch: objects cannot be broadcast to a single shape
    

I am perplexed because i thought the loop would take care of this problem.


Answer: I had to guess that for every row of `gages`, `pestID` is unqiue, and that for
every row of `data`, `Name` is unique.

Using your data:

    
    
    gages_txt = """gage date    pestID  Measurement(cfd)    weight  group
    06459175    10/1/1993   devfl1  12788474.59 1.40309E-06 devflux
    06459175    11/1/1993   devfl2  12208086.39 1.40309E-06 devflux
    06459175    12/1/1993   devfl3  13559062.49 1.40309E-06 devflux
    06459175    1/1/1994    devfl4  12419465.45 1.40309E-06 devflux
    06459175    2/1/1994    devfl5  12070242.32 1.40309E-06 devflux
    06459175    3/1/1994    devfl6  14298632.14 1.40309E-06 devflux
    06459175    4/1/1994    devfl7  13348094.29 1.40309E-06 devflux
    06459175    5/1/1994    devfl8  13164766.46 1.40309E-06 devflux
    06459175    6/1/1994    devfl9  12737079.24 1.40309E-06 devflux
    06459175    7/1/1994    devfl10 12663994.86 1.40309E-06 devflux
    06459175    8/1/1994    devfl11 13164849.87 1.40309E-06 devflux
    06459200    10/1/1966   devfl253    17304667.25 1.20897E-06 devflux
    06459200    11/1/1966   devfl254    16790039.95 1.20897E-06 devflux
    06459200    12/1/1966   devfl255    13414046.27 1.20897E-06 devflux
    06459200    1/1/1967    devfl256    13146007.51 1.20897E-06 devflux
    06459200    2/1/1967    devfl257    15104020.28 1.20897E-06 devflux
    06459200    3/1/1967    devfl258    16573573.51 1.20897E-06 devflux
    06459200    4/1/1967    devfl259    18090091.13 1.20897E-06 devflux
    06459200    5/1/1967    devfl260    18112268.35 1.20897E-06 devflux
    06459200    6/1/1967    devfl261    16365348.96 1.20897E-06 devflux
    06459200    7/1/1967    devfl262    16490349.44 1.20897E-06 devflux
    06459200    8/1/1967    devfl263    16167208.44 1.20897E-06 devflux
    06459200    9/1/1967    devfl264    15875425.16 1.20897E-06 devflux
    06776500    7/1/1961    devfl6725   27784610.2  7.33613E-07 devflux
    06776500    8/1/1961    devfl6726   27008782.61 7.33613E-07 devflux
    06776500    9/1/1961    devfl6727   27727258.45 7.33613E-07 devflux
    06776500    10/1/1961   devfl6728   30051668.13 7.33613E-07 devflux
    06776500    11/1/1961   devfl6729   28593805.65 7.33613E-07 devflux
    06776500    12/1/1961   devfl6730   20188155.91 7.33613E-07 devflux
    06776500    1/1/1962    devfl6731   18106275.83 7.33613E-07 devflux
    06776500    2/1/1962    devfl6732   19852941.78 7.33613E-07 devflux
    06776500    3/1/1962    devfl6733   26060013.78 7.33613E-07 devflux
    """
    
    data_rei_txt = """Name                 Group          Measured         Modelled         Residual         Weight
     pdwl1                pdwls          2083.620         2089.673        -6.052805        9.4067000E-04
     pdwl2                pdwls          2186.748         2199.771        -13.02284        8.9630800E-04
     pdwl3                pdwls          2150.983         2160.259        -9.275730        9.1121100E-04
     pdwl4                pdwls          2133.283         2142.970        -9.686504        9.1877100E-04
     pdwl5                pdwls          2241.741         1769.331         472.4097        8.7432100E-04
     pst_1                devwls         2191.200         2094.658         96.54200         1.000000
     pst_2                devwls         2194.160         2094.070         100.0900         1.000000
     pst_3                devwls         2190.790         2093.375         97.41500         1.000000
     pst_4                devwls         2191.700         2092.671         99.02900         1.000000
     pst_5                devwls         2188.260         2092.739         95.52100         1.000000
     devfl1               devflux       1.2788475E+07    1.2199410E+07     589064.6        1.4030900E-06
     devfl2               devflux       1.2208086E+07    1.2044727E+07     163359.4        1.4030900E-06
     devfl3               devflux       1.3559062E+07    1.1423958E+07     2135104.        1.4030900E-06
     devfl4               devflux       1.2419465E+07    1.1141419E+07     1278046.        1.4030900E-06
     devfl5               devflux       1.2070242E+07    1.0925833E+07     1144409.        1.4030900E-06
     devfl10673           devflux       1.5491064E+07    1.0987895E+08   -9.4387886E+07    3.3832500E-07
     devfl10674           devflux       1.4034349E+07    1.0585104E+08   -9.1816691E+07    3.3832500E-07
     devfl10675           devflux       1.8542658E+07    1.0808722E+08   -8.9544562E+07    3.3832500E-07
     devfl10676           devflux       2.6080914E+07    1.1146742E+08   -8.5386506E+07    3.3832500E-07
     devfl10677           devflux       2.7600680E+07    1.1286638E+08   -8.5265700E+07    3.3832500E-07
     devfl10678           devflux       5.7568459E+07    1.2289897E+08   -6.5330511E+07    3.3832500E-07
     devfl10679           devflux       7.9939784E+07    1.2019735E+08   -4.0257566E+07    3.3832500E-07
     devfl10772           devflux       5.8896718E+07    1.3656509E+08   -7.7668372E+07    3.3832500E-07
     devfl10773           devflux       9.1145662E+07    1.3911792E+08   -4.7972258E+07    3.3832500E-07
     devfl10774           devflux       7.6386027E+07    1.3618379E+08   -5.9797763E+07    3.3832500E-07
     devfl10775           devflux       8.6729650E+07    1.5717141E+08   -7.0441760E+07    3.3832500E-07
     devfl10776           devflux       1.3065667E+08    1.5286262E+08   -2.2205948E+07    3.3832500E-07
    """
    

And this code:

    
    
    import StringIO
    import pandas as pd
    import matplotlib.pyplot as plt
    gages = pd.read_csv(StringIO.StringIO(gages_txt), sep=None, parse_dates=[1])
    data = pd.read_csv(StringIO.StringIO(data_rei_txt), sep=' ', skipinitialspace=True)
    gages.index=gages['pestID']
    data.index=data['Name']
    joined = gages.join(data)
    joined.index = joined['date']
    joined['Measured'].dropna().plot(style='o')
    plt.show()
    

You get:

![enter image description here](http://i.stack.imgur.com/5GHDL.png)

