Overlaying a boxplot onto a time series figure

Discussion in 'Python' started by Jamie Mitchell, Jun 6, 2014.

  1. Hi there,

    I would like to overlay some boxplots onto a time series.

    I have tried pylab.hold(True) in between the two plots in my code but this hasn't worked.

    The problem is that the x-axes of the boxplots and the time series are not the same.

    Code for time series:

    python2.7
    import netCDF4
    import matplotlib.pyplot as plt
    import numpy as np

    swh_Q0_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_annavg1D.nc','r')
    hs_Q0_con_sw=swh_Q0_con_sw.variables['hs'][:]
    year_con=swh_Q0_con_sw.variables['year'][:]
    swh_Q3_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q3/swh/controlperiod/south_west/swhcontrol_swest_annavg1D.nc','r')
    hs_Q3_con_sw=swh_Q3_con_sw.variables['hs'][:]
    swh_Q4_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q4/swh/controlperiod/south_west/swhcontrol_swest_annavg1D.nc','r')
    hs_Q4_con_sw=swh_Q4_con_sw.variables['hs'][:]
    swh_Q14_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q14/swh/controlperiod/south_west/swhcontrol_swest_annavg1D.nc','r')
    hs_Q14_con_sw=swh_Q14_con_sw.variables['hs'][:]
    swh_Q16_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q16/swh/controlperiod/south_west/swhcontrol_swest_annavg1D.nc','r')
    hs_Q16_con_sw=swh_Q16_con_sw.variables['hs'][:]
    swh_Q0_fut_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/2050s/south_west/swh2050s_swest_annavg1D.nc','r')
    hs_Q0_fut_sw=swh_Q0_fut_sw.variables['hs'][:]
    year_fut=swh_Q0_fut_sw.variables['year'][:]
    swh_Q3_fut_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q3/swh/2050s/south_west/swh2050s_swest_annavg1D.nc','r')
    hs_Q3_fut_sw=swh_Q3_fut_sw.variables['hs'][:]
    swh_Q4_fut_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q4/swh/2050s/south_west/swh2050s_swest_annavg1D.nc','r')
    hs_Q4_fut_sw=swh_Q4_fut_sw.variables['hs'][:]
    swh_Q14_fut_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q14/swh/2050s/south_west/swh2050s_swest_annavg1D.nc','r')
    hs_Q14_fut_sw=swh_Q14_fut_sw.variables['hs'][:]
    swh_Q16_fut_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q16/swh/2050s/south_west/swh2050s_swest_annavg1D.nc','r')
    hs_Q16_fut_sw=swh_Q16_fut_sw.variables['hs'][:]

    fit_Q0_con_sw=np.polyfit(year_con,hs_Q0_con_sw,1)
    fit_fn_Q0_con_sw=np.poly1d(fit_Q0_con_sw)

    plt.plot(year_con,hs_Q0_con_sw,'g.')
    plt.plot(year_con,fit_fn_Q0_con_sw(year_con),'g',label='Q0 no pert')

    fit_Q3_con_sw=np.polyfit(year_con,hs_Q3_con_sw,1)
    fit_fn_Q3_con_sw=np.poly1d(fit_Q3_con_sw)

    plt.plot(year_con,hs_Q3_con_sw,'b.')
    plt.plot(year_con,fit_fn_Q3_con_sw(year_con),'b',label='Q3 low sens')

    fit_Q4_con_sw=np.polyfit(year_con,hs_Q4_con_sw,1)
    fit_fn_Q4_con_sw=np.poly1d(fit_Q4_con_sw)

    plt.plot(year_con,hs_Q4_con_sw,'y.')
    plt.plot(year_con,fit_fn_Q4_con_sw(year_con),'y',label='Q4 low sens')

    fit_Q14_con_sw=np.polyfit(year_con,hs_Q14_con_sw,1)
    fit_fn_Q14_con_sw=np.poly1d(fit_Q14_con_sw)

    plt.plot(year_con,hs_Q14_con_sw,'r.')
    plt.plot(year_con,fit_fn_Q14_con_sw(year_con),'r',label='Q14 high sens')

    fit_Q16_con_sw=np.polyfit(year_con,hs_Q16_con_sw,1)
    fit_fn_Q16_con_sw=np.poly1d(fit_Q16_con_sw)

    plt.plot(year_con,hs_Q16_con_sw,'c.')
    plt.plot(year_con,fit_fn_Q16_con_sw(year_con),'c',label='Q16 high sens')

    fit_Q0_fut_sw=np.polyfit(year_fut,hs_Q0_fut_sw,1)
    fit_fn_Q0_fut_sw=np.poly1d(fit_Q0_fut_sw)

    plt.plot(year_fut,hs_Q0_fut_sw,'g.')
    plt.plot(year_fut,fit_fn_Q0_fut_sw(year_fut),'g')

    fit_Q3_fut_sw=np.polyfit(year_fut,hs_Q3_fut_sw,1)
    fit_fn_Q3_fut_sw=np.poly1d(fit_Q3_fut_sw)

    plt.plot(year_fut,hs_Q3_fut_sw,'b.')
    plt.plot(year_fut,fit_fn_Q3_fut_sw(year_fut),'b')

    fit_Q4_fut_sw=np.polyfit(year_fut,hs_Q4_fut_sw,1)
    fit_fn_Q4_fut_sw=np.poly1d(fit_Q4_fut_sw)

    plt.plot(year_fut,hs_Q4_fut_sw,'y.')
    plt.plot(year_fut,fit_fn_Q4_fut_sw(year_fut),'y')

    fit_Q14_fut_sw=np.polyfit(year_fut,hs_Q14_fut_sw,1)
    fit_fn_Q14_fut_sw=np.poly1d(fit_Q14_fut_sw)

    plt.plot(year_fut,hs_Q14_fut_sw,'r.')
    plt.plot(year_fut,fit_fn_Q14_fut_sw(year_fut),'y')

    fit_Q16_fut_sw=np.polyfit(year_fut,hs_Q16_fut_sw,1)
    fit_fn_Q16_fut_sw=np.poly1d(fit_Q16_fut_sw)

    plt.plot(year_fut,hs_Q16_fut_sw,'c.')
    plt.plot(year_fut,fit_fn_Q16_fut_sw(year_fut),'c')

    plt.legend(loc='best')
    plt.xlabel('Year')
    plt.ylabel('Significant Wave Height annual averages SW England')
    plt.title('Time series of Significant Wave Height')
    plt.show()

    Code for boxplots:

    python2.7
    from pylab import *
    import netCDF4

    data=(hs_Q0_con_sw,hs_Q3_con_sw,hs_Q4_con_sw,hs_Q14_con_sw,hs_Q16_con_sw)

    figure(1)
    boxplot(data)
    labels=('QO no pert','Q3 low sens','Q4 low sens','Q14 high sens','Q16 high sens')
    xticks(range(1,6),labels,rotation=15)
    xlabel('Ensemble Member')
    ylabel('Significant Wave Height Annual Average')
    title('Significant Wave Height SW England 1981-2010')
    show()



    If anybody knows how I could integrate these two plots I would be eternally grateful!

    Thanks,

    Jamie
     
    Jamie Mitchell, Jun 6, 2014
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.