예제 #1
0
파일: viz.py 프로젝트: qAp/climatools
def plotVS_interest_for_all_cases(dsets, diff_dsets, interest = 'FLNT',
                                  left_ylim = None,
                                  right_ylim = None,
                                  linestyles = None):

    vspairs = [[p.strip() for p in diff_case.split('-')]
               for diff_case in sorted(diff_dsets.keys())]
    
    Nplots = len(vspairs)
    
    fig, axes = plt.subplots(figsize = (11, 3 * Nplots + 2.5), dpi = 300,
                             nrows = Nplots, ncols = 1)
    
    handles, labels = [], []
    
    for ax, vspair in zip([axes] if Nplots == 1 else axes, vspairs):
        x, y = vspair
        
        for model in vspair:
            ax = plot_DataArray(ax, dsets[model][interest],
                                label = '{}'.format(model),
                                colour = linestyles[model]['colour'],
                                linestyle = linestyles[model]['linestyle'],
                                marker = '',
                                ylabel = interest, ylim = left_ylim)
            
        da = diff_dsets[' - '.join(vspair)][interest]
        
        diff_colour = (0.929, 0.329, 0.972)
        ax2 = ax.twinx()
        ax2 = plot_DataArray(ax2, da, label = 'difference',
                             title = '',
                             colour = diff_colour, linestyle = '-', marker = '',
                             ylim = right_ylim)
        ax2.set_ylabel('')
        [label.set_color(diff_colour)
         for label in ax2.yaxis.get_ticklabels()]
        
        ### background shading for daytime and nighttime
        ax2 = daytime_nighttime_shading(ax2, da.coords['time'].to_pandas().index,
                                        hour_daystart = 6, hour_nightstart = 18)
        
        ## collect handles and labels for legend later
        handles1, labels1 = ax.get_legend_handles_labels()
        handles2, labels2 = ax2.get_legend_handles_labels()
        handles.extend(handles1 + handles2)
        labels.extend(labels1 + labels2)
        
    ## Make 1 legend for whole figure
    uhandles, ulabels = misc.any_unique_labels(handles, labels)
    uhandles, ulabels = zip(*sorted(zip(uhandles, ulabels),
                                    key = lambda x: x[1]))
    
    fig.suptitle(da.attrs['long_name'])
    
    fig.legend(uhandles, ulabels,
               loc = 'center', ncol = 3,
               bbox_to_anchor = (.45, .91), prop = {'size': 12})
    plt.subplots_adjust(wspace = 0., top = .84, bottom = .15)
    return fig
예제 #2
0
파일: viz.py 프로젝트: qAp/climatools
def plotVS_timeaveraged_interest_for_all_cases(d3sets, diff_d3sets,
                                               interest = 'CLOUD',
                                               xscale = 'linear',
                                               bot_xlim = None, bot_xlabels_rotate = 0.,
                                               top_xlim = None, top_xlabels_rotate = 0.,
                                               yscale = 'linear', ylim = None,
                                               linestyles = None):
    
    
    vspairs = [[p.strip() for p in diff_case.split('-')]
               for diff_case in sorted(diff_d3sets.keys())]
    
    Nplots = len(vspairs)
    
    fig, axes = plt.subplots(figsize = (9, 9), dpi = 300,
                             nrows = 1, ncols = Nplots,
                             sharey = True)
    
    labels, handles = [], []
    
    axes.invert_yaxis() if Nplots ==1 else axes[0].invert_yaxis()
    
    for ax, vspair in zip([axes] if Nplots == 1 else axes, vspairs):
        x, y = vspair
        
        for model in vspair:
            if interest in ['AREI', 'AREL']:
                da = dates.average_over_time(d3sets[model][interest], key = lambda x: x > 0)
            else:
                da = dates.average_over_time(d3sets[model][interest])
            
            # plot each member in the comparison pair
            ax = plot_vertical_profile(ax, da,
                                       label = '{}'.format(model),
                                       colour = linestyles[model]['colour'],
                                       linestyle = linestyles[model]['linestyle'],
                                       xscale = xscale, xlabels_rotate = bot_xlabels_rotate,
                                       yscale = yscale,
                                       xlim = bot_xlim)
            
        ax = axes_beyond_ticks(ax, which = 'x')

        # plot difference on twiny
        diff_colour = (0.929, 0.329, 0.972)
        ax2 = ax.twiny()
        
        da = dates.average_over_time(diff_d3sets[' - '.join(vspair)][interest])
            
        ax2 = plot_vertical_profile(ax2, da,
                                    label = 'difference',
                                    colour = diff_colour, linestyle = '-',
                                    xscale = xscale, xlabels_rotate = top_xlabels_rotate,
                                    yscale = yscale,
                                    xlim = top_xlim)
        
        [ticklabel.set_color(diff_colour) for ticklabel in ax2.xaxis.get_ticklabels()]
        ax2 = axes_beyond_ticks(ax2, which = 'x')
        ax2.axvline(x = 0, color = 'r', alpha = .5)
        
        handles1, labels1 = ax.get_legend_handles_labels()
        handles2, labels2 = ax.get_legend_handles_labels()
        handles.extend(handles1 + handles2)
        labels.extend(labels1 + labels2)
        
    ## Make 1 legend for whole figure
    uhandles, ulabels = misc.any_unique_labels(handles, labels)
    uhandles, ulabels = zip(*sorted(zip(uhandles, ulabels), key = lambda x: x[1]))
    fig.legend(uhandles, ulabels,
               loc = 'center', ncol = 3,
               bbox_to_anchor = (.35, .91), prop = {'size': 14})
    
    fig.suptitle(da.attrs['long_name'])
    
    plt.figtext(x = 0.02, y = .5, s = 'lev [mbar]', rotation = 90.)
    plt.figtext(x = .45, y = 0.08, s = '{} [{}]'.format(interest, da.units))
    plt.figtext(x = .7, y = .91, s = 'difference')
    if hasattr(ax2, 'xaxis_pow'):
        plt.figtext(x = .9, y = 0.08, s = '1e{}'.format(- ax.xaxis_pow))
        plt.figtext(x = .9, y = .91, s = '1e{}'.format(- ax2.xaxis_pow))
    plt.subplots_adjust(wspace = 0., top = .84, bottom = .15)
    return fig