Example #1
0
def plot_amplitude_timeseries(amplitudes, station, channel):
    """
    Make a plot of the amplitude time series and save it with a unique
    filename that will be overwritten if the script is run again later.
    (i.e. not having time in the name)
    """
    fig = plt.figure(figsize=(10, 7.5))
    ax1 = plt.subplot(111)

    # Set labels and tick sizes
    ax1.set_xlabel(r'Time', fontsize=16)
    ax1.set_ylabel(r'Relative Seismic Noise', fontsize=16)

    # Turn off top and right tick marks
    ax1.get_xaxis().tick_bottom()
    ax1.get_yaxis().tick_left()

    # Make ticks readable
    ax1.tick_params(axis='both', which='major', labelsize=14)

    # Turn off top and right splines
    ax1.spines['top'].set_visible(False)
    ax1.spines['right'].set_visible(False)

    # Plotting
    ax1.plot(amplitudes[:, 0], amplitudes[:, 1] / np.min(amplitudes[:, 1]))

    # Setup the x axis time ticking
    ax1.xaxis.set_major_formatter(DateFormatter('%m/%d'))
    ax1.xaxis.set_minor_formatter(DateFormatter('%HZ'))
    ax1.xaxis.set_major_locator(DayLocator())
    ax1.xaxis.set_minor_locator(HourLocator(range(2, 24, 2)))

    # Add a title
    plt.title('Station {} {}'.format(station, channel), fontsize=18)

    # Save the figure
    plt.savefig('plots/seismic_amplitude_{}_{}.png'.format(station, channel),
                bbox_inches='tight')
Example #2
0
    def _plot_base(self):
        f,ax = plt.subplots(figsize=(10,6))


        # Sun! ##############################
        t = self.tonight.obstime.value
        ax.hlines(20,t[0],t[-1],linestyle='--',color='white',linewidth=3)
        sunaltazs_tonight = get_sun(self.tonight.obstime).transform_to(self.tonight)
        self.plot_vis_obj(sunaltazs_tonight,ax =ax,
                     linestyle='dashed',label = 'Sun',color='orange',linewidth=10,zorder=6)

        plt.fill_between(t, 0, 90,
                         sunaltazs_tonight.alt < -0*u.deg, color='midnightblue', zorder=0)
        plt.fill_between(t, 0, 90,
                         sunaltazs_tonight.alt < -18*u.deg, color='k', zorder=0)
        plt.fill_between(t, 0, 90,
                         sunaltazs_tonight.alt >= 0*u.deg, color='deepskyblue', zorder=0)
        plt.fill_between(t,0,20,color='lightgrey',zorder=3)
        ####################################

        xloc = ax.xaxis.set_major_locator(HourLocator(byhour=range(0,24,1)))
        hours_fmt = DateFormatter('%H')
        ax.xaxis.set_major_formatter(hours_fmt)

        # LEGEND

        # Axes Limits and Labels
        plt.grid(color='w',alpha=0.7)
        ax.set_xlabel('Time (UT)',fontsize=16)
        ax.set_ylim(0,90)
        ax.set_ylabel('Altitude (deg)',fontsize=16)
        plt.tick_params(which='both',direction='inout')
        plt.title('NTT Visibility ' + datetime.strftime(sunaltazs_tonight.obstime[0].to_datetime(),"%Y-%m-%d"),
                                                       fontsize=18)
        fontP = FontProperties()
        fontP.set_size('small')
        ax.legend(title="Objects", prop=fontP,loc='upper center', bbox_to_anchor=(0.9, 1),
                  ncol=1, fancybox=True, shadow=True)
def visualize_column_24hour(path_to_data, column_name):
    df = pd.read_csv(path_to_data)
    start_points = np.arange(0, len(df.index), 96)
    sp = start_points[random.randint(0, len(start_points) - 1)]
    df = df[sp:sp + day + 1]

    df = df.loc[:, (df != 0).any(axis=0)]

    customdate = datetime.datetime(2016, 1, 1, 0, 0)

    y = df[column_name]
    x = [
        customdate + datetime.timedelta(minutes=15 * i) for i in range(len(y))
    ]

    ax = plt.subplot()

    # removing top and right borders
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)

    # adds major gridlines
    ax.grid(color='grey', linestyle='-', linewidth=0.25, alpha=0.5)

    ax.plot(x, y, color='black', linewidth=0.6)
    ax.xaxis.set_major_locator(HourLocator(interval=3))
    ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
    ax.xaxis.set_ticks([
        customdate + datetime.timedelta(hours=i * 3)
        for i in range(int(24 / 3) + 1)
    ])
    ax.set_xlabel("Time (15-min interval)")
    ax.set_ylabel("Energy use [kWh]")

    # beautify the x-labels
    # plt.gcf().autofmt_xdate()

    plt.show()
Example #4
0
 def apply_date_xticks(self, ax=None, major_unit='week', minor_unit='day', major_format="%Y-%m-%d", xdata=None):
     """
     Params:
     major_unit: major locator unit, could be {month, day, week, hour, minute}
     minor_unit: minor locator unit,
     """
     locators = {
         "month": MonthLocator(),
         "month3": MonthLocator(bymonth=[1,4,7,10]),
         "week": WeekdayLocator(MONDAY),
         "day": DayLocator(),
         "hour": HourLocator(),
         "minute": MinuteLocator
     }
     ax = plt.gca() if ax is None else ax
     ax.xaxis.set_major_locator(locators[major_unit])
     if minor_unit is not None:
         ax.xaxis.set_minor_locator(locators[minor_unit])
     ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
     ax.margins(x=0)
     plt.setp(plt.gca().get_xticklabels(), fontsize=self.fontsize, rotation=30, horizontalalignment='right')
     if xdata is not None:
         return self.__try_convert_as_mdate(xdata)
Example #5
0
def pvpcplot_tarifas_hora(df, ax=None, show=True, ymax=None, fs=FIGSIZE):
    df = _prep_pvpc_data_for_plot_web_esios(df)
    if ax is None:
        fig, ax = plt.subplots(figsize=fs)
    # sns.set_style("whitegrid")
    for k in TARIFAS:
        ax.plot(df.index,
                df[k].values,
                color=TARIFAS_COL[k],
                label=TARIFAS_DESC[k],
                lw=4)
    if ymax is not None:
        ax.set_ylim([0, ymax])
    if len(df) < 30:
        ax.xaxis.set_major_formatter(DateFormatter('%H'))
        ax.xaxis.set_major_locator(HourLocator(
            byhour=range(len(df))))  # byhour=4)) #MultipleLocator(4)), tz=TZ
    ax.grid(axis='x', b='off')
    ax.grid(axis='y', color='grey', linestyle='--', linewidth=.5)
    ax.set_axisbelow(False)
    ax.legend(loc=0, fontsize='large', frameon=True, framealpha=.8)
    if show:
        plt.show()
Example #6
0
def plot_battery(folder,directory,batt_settings,df_batterydata,battery=None):
    #Battery physical data
    list_batt = list(batt_settings['battery_name'])
    if not battery:
        battery = list_batt[0]
    list_batt_inv = []
    for batt in list_batt:
        list_batt_inv += ['Bat_inverter_'+batt[8:]]   

    fig = ppt.figure(figsize=(12,4),dpi=150)   
    ppt.ioff()
    #House load
    ax = fig.add_subplot(111)
    print(df_batterydata.iloc[:5])
    lns1 = ax.plot(df_batterydata['total_batt_load'],'r-',label='Total battery load')
    #lns1 = ax.plot(df_batterydata['SOC_'+battery],'r-',label='Total battery load')
    ax.set_ylabel('kWh')
    ax.set_xlim(xmin=df_batterydata.index[0], xmax=df_batterydata.index[-1])
    
    ax.xaxis.set_major_locator(HourLocator(interval=1))
    #ax.xaxis.set_minor_locator(HourLocator(drange(0, 25, 6)))
    ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
    
    ppt.hlines(0, df_batterydata.index[0], df_batterydata.index[-1], colors='k')
    
    ax2 = ax.twinx()
    ax2.set_ylabel('local retail price')
    lns2 = ax2.plot(df_batterydata['clearing_price'],'bx',label='Realtime WS price')
    
    #Legend
    lns = lns1 + lns2
    labs = [l.get_label() for l in lns]
    L = ax.legend(lns, labs, bbox_to_anchor=(0.3, -0.4), loc='lower left', ncol=1)
    #L.get_texts()[0].set_text('Total system load')
    #L.get_texts()[1].set_text('Total unresponsive system load')
    ppt.savefig(directory+'/'+battery+'_Battload_vs_price.png', bbox_inches='tight')
    return
def temp_ax_format(ax, tminmax, dates):
    ax.text(0.01,
            0.92,
            sunrise_string(loc, dates[0]),
            fontsize=10,
            transform=ax.transAxes)
    ax.set_yticks(
        np.arange(np.round(tminmax[0]) - 3,
                  np.round(tminmax[1]) + 3, 3))
    ax.set_ylim(np.round(tminmax[0]) - 3, np.round(tminmax[1]) + 3)
    ax.yaxis.set_major_formatter(
        FormatStrFormatter('%d' + u'\N{DEGREE SIGN}' + 'C'))

    # x axis lims, ticks, labels
    ax.set_xlim(dates[0], dates[-1])
    ax.xaxis.set_minor_locator(HourLocator(np.arange(0, 25, 6)))  # minor
    ax.xaxis.set_minor_formatter(DateFormatter("%Hh"))
    ax.get_xaxis().set_tick_params(which='minor',
                                   direction='in',
                                   pad=-10,
                                   labelsize=6)
    ax.grid(alpha=0.15)

    ax.xaxis.set_major_locator(DayLocator())  # major
    ax.xaxis.set_major_formatter(DateFormatter(" %a\n %d %b"))
    for tick in ax.xaxis.get_majorticklabels():
        tick.set_horizontalalignment("left")

    # remove labels at edges
    ax.get_xticklabels()[-1].set_visible(False)
    ax.get_xticklabels(which="minor")[-1].set_visible(False)
    ax.get_xticklabels(which="minor")[0].set_visible(False)

    # add vertical line after each sunday
    mondays = [datetime.datetime(2018, 6, 11, 0, 0)]
    for m in mondays:
        ax.plot([m, m], [-50, 50], "k", lw=0.1)
Example #8
0
def draw_multi_charts(chartlist, main_title, outputfile):
    if outputfile is None:
        return
    chart_width, chart_height = 14, 8.5
    fig = plt.figure(figsize=(chart_width, chart_height))
    rows, cols = get_page_dim(len(chartlist))
    index = 1

    for (chart_title, predicted, actual, dates) in chartlist:
        ax = fig.add_subplot(rows, cols, index)
        index += 1

        day_count = 1 + (dates[-1] - dates[0]).days
        ordinals = [matplotlib.dates.date2num(d) for d in dates]
        ax.plot_date(ordinals, actual, 'b-', label='Actual')
        ax.plot_date(ordinals, predicted, 'r-', label='Predicted')

        ax.xaxis.set_tick_params(labelsize=6)
        ax.xaxis.set_major_locator(
            DayLocator(interval=compute_date_step(day_count,
                                                  float(chart_width) / cols)))
        ax.xaxis.set_major_formatter(DateFormatter('%Y-%b-%d'))
        locator = HourLocator()
        locator.MAXTICKS = (day_count + 3) * 24
        ax.xaxis.set_minor_locator(locator)

        ax.autoscale_view()
        ax.grid(True)
        fig.autofmt_xdate()

        ax.legend(loc='upper right', shadow=True, fontsize=4)
        ax.set_title(chart_title)

    fig.suptitle(main_title)
    fig.savefig(outputfile)
    plt.close()
    print(">>   wrote: ", outputfile)
Example #9
0
def plot_one_week(x, y):
    """时间跨度为一周
    major tick = every days
    minor tick = every 3 hours
    """
    plt.close("all")

    fig = plt.figure(figsize=(14, 9))
    ax = fig.add_subplot(111)
    ax.plot(x, y)

    days = DayLocator(range(365))
    daysFmt = DateFormatter("%Y-%m-%d")
    hours = HourLocator([3, 6, 9, 12, 15, 18, 21])
    hoursFmt = DateFormatter("%H:%M")

    ax.xaxis.set_major_locator(days)
    ax.xaxis.set_major_formatter(daysFmt)
    ax.xaxis.set_minor_locator(hours)
    ax.xaxis.set_minor_formatter(hoursFmt)

    ax.autoscale_view()
    ax.grid()
    plt.setp(ax.xaxis.get_majorticklabels(), rotation=90)
    plt.setp(ax.xaxis.get_minorticklabels(), rotation=90)

    plt.ylim(
        [min(y) - (max(y) - min(y)) * 0.05,
         max(y) + (max(y) - min(y)) * 0.05])

    plt.xlabel("%s to %s" % (
        str(x[0]),
        str(x[-1]),
    ))

    return plt
    def showGraph(self):
        self.xData = []
        self.yData = []
        PullData.getGraphHistorical(self)
        for date in sorted(self.graphHistoricalInfo):
            self.xData += [dt.datetime.strptime(date, '%Y-%m-%d')]
            self.yData += [self.graphHistoricalInfo[date]['Close']]
        # self.xData = np.array([self.xData])
        # self.yData = np.array([self.yData])

        fig, ax = plt.subplots(
        )  #Got help from online about how to put dates on graph
        ax.plot(self.xData, self.yData)
        ax.xaxis.set_major_locator(DayLocator())
        ax.xaxis.set_minor_locator(HourLocator(arange(0, 25, 6)))
        ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))

        ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S')
        fig.autofmt_xdate()
        plt.title(self.stockName)
        plt.grid(True)
        plt.xlabel('Date')
        plt.ylabel('Price')
        plt.show()
Example #11
0
def smart_timeticks(time):
    '''
    Given a list or array of time values, create intelligent timeticks based
    on the max and min of the input.
    Return three items: major tick locator, minor tick locator, and a 
    format string.

    Example:
    >>>Mtick, mtick, fmt = smart_timeticks([time1, time2])
    '''
    deltaT = time[-1] - time[0]
    nHours = deltaT.days * 24.0 + deltaT.seconds / 3600.0
    if nHours < 1:
        Mtick = MinuteLocator(byminute=[0, 15, 30, 45])
        mtick = MinuteLocator(byminute=range(60), interval=5)
        fmt = DateFormatter('%H:%M UT')
    elif nHours < 4:
        Mtick = MinuteLocator(byminute=[0, 30])
        mtick = MinuteLocator(byminute=range(60), interval=10)
        fmt = DateFormatter('%H:%M UT')
    elif nHours < 12:
        Mtick = HourLocator(byhour=range(24), interval=2)
        mtick = MinuteLocator(byminute=[0, 15, 30, 45])
        fmt = DateFormatter('%H:%M UT')
    elif nHours < 24:
        Mtick = HourLocator(byhour=[0, 3, 6, 9, 12, 15, 18, 21])
        mtick = HourLocator(byhour=range(24))
        fmt = DateFormatter('%H:%M UT')
    elif nHours < 48:
        Mtick = HourLocator(byhour=[0, 6, 12, 18])
        mtick = HourLocator(byhour=range(24))
        fmt = DateFormatter('%H:%M UT')
    else:
        Mtick = DayLocator(bymonthday=range(1, 32))
        mtick = HourLocator(byhour=[0, 6, 12, 18])
        fmt = DateFormatter('%d %b')

    return (Mtick, mtick, fmt)
Example #12
0
        text2 = 'Pres'
        break
    else:
        print "No ingreso un dato valido\n"

print "plotting..."

#style.use('fivethirtyeight')
style.use('ggplot')

fig, ax = plt.subplots()
ax.set_title('Pruebas')
plt.xlabel('Fecha y hora')
plt.ylabel(text2)
ax.set_ylim(0, 100)
ax.xaxis.set_major_locator(HourLocator(arange(0, 24, 1)))
ax.xaxis.set_minor_locator(MinuteLocator(arange(0, 60, 5)))
ax.xaxis.set_major_formatter(DateFormatter("\n%Y-%m-%d"))
ax.xaxis.set_minor_formatter(DateFormatter('%H:%M'))
'''ax.set_title('Tanque de Prueba en lancha: Maca')
ax.plot_date(date, level, '-bs', markersize = 3)
ax.set_xlabel('Fecha y hora')
ax.set_ylabel('Nivel de combustible [cm]')
ax.set_ylim(0,60)
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_minor_locator(HourLocator(arange(0,25,6)))
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d ___'))
ax.xaxis.set_minor_formatter(DateFormatter('%H:%M'))'''


def animate(i):
Example #13
0
                            vmin=hmin)
        ax1.set_xlim(hovmoller['valid_1d+'][0], hovmoller['valid_1d+'][-1])
        ax1.set_ylim(0, 19)
        ax1.set_yticks(range(0, 19, 3))
        ax1.axes.xaxis.set_ticklabels([])
        ax1.set_ylabel('HRRR Forecast Hour')
        #
        # HRRR hovmoller max (contour)
        CS = ax1.contour(hovmoller['valid_2d'],
                         hovmoller['fxx_2d'],
                         hovBoxMax - hovCenter,
                         colors='k',
                         levels=S['contour'])
        plt.clabel(CS, inline=1, fontsize=10, fmt='%1.f')
        #
        ax1.grid()
        #
        fig.subplots_adjust(hspace=0, right=0.8)
        cbar_ax = fig.add_axes([0.82, 0.15, 0.02, 0.7])
        cb = fig.colorbar(hv, cax=cbar_ax)
        cb.ax.set_ylabel('%s (%s)' % (s, S['units']))
        #
        ax1.xaxis.set_major_locator(HourLocator(byhour=range(0, 24, 3)))
        dateFmt = DateFormatter('%b %d\n%H:%M')
        ax1.xaxis.set_major_formatter(dateFmt)
        #
        ax1.set_xlabel(r'Contour shows difference between maximum value in %s km$\mathregular{^{2}}$ box centered at %s %s and the value at the center point.' \
                    % (half_box*6, location[stn]['latitude'], location[stn]['longitude']), fontsize=8)
        #
        plt.savefig(SAVE + S['save'] + '.png')
Example #14
0
ax1.plot(a['DATES'], a['T_water1'], color='r', label='GSLBY -0.4 meters')

ax1.legend()
ax1.grid()

plt.title('Great Salt Lake Surface Temperature at GSLBY')
plt.ylabel('Temperature (C)')

##Format Ticks on Date Axis##
##----------------------------------
# Find months
months = MonthLocator()
# Find days
days = DayLocator(bymonthday=[1, 5, 10, 15, 20, 25])
# Find each 0 and 12 hours
hours = HourLocator(byhour=[0, 3, 6, 9, 12, 15, 18, 21])
# Find all hours
hours_each = HourLocator()
# Tick label format style
dateFmt = DateFormatter('%b %d\n%H:%M')
blank_dateFmt = DateFormatter('')
# Set the x-axis major tick marks
#ax1.xaxis.set_major_locator(days)
# Set the x-axis labels
ax1.xaxis.set_major_formatter(dateFmt)
# For additional, unlabeled ticks, set x-axis minor axis
#ax1.xaxis.set_minor_locator(hours)

SAVEDIR = '/uufs/chpc.utah.edu/common/home/u0553130/public_html/GSL_HRRR_temp/'
plt.savefig(SAVEDIR + 'GSLBY_HRRR_MesoWest.png')
    def format_x_axis(self, ax, data_frame, gp, has_bar, bar_ind, has_matrix):

        if has_matrix:
            # ax.colorbar()
            # ax.xticks(rotation=90)
            ax.set_xticks(bar_ind)
            ax.set_xlim([0, len(bar_ind)])
            ax.set_yticks(bar_ind)
            ax.set_ylim([0, len(bar_ind)])
            ax.set_xticklabels(data_frame.columns, minor=False)
            ax.set_yticklabels(data_frame.index, minor=False)

            # ax.plot([], [])

            for y in range(len(data_frame.index)):
                for x in range(len(data_frame.columns)):
                    plt.text(
                        x + 0.5,
                        y + 0.5,
                        '%.0f' % data_frame.loc[y, x],
                        horizontalalignment='center',
                        verticalalignment='center',
                    )

            return

        if has_bar:
            ax.set_xticks(bar_ind)
            ax.set_xticklabels(data_frame.index)
            ax.set_xlim([-1, len(bar_ind)])

            # if lots of labels make text smaller and rotate
            if len(bar_ind) > 6:
                plt.setp(plt.xticks()[1], rotation=90)
                # plt.gca().tight_layout()
                # matplotlib.rcParams.update({'figure.autolayout': True})
                # plt.gcf().subplots_adjust(bottom=5)
                import matplotlib.dates as mdates

                if gp.date_formatter is not None:
                    ax.format_xdata = mdates.DateFormatter(gp.date_formatter)

                plt.tight_layout()
                # ax.tick_params(axis='x', labelsize=matplotlib.rcParams['font.size'] * 0.5)
            return

        # format X axis
        dates = data_frame.index

        # scaling for time series plots with hours and minutes only (and no dates)
        if hasattr(data_frame.index[0],
                   'hour') and not (hasattr(data_frame.index[0], 'month')):
            ax.xaxis.set_major_locator(MultipleLocator(86400. / 3.))
            ax.xaxis.set_minor_locator(MultipleLocator(86400. / 24.))
            ax.grid(b=True, which='minor', color='w', linewidth=0.5)

        # TODO have more refined way of formating time series x-axis!

        # scaling for time series plots with dates too
        else:
            # to handle dates
            try:
                dates = dates.to_pydatetime()
                diff = data_frame.index[-1] - data_frame.index[0]

                import matplotlib.dates as md

                if gp.date_formatter is not None:
                    ax.xaxis.set_major_formatter(
                        md.DateFormatter(gp.date_formatter))
                elif diff < timedelta(days=4):

                    # class MyFormatter(Formatter):
                    #     def __init__(self, dates, fmt='%H:%M'):
                    #         self.dates = dates
                    #         self.fmt = fmt
                    #
                    #     def __call__(self, x, pos=0):
                    #         'Return the label for time x at position pos'
                    #         ind = int(round(x))
                    #         if ind >= len(self.dates) or ind < 0: return ''
                    #
                    #         return self.dates[ind].strftime(self.fmt)
                    #
                    # formatter = MyFormatter(dates)
                    # ax.xaxis.set_major_formatter(formatter)

                    date_formatter = '%H:%M'
                    xfmt = md.DateFormatter(date_formatter)
                    ax.xaxis.set_major_formatter(xfmt)

                    if diff < timedelta(minutes=20):
                        ax.xaxis.set_major_locator(
                            MinuteLocator(byminute=range(60), interval=2))
                        ax.xaxis.set_minor_locator(MinuteLocator(interval=1))
                    elif diff < timedelta(hours=1):
                        ax.xaxis.set_major_locator(
                            MinuteLocator(byminute=range(60), interval=5))
                        ax.xaxis.set_minor_locator(MinuteLocator(interval=2))
                    elif diff < timedelta(hours=6):
                        locator = HourLocator(interval=1)
                        ax.xaxis.set_major_locator(locator)
                        ax.xaxis.set_minor_locator(MinuteLocator(interval=30))
                    elif diff < timedelta(days=3):
                        ax.xaxis.set_major_locator(HourLocator(interval=6))
                        ax.xaxis.set_minor_locator(HourLocator(interval=1))

                elif diff < timedelta(days=10):
                    locator = DayLocator(interval=2)
                    ax.xaxis.set_major_locator(locator)
                    ax.xaxis.set_major_formatter(md.DateFormatter('%d %b %y'))

                    day_locator = DayLocator(interval=1)
                    ax.xaxis.set_minor_locator(day_locator)

                elif diff < timedelta(days=40):
                    locator = DayLocator(interval=10)
                    ax.xaxis.set_major_locator(locator)
                    ax.xaxis.set_major_formatter(md.DateFormatter('%d %b %y'))

                    day_locator = DayLocator(interval=1)
                    ax.xaxis.set_minor_locator(day_locator)

                elif diff < timedelta(days=365 * 0.5):
                    locator = MonthLocator(bymonthday=1, interval=2)
                    ax.xaxis.set_major_locator(locator)
                    ax.xaxis.set_major_formatter(md.DateFormatter('%b %y'))

                    months_locator = MonthLocator(interval=1)
                    ax.xaxis.set_minor_locator(months_locator)

                elif diff < timedelta(days=365 * 2):
                    locator = MonthLocator(bymonthday=1, interval=3)
                    ax.xaxis.set_major_locator(locator)
                    ax.xaxis.set_major_formatter(md.DateFormatter('%b %y'))

                    months_locator = MonthLocator(interval=1)
                    ax.xaxis.set_minor_locator(months_locator)

                elif diff < timedelta(days=365 * 5):
                    locator = YearLocator()
                    ax.xaxis.set_major_locator(locator)
                    ax.xaxis.set_major_formatter(md.DateFormatter('%Y'))

            except:
                try:
                    # otherwise we have integers, rather than dates
                    # TODO needs smarter more generalised mapping of dates
                    max = dates.max()
                    min = dates.min()

                    big_step = self.round_to_1((max - min) / 10)

                    small_step = big_step / 5

                    ax.xaxis.set_major_locator(MultipleLocator(big_step))
                    ax.xaxis.set_minor_locator(MultipleLocator(small_step))

                    plt.xlim(min, max)
                except:
                    pass
Example #16
0
            time = np.nan
        times.append(time)
    return times


def to_datetime(_seconds):
    if _seconds is np.nan:
        return np.datetime64('NaT')
    return np.datetime64(int(1e3 * _seconds), 'ms')


colors = plt.cm.viridis(np.linspace(0, 0.8, len(degrees)))
fig, ax = plt.subplots(1, 1)
for degree in degrees:
    times = computation_times(degree)
    times = [to_datetime(time) for time in times]
    ax.plot(numAssets,
            times,
            color=colors[degree - 1],
            label=f"Degree {degree}")
ax.legend()
ax.set_xlim(numAssets[0], numAssets[-1])
ax.set_xlabel("$d$")
ax.set_ylabel("computation time [H:M]")
ax.set_title("Computation time for the experiments in Table 5")
ax.yaxis.set_major_locator(HourLocator())
ax.yaxis.set_major_formatter(DateFormatter('%H:%M'))
fig.tight_layout()
plt.savefig("computation_times.png")
plt.show()
def get_treatment_plot(cf, fpath, site):
    num_plots = 5
    rdwx_nc = Dataset(fpath,"r")
    sites = rdwx_nc.variables["site_list"][:]
    i = numpy.where(sites == site)
    nan = float('nan')
    init_time = rdwx_nc.variables["forc_time"][:]
    fig = Figure(figsize=(FIG_WIDTH,FIG_HEIGHT),dpi=150)
    app_rate_var = rdwx_nc.variables["application_rate"]
    app_rates = ma.filled(app_rate_var[i],nan).flatten()
    chem_types = ma.filled(rdwx_nc.variables["chem_type"][i],nan).flatten()
    precip_types = ma.filled(rdwx_nc.variables["precip_type"][i],nan).flatten()
    #precip_types = map(lambda x: nan if math.isnan(x) else int(nan), precip_types)
    phase_types = ma.filled(rdwx_nc.variables["phase_type"][i],nan).flatten()
    #phase_types = map(lambda x: nan if math.isnan(x) else int(nan), phase_types)
    road_temp_var = rdwx_nc.variables[cf.met_vars.tmt_road_temp_var]
    road_temps = ma.filled(road_temp_var[i],nan).flatten()
    chem_type_labels = ["None","NACL","CACL2","MGCL2","CMA","KAC","Caliber","IceSlicer","IceBan"]
    precip_type_labels = ["None","Rain","Snow","Mixed Rain/Snow","Mixed Snow/Rain","Freezing Rain"]
    phase_type_labels = ["Dry","Wet","Chemically Wet","Chemically Ice","Slush","Snow","Ice"]
    begin_time = datetime.datetime.fromtimestamp(init_time)
    end_time = datetime.datetime.fromtimestamp(init_time + (len(road_temps)*3600))
    delta = datetime.timedelta(hours=1)
    dates = drange(begin_time, end_time, delta)
    axs = []
    ax = fig.add_subplot(num_plots,1,1)
    ax.set_ylabel(app_rate_var.units)
    ax.plot_date(dates,app_rates,"-")
    ax.set_ylim(bottom=0)
    axs.append(ax)
    ax = fig.add_subplot(num_plots,1,2)
    ax.set_ylabel("chem type")
    ax.plot_date(dates,chem_types,"-")
    ax.set_ylim(bottom=0)
    axs.append(ax)
    ax.yaxis.set_ticklabels(chem_type_labels)
    ax = fig.add_subplot(num_plots,1,3)
    ax.set_ylabel("precip type")
    ax.plot_date(dates,precip_types,"-")
    ax.yaxis.set_ticklabels(precip_type_labels)
    ax.set_ylim(bottom=0)
    axs.append(ax)
    ax = fig.add_subplot(num_plots,1,4)
    ax.set_ylabel("phase type")
    ax.plot_date(dates,phase_types,"-")
    ax.set_ylim(bottom=0)
    ax.yaxis.set_ticklabels(phase_type_labels)
    axs.append(ax)
    ax = fig.add_subplot(num_plots,1,5)
    ax.set_ylabel(road_temp_var.units)
    ax.plot_date(dates,road_temps,"-",label="road temp")
    axs.append(ax)
    for ax in axs:
        ax.grid(True)
        ax.xaxis.set_major_locator(DayLocator())
        ax.xaxis.set_minor_locator(HourLocator([6,12,18]))
        ax.xaxis.set_major_formatter(DateFormatter("%Y%m%d %H:%M"))
        ax.xaxis.set_minor_formatter(DateFormatter("%H"))
        ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S')
        for tick in ax.xaxis.get_minor_ticks():
            tick.label.set_fontsize(8)
    fig.autofmt_xdate()
    f = StringIO.StringIO()
    can = FigureCanvas(fig)
    can.print_figure(f)
    can.print_figure("/home/dicast/testme_treatment.png")
    tmt_plot = base64.b64encode(f.getvalue())

    recs = []
    tmt_expls = rdwx_nc.variables["treatment_explanation"][i][0]
    (days,fc_times_per_day,max_str_len) = tmt_expls.shape
    for d in range(days):
        for h in range(fc_times_per_day):
            expl = ma.filled(tmt_expls[d][h])
            expl = string.join(expl,"")
            vtime = init_time + (d*86400) + (h*3600)
            time_tup = time.gmtime(vtime)
            date_str = time.strftime("%Y-%m-%d %H:%M:%S", time_tup)
            recs.append({"date_str":date_str, "explanation":expl})
    rdwx_nc.close()
    return (tmt_plot,recs)
def get_summary_plot(logg, cf, rdwx_fpath, tmt_fpath, site_num, current_utc_hour, state_timezone, outfile=""):
    """ Makes a plot summary of road conditions.

    Parameters
    ----------
    cf : dict
        State dictionary such as backend_sys_path.State_dictionary[state]
    rdwx_fpath : str
        A string containing the file path for the road weather nc file
    tmt_fpath : str
        A string containing the file path for the treatments nc file
    site_num : int
        The site number to be plotted
    current_utc_hour : int
        The current utc hour
    state_timezone : tz
        The state timezone given by pytz.timezone()
    outfile : str
        Optional argument for user to save plots to an outfile, left empty returns a base 64 encoded plot
    """

    logg.write_info("get_summary_plot: calling get_rdwx_sites")
    all_sites = sitedict.get_rdwx_sites(cf)

    try:
        site_num_test = all_sites[site_num]
    except:
        return
    
    # Retrieve data from netcdf files
    logg.write_info("get_summary_plot: calling RdwxNetcdfFileData using rdwx_fpath %s and site_num %d" % (rdwx_fpath, site_num))
    rdwx_nc_object = RdwxNetcdfFileData(rdwx_fpath, site_num, current_utc_hour, state_timezone)

    logg.write_info("get_summary_plot: calling TmtNetcdfFileData using tmt_fpath %s and site_num %d" % (tmt_fpath, site_num))
    tmt_nc_object = TmtNetcdfFileData(tmt_fpath, site_num, current_utc_hour, state_timezone)

    logg.write_info("get_summary_plot: calling extract_precip_info()")
    rdwx_nc_object.extract_precip_info()

    logg.write_info("get_summary_plot: calling extract_temp_dewp()")
    rdwx_nc_object.extract_temp_dewp()

    logg.write_info("get_summary_plot: calling extract_wind_info()")
    rdwx_nc_object.extract_wind_info()

    logg.write_info("get_summary_plot: calling extract_snow_accum()")
    rdwx_nc_object.extract_snow_accum()

    logg.write_info("get_summary_plot: calling extract_road_temps()")
    tmt_nc_object.extract_road_temps()


    # Retrieve time alerts and recommended treatments
    logg.write_info("get_summary_plot: calling get_time_alerts_tmts()")
    time_alerts, time_tmts = get_time_alerts_tmts(cf, site_num)
    
    fig=plt.figure(figsize=(FIG_WIDTH,FIG_HEIGHT), dpi=150)

    # Create plots and create a consolidated list of these plots
    axs = []
    axs = make_alert_bar(axs, time_alerts, rdwx_nc_object, all_sites, site_num)
    axs, snow_accum_plot_bool = make_snow_accum_plot(axs, rdwx_nc_object)
    axs = make_precip_graph(axs, rdwx_nc_object, snow_accum_plot_bool)
    axs = make_temp_dewp_graph(axs, rdwx_nc_object, tmt_nc_object, snow_accum_plot_bool)
    axs = make_wind_graph(axs, rdwx_nc_object, snow_accum_plot_bool)
    if len(time_alerts) > 0:
        axs = make_rec_treatments_bar(axs, time_tmts, time_alerts, tmt_nc_object, snow_accum_plot_bool)
    logg.write_info("get_summary_plot: finished making plots")
    
    # fix missing major tick issue caused by autofmt_axdate
    fig.autofmt_xdate(rotation=0, ha='left') # horizontal rotation and left alignment of date
    fig.subplots_adjust(hspace=1, bottom=0.1) # add padding to make x axis visible

    # Set up axes labels and legends
    if not rdwx_nc_object.rdwx_error:
        for ax in axs:
            ax.xaxis.set_major_locator(DayLocator())
            ax.xaxis.set_minor_locator(HourLocator(arange(0,24,6), tz=state_timezone)) # Label every 6 hours

            ax.xaxis.set_major_formatter(DateFormatter("%Y%m%d")) # Only display date on major x ticks
            ax.xaxis.set_minor_formatter(DateFormatter("%H", tz=state_timezone))
            ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S')
            ax.legend(loc='upper left',bbox_to_anchor=(1,1.06),prop={'size':9}) # Move legend to outside of plot
            box = ax.get_position() # Grab plot sizes
            ax.set_position([box.x0, box.y0, box.width * 0.87, box.height]) # Shrink width to make legend room
            for tick in ax.xaxis.get_minor_ticks():
                tick.label.set_fontsize(8)
            for tick in ax.xaxis.get_major_ticks():
                tick.set_pad(-10) # Set date label lower on axis
                tick.label.set_fontsize(10)

    plt.tick_params(axis='x', which='major', reset=True, labelsize=10, pad=15)
    plt.xticks(ha='left')
    plt.tick_params(axis='x', which='minor', labelsize=8)

    # add all plots to the figure
    for ax in axs:
        fig.add_subplot(ax)

    if outfile != "":
        fig.savefig(outfile)

    f = StringIO.StringIO()
    fig_canvas = FigureCanvas(fig)
    fig_canvas.print_figure(f)
    return base64.b64encode(f.getvalue())
Example #19
0
def  plot_cloud_liquid(mwr_df, station, station_name, logo_path, curr_dt):
    '''
    Takes in a df with field variables integrated vapor, integrated liquid, cloud base (km), 
    and a rain flag of either 0.0 or 1.0.  Outputs a scatter plot of the df variables with the 
    left axis in kilometers and the right axis in mm liquid/ cm vapor.

    Parameters:
    mwr_df (dataframe): pandas dataframe of mwr precip data for that station.
    station (str): string of the 4 character station name.
    station_name (str): long name of station
    logo_path (string): full path to logo file
    curr_dt (datetime) - current date & hour
    
    Returns:
    Scatter plot output to (plot_parent_dir + 'nys_mwr_cloud_qc/' + today_date)
    '''
    
    # Make sure lidar_df is not full of NaN's
    empty_columns = []
    for col in mwr_df.columns:
        if mwr_df[col].count() == 0:
            empty_columns.append(col)
    if len(empty_columns) > 0:
        print('  mwr_df missing fields {} - no mwr cloud plot will be created.'.format(empty_columns))
        return

    # ---------------------------
    # GET DATA READY FOR PLOTTING
    # ---------------------------
    # Get times (which are first level index)
    # Can't use next two statements because index is not labelled
    #times_df = pd.DataFrame(mwr_df.index.get_level_values('time_integrated'))
    #times_array = np.array(times_df.drop_duplicates()['time_integrated'])
    times_df = pd.DataFrame(mwr_df.index.get_level_values(0).values)
    times_array = np.array(times_df.drop_duplicates()[0].values)
    if len(times_array) < min_times:
        print('  mwr_df has only {} times - no mwr cloud plot will be created.'.format(len(times_array)))
        return
    datetimes_array = pd.to_datetime(times_array)

    # Get start and stop time strings for plot title and current date string
    graphtimestamp_start=datetimes_array[0].strftime("%H UTC %m/%d/%y")
    graphtimestamp_end=curr_dt.strftime("%H UTC %m/%d/%y")
    today_date = curr_dt.strftime('%Y%m%d')
    
    # Get non-zero values of rain flag which represent rain
    rain = mwr_df['rain_flag'].values
    rain_indices = np.where( rain != 0.0 )[0]

    # -----------
    # CREATE PLOT
    # -----------
    fig, axL = plt.subplots(figsize = (9, 7.875))

    #axL.set_title('{} ({}) Derived MWR Products\n{} - {}'.format(
    #    station_name, station, graphtimestamp_start, graphtimestamp_end), fontsize = 24)    
    plt.suptitle('{} ({}) Derived MWR Products'.format(station_name, station), x = 0.57, fontsize = 24)
    plt.title('{} - {}'.format(graphtimestamp_start, graphtimestamp_end), ha = 'center', fontsize = 20)

    # Height axis for cloud base height 
    height_ticks = np.arange(0,11,1)
    axL.set_ylim(0,10)
    axL.set_yticks(height_ticks)
    axL.set_yticklabels(height_ticks, fontsize = 13)  # 16 in field
    axL.set_ylabel('Cloud Base (km)', fontsize = 15)  # 20 in field
    
    # Right axis for integrated liquid/vapor
    axR = axL.twinx()
    axR.set_ylim(0,10)
    axR.set_yticks(height_ticks)
    axR.set_yticklabels(height_ticks, fontsize = 13)  # 16 in field
    axR.set_ylabel('Integrated Moisture (mm liquid | cm vapor)', fontsize = 15)  # 20 in field
    
    # Make scatter plots
    cb = axL.scatter(datetimes_array.values, mwr_df['cloud_base'].values,c='black') #in kms
    iv = axR.scatter(datetimes_array.values, mwr_df['integrated_vapor'].values,c='red')
    il = axR.scatter(datetimes_array.values, mwr_df['integrated_liquid'].values,c='blue')
    
    # Plot vertical line for rainfall, and save last line as object for legend
    try:
        # WHY STOP AT SECOND LAST VALID INDEX?
        for rainy_time in datetimes_array[rain_indices][:-1]:
            # Set low value for alpha to get lighter color
            axL.axvline(rainy_time,color = 'g',alpha=.2)
        rf = axL.axvline(datetimes_array[rain_indices][-1],color = 'g',alpha=.5)
        axR.legend((cb,iv,il,rf) , ("Cloud Base","Integrated Vapor", "Integrated Liquid", "Rain Flag"),
                   fontsize = 14)  # 16 in field
    except:
        axR.legend((cb,iv,il) , ("Cloud Base","Integrated Vapor", "Integrated Liquid"), fontsize = 14)  # 16 in field
    
    # Get the time ticks
    axL.set_xlabel('Time (UTC)', fontsize=15)  # 20 in field
    # Add ticks at labeled times 
    axL.tick_params(axis='x',which='both',bottom='on',top='off')
    axL.yaxis.grid(linestyle = '--')
    # One date written per day
    axL.xaxis.set_major_locator( DayLocator(interval = 1) )
    # Show date, written as 'Jul-12'
    axL.xaxis.set_major_formatter( DateFormatter('%b-%d') )
    # Hour labels every 2 hours
    axL.xaxis.set_minor_locator( HourLocator(byhour=range(2,24,2),interval = 1) )
    # Show hour labels
    axL.xaxis.set_minor_formatter( DateFormatter('%H') )
    # Axis will squueze to size of actual data
    axL.set_xlim(curr_dt-timedelta(hours=24), datetimes_array[-1])
    axL.get_yaxis().set_label_coords(-0.04,0.5)
    axL.xaxis.get_major_ticks()[0].label.set_fontsize(13)  # 16 in field
    for tick in axL.xaxis.get_minor_ticks():
        tick.label.set_fontsize(14)  # 16 in field
    
    # Add mesonet logo
    fig.subplots_adjust(bottom=0.1,left=.05,right=1.1)
    # Read image from file to array
    im = plt.imread(get_sample_data(logo_path))
    # Add axes, plot image and remove x and y axes
    new_ax = fig.add_axes([1.07, -0.01, 0.1, 0.1])
    new_ax.imshow(im)
    new_ax.axis('off')
    
    # Save plot
    plot_path = plot_parent_dir + 'nys_mwr_cloud_qc/' + today_date
    if not os.path.exists(plot_path):
        os.mkdir(plot_path)
    plt.savefig(plot_path + '/ops.nys_mwr_cloud.' + curr_dt_string + '.' + station.lower() +'.png',
        bbox_inches='tight')
    print('  Plotted ' + 'MWR cloud/liquid profile' + ' for ' + station)
   
    # Clear current figure and axes, and close window - this is probably redundant:-)
    plt.clf, plt.cla(), plt.close()
Example #20
0
qclim['oldRH40_Avg'] = [0, 105]
qclim['oldRH60_Avg'] = [0, 105]
qclim['oldRH80_Avg'] = [0, 105]

# clean up the data with the qc limits
for j in datakeys:
    qcarr = np.ma.masked_outside(dataarray[datakeys.index(j)], qclim[j][0],
                                 qclim[j][1])
    qcarr.fill_value = np.nan
    dataarray[datakeys.index(j)] = qcarr.filled()

# date string for title
figdate = yesterday.strftime('%Y%m%d') + ' - ' + currenttime.strftime('%m%d')

# setup up the plots axes
hrs3 = HourLocator(range(24), interval=3)
hrsfmt = DateFormatter("%H")

#plot co2 and ID
#f, axarr = plt.subplots(2,sharex=True)
#axarr[0].plot_date(fasttimelist,fastarray[fastkeys.index('ID')],'.',
#xdate=True,ydate=False,label='ID')
#axarr[0].set_ylabel('ID')
#axarr[0].set_title('CO2 mv and ID ' + figdate)
#axarr[0].grid(True)
#axarr[1].plot_date(fasttimelist,fastarray[fastkeys.index('AVG_CO2')],'.',
#xdate=True,ydate=False,label='Avg CO2')
#axarr[1].set_xlabel('Time')
#axarr[1].set_ylabel('mV')
#axarr[1].grid(True)
#axarr[1].xaxis.set_major_locator(hrs3)
Example #21
0
tz = timezone('US/Pacific')


date1 = datetime.datetime( 2000, 3, 2, tzinfo=tz)
date2 = datetime.datetime( 2000, 3, 6, tzinfo=tz)
delta = datetime.timedelta(hours=6)
dates = drange(date1, date2, delta)
    
y = ML.arrayrange( len(dates)*1.0)
ysq = y*y

# note new constructor takes days or sequence of days you want to
# tick, not the hour as before.  Default is to tick every day
majorTick = DayLocator(tz=tz)

# the hour locator takes the hour or sequence of hours you want to
# tick, not the base multiple
minorTick = HourLocator(range(0,25,6), tz=tz)
ax = ML.subplot(111)
ax.plot_date(dates, ysq, tz=tz)

# this is superfluous, since the autoscaler should get it right, but
# use date2num and num2date to to convert between dates and floats if
# you want; both date2num and num2date convert an instance or sequence
ML.xlim( dates[0], dates[-1] )
ax.xaxis.set_major_locator(majorTick)
ax.xaxis.set_minor_locator(minorTick)
labels = ax.get_xticklabels()
ML.set(labels,'rotation', 90)
ML.show()
def carpet(timeseries, **kwargs):
    """
    Draw a carpet plot of a pandas timeseries.

    The carpet plot reads like a letter. Every day one line is added to the
    bottom of the figure, minute for minute moving from left (morning) to right
    (evening).
    The color denotes the level of consumption and is scaled logarithmically.
    If vmin and vmax are not provided as inputs, the minimum and maximum of the
    colorbar represent the minimum and maximum of the (resampled) timeseries.

    Parameters
    ----------
    timeseries : pandas.Series
    vmin, vmax : If not None, either or both of these values determine the range
    of the z axis. If None, the range is given by the minimum and/or maximum
    of the (resampled) timeseries.
    zlabel, title : If not None, these determine the labels of z axis and/or
    title. If None, the name of the timeseries is used if defined.
    cmap : matplotlib.cm instance, default coolwarm
    """

    # define optional input parameters
    cmap = kwargs.pop('cmap', cm.coolwarm)
    norm = kwargs.pop('norm', LogNorm())
    interpolation = kwargs.pop('interpolation', 'nearest')
    cblabel = kwargs.pop('zlabel', timeseries.name if timeseries.name else '')
    title = kwargs.pop('title', 'carpet plot: ' + timeseries.name if timeseries.name else '')

    # data preparation
    if timeseries.dropna().empty:
        print('skipped {} - no data'.format(title))
        return
    ts = timeseries.resample('min', label='left', closed='left').mean()
    vmin = max(0.1, kwargs.pop('vmin', ts[ts > 0].min()))
    vmax = max(vmin, kwargs.pop('vmax', ts.quantile(.999)))

    # convert to dataframe with date as index and time as columns by
    # first replacing the index by a MultiIndex
    # tz_convert('UTC'): workaround for https://github.com/matplotlib/matplotlib/issues/3896
    mpldatetimes = date2num(ts.index.tz_convert('UTC').to_pydatetime())
    ts.index = pd.MultiIndex.from_arrays(
        [np.floor(mpldatetimes), 2 + mpldatetimes % 1])  # '2 +': matplotlib bug workaround.
    # and then unstacking the second index level to columns
    df = ts.unstack()

    # data plotting

    fig, ax = plt.subplots()
    # define the extent of the axes (remark the +- 0.5  for the y axis in order to obtain aligned date ticks)
    extent = [df.columns[0], df.columns[-1], df.index[-1] + 0.5, df.index[0] - 0.5]
    im = plt.imshow(df, vmin=vmin, vmax=vmax, extent=extent, cmap=cmap, aspect='auto', norm=norm,
                    interpolation=interpolation, **kwargs)

    # figure formatting

    # x axis
    ax.xaxis_date()
    ax.xaxis.set_major_locator(HourLocator(interval=2))
    ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
    ax.xaxis.grid(True)
    plt.xlabel('UTC Time')

    # y axis
    ax.yaxis_date()
    dmin, dmax = ax.yaxis.get_data_interval()
    number_of_days = (num2date(dmax) - num2date(dmin)).days
    # AutoDateLocator is not suited in case few data is available
    if abs(number_of_days) <= 35:
        ax.yaxis.set_major_locator(DayLocator())
    else:
        ax.yaxis.set_major_locator(AutoDateLocator())
    ax.yaxis.set_major_formatter(DateFormatter("%a, %d %b %Y"))

    # plot colorbar
    cbticks = np.logspace(np.log10(vmin), np.log10(vmax), 11, endpoint=True)
    cb = plt.colorbar(format='%.0f', ticks=cbticks)
    cb.set_label(cblabel)

    # plot title
    plt.title(title)

    return im
Example #23
0
        # theta levels are spaced by 5
        cubes = iris.load(save_dir + folder + '/circulation/circulations_' + str(theta) + 'K_' + strapp + '.nc')
    
        n = 1
        m = 0
        plt.subplot(2, 2, n) # JB for this specific case with 4 plots
        for cube in cubes:
            if 'circulation' in cube.name(): # the '_' before can be added to remove the line integral
                iplt.plot(cube, label=cube.name(), color = colours[m][i], linewidth = 2.5)
                m += 1

        pg = plt.gca()
        fmt = DateFormatter('\n%m/%d')                          # <--- change this bit to get different formatting of dates/times
        fmt2 = DateFormatter('%H')
        majorLocator = DayLocator(interval=1)
        minorLocator = HourLocator(range(0, 24, 6))
        pg.xaxis.set_major_formatter(fmt)
        pg.xaxis.set_minor_formatter(fmt2)
        pg.xaxis.set_minor_locator(minorLocator)
        pg.xaxis.set_major_locator(majorLocator)

        plt.legend(ncol=2, loc=3, bbox_to_anchor=(0., 1.07, .6, .102))
        # plt.savefig(plotdir + 'circulation_' + theta + 'K.png')
        #plt.title(folder + '_' + strapp)
        plt.title('Circulation')

        for cube in cubes:        
            if 'circulation' not in cube.name():
                n += 1
                plt.subplot(2, 2, n) # JB for this specific case with 4 plots
                qplt.plot(cube/cube.data[2], color = colours[0][i], label = str(theta) + 'K', linewidth = 2.5)
Example #24
0
            label='f12')
    plt.title(stn)
    plt.grid()
    plt.ylabel('Temperature (c)')
    plt.legend()

    ##Format Ticks##
    ##----------------------------------
    from matplotlib.dates import DateFormatter, YearLocator, MonthLocator, DayLocator, HourLocator
    # Find months
    months = MonthLocator()
    # Find days
    days = DayLocator(bymonthday=[1, 15])
    days = DayLocator(bymonthday=range(0, 31))
    # Find each 0 and 12 hours
    hours = HourLocator(byhour=[0, 6, 12, 18])
    # Find all hours
    hours_each = HourLocator()
    # Tick label format style
    #dateFmt = DateFormatter('%b %d, %Y\n%H:%M')
    dateFmt = DateFormatter('%b %d\n%Y')
    # Set the x-axis major tick marks
    ax.xaxis.set_major_locator(days)
    # Set the x-axis labels
    ax.xaxis.set_major_formatter(dateFmt)
    # For additional, unlabeled ticks, set x-axis minor axis
    #ax.xaxis.set_minor_locator(hours)

    plt.savefig('./fig/' + stn + '_temp.png')

    # Plot the comparison
def plot_station_data(date,site,sitetitle,df):
    '''Given site station ID, the title of that site, and a dataframe of ASOS observation data from the last 1 day,
    returns a plot of the last day of weather at that site. 
    
    Parameters: 
    site (str): string of ASOS station ID
    sitetitle (str): string of ASOS station full name 
    df (dataframe): dataframe containing last 72 hours (3 days) of ASOS station data 
    
    Returns: 
    None
    
    *saves plots to plot_dir listed near top of script*
    '''
    if isinstance(df, int): #Returns if the station is not reporting
        return
    
    lower_site = site.lower()
    timestamp_end=str(df.index[-1].strftime('%Y%m%d%H%M'))
    dt = df.index[:]
    graphtimestamp_start=dt[0].strftime("%m/%d/%y %H:%M") 
    graphtimestamp_end=dt[-1].strftime("%m/%d/%y %H:%M")      
    #now = datetime.datetime.utcnow()
    #now = datetime.strptime(date,'%Y%m%d')
    #today_date = dt[-1].strftime('%Y%m%d')
    today_date = dt[0].strftime('%Y%m%d')
    markersize = 1.5
    linewidth = 1.0
    
    #make figure and axes
    fig = plt.figure()
    fig.set_size_inches(18,10)
    if 'snow_depth_set_1' in df.keys():          #six axes if snow depth 
        ax1 = fig.add_subplot(6,1,1)
        ax2 = fig.add_subplot(6,1,2,sharex=ax1)
        ax3 = fig.add_subplot(6,1,3,sharex=ax1)
        ax4 = fig.add_subplot(6,1,4,sharex=ax1)
        ax5 = fig.add_subplot(6,1,5,sharex=ax1)
        ax6 = fig.add_subplot(6,1,6,sharex=ax1)
        ax6.set_xlabel('Time (UTC)')
    else:
        ax1 = fig.add_subplot(5,1,1)             #five axes if no snow depth
        ax2 = fig.add_subplot(5,1,2,sharex=ax1)
        ax3 = fig.add_subplot(5,1,3,sharex=ax1)
        ax4 = fig.add_subplot(5,1,4,sharex=ax1)
        ax5 = fig.add_subplot(5,1,5,sharex=ax1)
        ax5.set_xlabel('Time (UTC)')
    
    ax1.set_title(site+' '+sitetitle+' '+graphtimestamp_start+' - '+graphtimestamp_end)
    #plot airT and dewT
    if 'air_temp_set_1' in df.keys():
        airT = df['air_temp_set_1']
        ax1.plot_date(dt,airT,'o-',label="Temp",color="blue",linewidth=linewidth,markersize=markersize)  
    if 'dew_point_temperature_set_1d' in df.keys():
        dewT = df['dew_point_temperature_set_1d']
        ax1.plot_date(dt,dewT,'o-',label="Dew Point",color="black",linewidth=linewidth,markersize=markersize)
    elif 'dew_point_temperature_set_1' in df.keys():
        dewT = df['dew_point_temperature_set_1']
        dewT_new = dewT.dropna()
        dewT_dt_list = []
        for i in range(0,len(dewT)):
            if pd.isnull(dewT[i]) == False:
                dewT_dt_list.append(dt[i])
        ax1.plot_date(dewT_dt_list,dewT_new,'o-',label="Dew Point",color="black",linewidth=linewidth,markersize=markersize)
    if ax1.get_ylim()[0] < 0 < ax1.get_ylim()[1]:
        ax1.axhline(0, linestyle='-', linewidth = 1.0, color='deepskyblue')
        trans = transforms.blended_transform_factory(ax1.get_yticklabels()[0].get_transform(), ax1.transData)
        ax1.text(0,0,'0C', color="deepskyblue", transform=trans, ha="right", va="center") #light blue line at 0 degrees C
    ax1.set_ylabel('Temp ($^\circ$C)')
    ax1.legend(loc='best',ncol=2)
    axes = [ax1]                             #begin axes

    #plotting wind speed and gust
    if 'wind_speed_set_1' in df.keys():
        wnd_spd = df['wind_speed_set_1']
        ax2.plot_date(dt,wnd_spd,'o-',label='Speed',color="forestgreen",linewidth=linewidth,markersize=markersize)
    if 'wind_gust_set_1' in df.keys():
        wnd_gst = df['wind_gust_set_1']
        max_wnd_gst = wnd_gst.max(skipna=True)
        ax2.plot_date(dt,wnd_gst,'o-',label='Gust (Max ' + str(round(max_wnd_gst,1)) + 'kt)',color="red",linewidth=0.0,markersize=markersize) 
    ax2.set_ylabel('Wind (kt)')
    ax2.legend(loc='best',ncol=2)
    axes.append(ax2)
    
    #plotting wind direction
    if 'wind_direction_set_1' in df.keys():
        wnd_dir = df['wind_direction_set_1']
        ax3.plot_date(dt,wnd_dir,'o-',label='Direction',color="purple",linewidth=0.2, markersize=markersize)
    ax3.set_ylim(-10,370)
    ax3.set_ylabel('Wind Direction')
    ax3.set_yticks([0,90,180,270,360])
    axes.append(ax3)
    
    #plotting MSLP
    if 'sea_level_pressure_set_1d' in df.keys():
        mslp = df['sea_level_pressure_set_1d']
        max_mslp = mslp.max(skipna=True)
        min_mslp = mslp.min(skipna=True)
        labelname = 'Min ' + str(round(min_mslp,1)) + 'hPa, Max ' + str(round(max_mslp,2)) + 'hPa'
        ax4.plot_date(dt,mslp,'o-',label=labelname,color='darkorange',linewidth=linewidth,markersize=markersize)
    elif 'sea_level_pressure_set_1' in df.keys():
        mslp = df['sea_level_pressure_set_1']
        mslp_new = mslp.dropna()
        mslp_dt_list = []
        for i in range(0,len(mslp)):
            if pd.isnull(mslp[i]) == False:
                mslp_dt_list.append(dt[i])
        max_mslp = mslp.max(skipna=True)
        min_mslp = mslp.min(skipna=True)
        labelname = 'Min ' + str(round(min_mslp,2)) + 'hPa, Max ' + str(round(max_mslp,2)) + 'hPa'
        ax4.plot_date(mslp_dt_list,mslp_new,'o-',label=labelname,color='darkorange',linewidth=linewidth,markersize=markersize)
    ax4.legend(loc='best')
    ax4.set_ylabel('MSLP (hPa)')
    ax4.set_xlabel('Time (UTC)')
    axes.append(ax4)
    
    #plotting precip accumulation
    if 'precip_intervals_set_1d' in df.keys():
        precip_inc = df['precip_intervals_set_1d']
        precip_inc_new = precip_inc.dropna()
        precip_inc_new = list(precip_inc_new.values)
        precip_accum = 0.0
        precip_accum_list = []
        for increment in precip_inc_new:
            precip_accum = precip_accum + increment
            precip_accum_list.append(precip_accum)
        precip_dt_list = []
        for i in range(0,len(precip_inc)):
            if pd.isnull(precip_inc[i]) == False:
                precip_dt_list.append(dt[i])
        max_precip = max(precip_accum_list)
        labelname = 'Precip (' + str(round(max_precip,2)) + 'mm)'
        ax5.plot_date(precip_dt_list,precip_accum_list,'o-',label=labelname,color='navy',linewidth=linewidth,markersize=markersize)
        if max_precip > 0:
            ax5.set_ylim(-0.1*max_precip,max_precip+max_precip*0.2)
        else:
            ax5.set_ylim(-0.5,5)
    ax5.legend(loc='best')
    ax5.set_ylabel('Precip (mm)')
    axes.append(ax5)
    
    #plotting snow depth
    if 'snow_depth_set_1' in df.keys():
        snow_depth = df['snow_depth_set_1']
        snow_depth_new = snow_depth.dropna()
        snow_depth_dt_list = []
        for i in range(0,len(snow_depth)):
            if pd.isnull(snow_depth[i]) == False:
                snow_depth_dt_list.append(dt[i])  
        max_snow_depth = snow_depth.max(skipna=True)
        min_snow_depth = snow_depth.min(skipna=True)
        labelname = 'Min Depth ' + str(round(min_snow_depth,2)) + 'mm, Max Depth ' + str(round(max_snow_depth,2)) + 'mm'
        ax6.plot_date(snow_depth_dt_list,snow_depth_new,'o-',label=labelname,color='deepskyblue',linewidth=linewidth,markersize=markersize)
        if max_snow_depth > 0:
            ax6.set_ylim(-0.1*max_snow_depth,max_snow_depth+max_snow_depth*0.2)
        else:
            ax6.set_ylim(-0.5,5)
        ax6.legend(loc='best')
        ax6.set_ylabel('Snow Depth (mm)')
        axes.append(ax6)
        
    for ax in axes: 
        ax.spines["top"].set_visible(False)  #darker borders on the grids of each subplot
        ax.spines["right"].set_visible(False)  
        ax.spines["left"].set_visible(False)
        ax.spines["bottom"].set_visible(False)
        ax.tick_params(axis='x',which='both',bottom='on',top='off')     #add ticks at labeled times
        ax.tick_params(axis='y',which='both',left='on',right='off') 

        ax.xaxis.set_major_locator( DayLocator() )
        ax.xaxis.set_major_formatter( DateFormatter('%b-%d') )
        
        ax.xaxis.set_minor_locator( HourLocator(np.linspace(6,18,3)) )
        ax.xaxis.set_minor_formatter( DateFormatter('%H') )
        ax.fmt_xdata = DateFormatter('Y%m%d%H%M%S')
        ax.yaxis.grid(linestyle = '--')
        ax.get_yaxis().set_label_coords(-0.06,0.5)
       
    plot_path = plot_dir+'/'+today_date
    if not os.path.exists(plot_path):
            os.makedirs(plot_path)
    try:
        plt.savefig(plot_path+'/ops.asos.'+timestamp_end+'.'+lower_site+'.png',bbox_inches='tight')
    except:
        print("Problem saving figure for %s. Usually a maxticks problem" %site)
    plt.close()
Example #26
0
# Plot mains
ax = elec.mains().plot(ax=ax,
                       unit=UNIT,
                       width=10000,
                       plot_kwargs={
                           'linewidth': 0.3,
                           'color': 'grey',
                           'label': 'Mains (active power)'
                       })

# Prettify
ax.grid(False)
ax.set_ylim([0, 4])
ax.set_xlabel('Time (hour of day)')
ax.xaxis.set_major_formatter(DateFormatter("%H", tz=TZ))
ax.xaxis.set_major_locator(HourLocator(interval=6, tz=TZ))
for text in ax.get_xticklabels():
    text.set_rotation(0)
    text.set_ha('center')
sns.despine(ax=ax)
legend = ax.legend(loc='upper left')
for line in legend.get_lines():
    line.set_linewidth(4)

plot_config.format_axes(ax)
plt.tight_layout()

plt.draw()

plt.savefig(join(plot_config.FIG_DIR, '02_area_plot.eps'),
            bbox_inches='tight',
                    df_system['basesupply_d2'],
                    linewidth=lw,
                    color='#d62728',
                    label='battery supply')

#import pdb; pdb.set_trace()
#C = df_settings.loc[(df_settings['run'] == run) & (df_settings['id'] == int(ind))]['line_capacity'].iloc[0]/1000.
#lns = lns + ax.plot(df_system.index,[C]*len(df_system.index),'r',dashes=[5, 5, 5, 5],linewidth=1.0,label='capacity constraint')

ax.plot(df_system.index, [0.0] * len(df_system.index), 'k', linewidth=1.0)

ax.set_ylabel('Supply [MW]')
ax.set_xlim(xmin=start, xmax=end)
ax.set_ylim(ymin=-0.25, ymax=2.0)  #, ymax=2.1)

ax.xaxis.set_major_locator(HourLocator(arange(0, 25, 6)))
ax.xaxis.set_minor_locator(HourLocator(arange(0, 25, 3)))
ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
#ax.xaxis.set_major_formatter(DateFormatter('%d:%m %H:%M'))

#import pdb; pdb.set_trace()
# ax2 = ax.twinx()
# ax2.set_ylabel('Price [USD/MW]')
# lns = lns + ax2.plot(df_system.index,df_system['clearing_price'],'g',label='LEM price')
# lns = lns + ax2.plot(df_system.index,df_system['WS'],'g',dashes=[1, 1],label='WS price')
# ax2.xaxis.set_major_locator(HourLocator(arange(0, 25, 24)))
# ax2.xaxis.set_minor_locator(HourLocator(arange(0, 25, 6)))
# ax2.xaxis.set_major_formatter(DateFormatter('%m/%d'))
# #ax2.set_ylim(ymin=-50.,ymax=110)

# align_yaxis(ax, 0, ax2, 0)
Example #28
0
def plot_mwr_ts(mwr_df, station, station_name, logo_path, curr_dt):
    '''
    Given microwave radiometer dataframe and station ID, save plot of mwr profiler data 
    
    Parameters: 
    mwr_df (dataframe): pandas dataframe of mwr time series data for that station.
    station (str): string of station ID
    station_name (str): string of station location
    logo_path (string): full path to logo file
    curr_dt (datetime) - current date & hour
    
    Returns:
    Timeseries plot output to (plot_parent_dir + 'nys_mwr_ts_qc/' + today_date)
    '''
    
    # Make sure mwr_df is not full of NaN's
    empty_columns = []
    for col in mwr_df.columns:
        if mwr_df[col].count() == 0:
            empty_columns.append(col)
    if len(empty_columns) == len(mwr_df.columns):
        print('  mwr_4plot_df missing all fields - no timeseries plot will be created.')
        return

    # ---------------------------
    # GET DATA READY FOR PLOTTING
    # ---------------------------
    # Get times (which are first level index)
    times_df = pd.DataFrame(mwr_df.index.get_level_values('time'))
    times_array = np.array(times_df.drop_duplicates()['time'])
    if len(times_array) < min_times:
        print('  mwr_df has only {} times - no timeseries plot will be created.'.format(len(times_array)))
        return
    datetimes_array = pd.to_datetime(times_array)
    
    # Get heights (which are second level index)
    heights_df = pd.DataFrame(mwr_df.index.get_level_values('range'))
    # Flip height vector since rows run from top to bottom
    heights_array = np.flip(np.array(heights_df.drop_duplicates()['range']))
    
    # Get start and stop time strings for plot title and current date string
    graphtimestamp_start=datetimes_array[0].strftime("%H UTC %m/%d/%y")
    graphtimestamp_end=curr_dt.strftime("%H UTC %m/%d/%y")
    today_date = curr_dt.strftime('%Y%m%d')

    # Create empty temperature, theta, liquid and rel humidity arrays
    temps_array = np.empty([len(heights_array),len(times_array)]) * np.nan
    th_array = np.empty([len(heights_array),len(times_array)]) * np.nan
    lq_array = np.empty([len(heights_array),len(times_array)]) * np.nan
    rh_array = np.empty([len(heights_array),len(times_array)]) * np.nan

    # Fill arrays for contour plotting so heights are reversed (high to low) & dims order = (heights,time)
    if mwr_df['temperature'].count() > 0:
        temps_df = pd.DataFrame(mwr_df['temperature'])
        temps_array = np.flip(np.transpose(np.array(temps_df).reshape((len(times_array),len(heights_array)))),0)
    if mwr_df['theta'].count() > 0:
        th_df = pd.DataFrame(mwr_df['theta'])
        th_array = np.flip(np.transpose(np.array(th_df).reshape((len(times_array),len(heights_array)))),0)
    if mwr_df['liquid'].count() > 0:
        lq_df = pd.DataFrame(mwr_df['liquid'])
        lq_array = np.flip(np.transpose(np.array(lq_df).reshape((len(times_array),len(heights_array)))),0)
    if mwr_df['relative_humidity'].count() > 0:
        rh_df = pd.DataFrame(mwr_df['relative_humidity'])
        rh_array = np.flip(np.transpose(np.array(rh_df).reshape((len(times_array),len(heights_array)))),0)

    # Replaced these two 'for' loops with above statements
    #for i in range(0,len(times_array)):
    #    for j in range(0,len(heights_array)):
    #        temps_array[j,i] = mwr_df.loc[(times_array[i],heights_array[j]),'temperature']
    #        th_array[j,i] = mwr_df.loc[(times_array[i],heights_array[j]),'theta']
    #        lq_array[j,i] = mwr_df.loc[(times_array[i],heights_array[j]),'liquid']
    #        rh_array[j,i] = mwr_df.loc[(times_array[i],heights_array[j]),'relative_humidity']

    # -----------
    # CREATE PLOT
    # -----------
    fig = plt.figure(figsize = (10, 7.875))
    axes = []
    
    # TEMPERATURE
    ax1 = fig.add_subplot(4,1,1)  # 4x1 grid, 1st subplot
    # QUESTION - shouldn't temps_array be (times x range) instead of (range x times)?
    if np.count_nonzero(np.isnan(temps_array)) != len(heights_array)*len(times_array):
        temp = ax1.contourf(datetimes_array, heights_array/1000., temps_array, levels=np.arange(-50,21,2),
                            extend='both', cmap='viridis')
        contour = ax1.contour(datetimes_array,heights_array/1000.,temps_array, levels=np.arange(-50,21,10),
                              colors='grey')
    else:
        ax1.text(0.5,0.20,'NO DATA AVAILABLE',horizontalalignment='center')
    plt.clabel(contour,fmt='%1.f',colors = 'green') # plot contour labels
    cb = plt.colorbar(temp)
    cb.set_ticks(np.arange(-50,21,10))
    cb.set_ticklabels(np.arange(-50,21,10))
    cb.ax.tick_params(labelsize=13)  # 16 in field
    cb.set_label('Temp. ($^\circ$C)', fontsize = 15)  # 16 in field
    ax1.tick_params(axis='x',which='both',bottom='off',top='off')
    ax1.set_xticks([])
    #ax1.set_title('{} ({}) MWR Products\n{} - {}'.format(station_name, station, graphtimestamp_start,
    #                                                     graphtimestamp_end), fontsize = 24)    
    plt.suptitle  ('{} ({}) MWR Products'.format(station_name, station), x = 0.465, fontsize = 24)
    plt.title('{} - {}'.format(graphtimestamp_start, graphtimestamp_end), ha = 'center', fontsize = 20)
    axes.append(ax1)
    
    # POTENTIAL TEMPERATURE (theta)
    ax2 = fig.add_subplot(4,1,2)  # 4x1 grid, 2nd subplot
    if np.count_nonzero(np.isnan(th_array)) != len(heights_array)*len(times_array):
        theta = ax2.contourf(datetimes_array, heights_array/1000., th_array, levels=np.arange(250,361,2),
                             extend='both', cmap='gist_ncar')
        contour = ax2.contour(datetimes_array,heights_array/1000.,th_array,levels=np.arange(250,361,10),
                              colors='grey')
    else:
        ax2.text(0.5,0.20,'NO DATA AVAILABLE',horizontalalignment='center')
    plt.clabel(contour,fmt='%1.f',colors = 'green')
    cb = plt.colorbar(theta)
    cb.set_ticks(np.arange(250,361,20))
    cb.set_ticklabels(np.arange(250,361,20))
    cb.ax.tick_params(labelsize=13)  # 16 in field
    cb.set_label('$\Theta$ (K)', fontsize = 15)  # 16 in field
    ax2.tick_params(axis='x',which='both',bottom='off',top='off')
    ax2.set_xticks([])
    axes.append(ax2)

    #LIQUID
    ax3 = fig.add_subplot(4,1,3)

    # Get lower and upper ends of log of data
    levs = np.power(10, np.arange(-3, 0.01, 0.125))  # These are powers of 10 for y-axis
    if np.count_nonzero(np.isnan(lq_array)) != len(heights_array)*len(times_array):
        lq = ax3.contourf(datetimes_array, heights_array/1000., lq_array, levels=levs, extend='both',
                          cmap='rainbow')
        # Contour only every 8th level (10^-2,10^-1,10^0)
        contour = ax3.contour(datetimes_array,heights_array/1000.,lq_array,levels=levs[8::8],colors='black')
    else:
        ax3.text(0.5,0.20,'NO DATA AVAILABLE',horizontalalignment='center')
        
    # Make labels in log format 
    fmt = matplotlib.ticker.LogFormatterMathtext()
    plt.clabel(contour,contour.levels,fmt=fmt,colors = 'white')
    norm = matplotlib.colors.LogNorm(vmin = .001, vmax = 1) 
    sm = plt.cm.ScalarMappable(norm=norm, cmap = lq.cmap)
    sm.set_array([])
    cb = plt.colorbar(sm)
    cb.set_ticks(levs[::8])
    #cb.set_ticklabels(['$10^-3$','$10^-2$','$10^-1$','$10^0$'])
    cb.set_ticklabels(['$10^{-3}$','$10^{-2}$','$10^{-1}$','$10^{0}$'])
    cb.ax.tick_params(labelsize=13)  # 16 in field
    cb.set_label('Liquid (g m$^{-3}$)', fontsize = 15)  # 16 in field
    ax3.tick_params(axis='x',which='both',bottom='off',top='off')
    ax3.set_xticks([])
    axes.append(ax3)
    
    # RH
    ax4 = fig.add_subplot(4,1,4)
    if np.count_nonzero(np.isnan(rh_array)) != len(heights_array)*len(times_array):
        rh = ax4.contourf(datetimes_array, heights_array/1000., rh_array, levels=np.arange(0,110,5),
                          cmap='BrBG')
        contour = ax4.contour(datetimes_array,heights_array/1000.,rh_array,levels=np.array([40,60,80,90,99]))
    else:
        ax4.text(0.5,0.20,'NO DATA AVAILABLE',horizontalalignment='center')
    plt.clabel(contour,contour.levels,fmt='%1.f')
    cb = plt.colorbar(rh)
    cb.set_ticks(np.arange(0,110,20))
    cb.set_ticklabels(np.arange(0,110,20))
    cb.ax.tick_params(labelsize=13)  # 16 in field
    cb.set_label('RH (%)',fontsize=15)  # 16 in field
    
    ax4.set_xlabel('Time (UTC)', fontsize=15)  # 16 in field
    # Place x-label away from figure
    ax4.get_xaxis().set_label_coords(0.5,-0.25)
    # Add ticks at labeled times
    ax4.tick_params(axis='x',which='both',bottom='on',top='off')
    ax4.tick_params(axis='x', which='major', length=8)
    ax4.tick_params(axis='x', which='minor', length=4)
    ax4.set_xlim(curr_dt-timedelta(hours=24), datetimes_array[-1])
    # One date written per day
    ax4.xaxis.set_major_locator( DayLocator(interval = 1) )
    # Show date, written as 'Jul-12'
    ax4.xaxis.set_major_formatter( DateFormatter('%b-%d') )
    # Hour labels every 2 hours
    ax4.xaxis.set_minor_locator( HourLocator(byhour=range(2,24,2),interval = 1) )
    # Show hour labels
    ax4.xaxis.set_minor_formatter( DateFormatter('%H') )
    ax4.xaxis.get_major_ticks()[0].label.set_fontsize(13)  # 16 in field
    for tick in ax4.xaxis.get_minor_ticks():
        tick.label.set_fontsize(13)  # 16 in field
    axes.append(ax4)

    # Plot times from x-axis of each plot
    for ax in axes:
        ax.set_ylabel('Height (km)',fontsize = 15)  # 16 in field
        ax.tick_params(axis='y',which='both',left='on',right='off', labelsize=13)  # 16 in field
        ax.tick_params(axis='y', which='major', length=8)
        ax.tick_params(axis='y', which='minor', length=4)
        ax.yaxis.grid(linestyle = '--')
        # Place y-labels away from figure
        ax.get_yaxis().set_label_coords(-0.05,0.5)
            
    # Add mesonet logo
    fig.subplots_adjust(bottom=0.1,left=.05,right=1.1)
    # Read image from file to array
    im = plt.imread(get_sample_data(logo_path))
    # Add axes, plot image and remove x and y axes
    #new_ax = fig.add_axes([0.94, -0.01, 0.1, 0.1])
    new_ax = fig.add_axes([0.93, -0.01, 0.1, 0.1])
    new_ax.imshow(im)
    new_ax.axis('off')

    # Save plot
    plot_path = plot_parent_dir + 'nys_mwr_ts_qc/' + today_date
    if not os.path.exists(plot_path):
        os.mkdir(plot_path)
    plt.savefig(plot_path + '/ops.nys_mwr_ts.' + curr_dt_string + '.' + station.lower() + '.png',
                bbox_inches='tight')
    print('  Plotted MWR timeseries for ' + station)

    # Clear current figure and axes, and close window - this is probably redundant:-)
    plt.clf, plt.cla(), plt.close()
Example #29
0
cmap = mcolors.LinearSegmentedColormap.from_list('colormap', colors)

fig, ax = plt.subplots(1, figsize=(15, 8))
#cst = ax.contourf(resam_df_5min.index[:], np.arange(2048)*5+100, resam_df_5min[:].T,100,cmap=cmap)
cst = ax.contourf(raw_spectrum_log.index[:],
                  np.arange(2048) * 5 + 100,
                  raw_spectrum_log[:].T,
                  100,
                  cmap=cmap)

ax.set_xlabel('Time')
ax.set_ylabel('Height [m]')
#ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
# format the x tick marks
ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))

ax.xaxis.set_minor_formatter(DateFormatter('\n%d %b'))

ax.xaxis.set_major_locator(HourLocator(interval=1))
ax.xaxis.set_minor_locator(DayLocator())

ax.set_ylim([0, 6000])

cbZe = fig.colorbar(cst, ax=ax)
cbZe.set_label(r'[sr$^-1$ m$^-1$]')
cbZe.set_ticks([-7, -6, -5, -4])
cbZe.set_ticklabels([r'$10^{-7}$', r'$10^{-6}$', r'$10^{-5}$', r'$10^{-4}$'])
plt.tight_layout()

plt.savefig('CS135.png', format='png', bbox_to_anchor='tight')
Example #30
0
def lidar_field_plotting(station, station_name, lidar_df, field, logo_path, curr_dt):
    '''
    Takes in the lidar data and will produce a plot of either CNR, Horizonal Speed or Vertical Speed
    for a specific station.  Each plot will range from 100m to 3000m and will have wind barbs with the 
    direction of wind.
    
    Parameters:
    station (str): string of the 4 character station name.
    station_name (str): long name of station
    lidar_df (dataframe): pandas dataframe of lidar data for that station.
    field (string): must be one of ['cnr', 'w', 'velocity']
    logo_path (string): full path to logo file
    curr_dt (datetime) - current date & hour

    Returns:
    Plot of field data output to (plot_parent_dir + 'nys_lidar_<field>_qc/' + today_date)
    '''

    # Make sure input field is valid
    valid_fields = ['cnr','w','velocity']
    if field not in valid_fields:
        print('lidar field = {} not valid ... returning with no plot for {}.'.format(field,curr_dt))
        return
    
    # Make sure lidar_df is not full of NaN's
    empty_columns = []
    for col in lidar_df.columns:
        if lidar_df[col].count() == 0:
            empty_columns.append(col)
    if len(empty_columns) > 0:
        print('  lidar_df missing fields {} - no {} plot will be created.'.format(empty_columns,field))
        return

    # ---------------------------
    # GET DATA READY FOR PLOTTING
    # ---------------------------
    # Get times (which are second level index)
    times_df = pd.DataFrame(lidar_df.index.get_level_values('time'))
    times_array = np.array(times_df.drop_duplicates()['time'])
    if len(times_array) < min_times:
        print('  lidar_df has only {} times - no {} plot will be created.'.format(len(times_array),field))
        return
    datetimes_array = pd.to_datetime(times_array)

    # Get heights (which are first level index) between 100-3000m
    heights_df = pd.DataFrame(lidar_df.index.get_level_values('range'))
    heights_df = heights_df.loc[(heights_df['range'] >= 100) & (heights_df['range'] <= 3000)]
    # Flip array (highest to lowest value) for plotting
    heights_array = np.flip(np.array(heights_df.drop_duplicates()['range']))
    
    # Get start and stop time strings for plot title and current date string
    graphtimestamp_start=datetimes_array[0].strftime("%H UTC %m/%d/%y")
    graphtimestamp_end=curr_dt.strftime("%H UTC %m/%d/%y")
    today_date = curr_dt.strftime('%Y%m%d')

    # Initialize field_array (contains data in 'field' input param)
    field_array = np.zeros([len(heights_array),len(times_array)])
    
    # Create empty Uwind and Vwind arrays and fill with NaN's
    Uwind = np.full([len(heights_array),len(times_array)], np.nan)
    Vwind = np.full([len(heights_array),len(times_array)], np.nan)

    # Fill in field_array, Uwind and Vwind arrays
    for i in range(0,len(times_array)):
        for j in range(0,len(heights_array)):
            field_array[j,i] = lidar_df.loc[(heights_array[j],times_array[i]),field]
            direction = lidar_df.loc[(heights_array[j],times_array[i]),'direction']
            velocity = lidar_df.loc[(heights_array[j],times_array[i]),'velocity']
            #Take every other row and column of wind data 
            if (j % 2 == 0) and (i % 2 == 0):
                #direction is 450 degrees offset from plotting orientation
                #HOW DO WE KNOW THIS?
                Uwind[j,i] = np.cos((450. - direction)/180.*np.pi)*velocity 
                Vwind[j,i] = np.sin((450. - direction)/180.*np.pi)*velocity 

    # -----------
    # CREATE PLOT
    # -----------
    # Get the smallest and largest non-nan of the field_array data
    field_max = np.nanmax(field_array)
    field_min = np.nanmin(field_array)
    
    # Get the binsize based off the amount of bins defined globally
    binsize = (field_max - field_min)/bin_number

    # Round levels more precisely, define colorbar levels and choose cmap
    if field == 'w':
        levs = np.arange(-5, 5.01, 0.25)
        cblevs = np.arange(-5, 5.01, 1)
        colormap = 'bwr'
        extend = 'both'
    elif field == 'cnr':
        levs = np.arange(-30, 6, 1)
        cblevs = np.arange(-30, 6, 5)
        colormap = 'gist_ncar'
        extend = 'max'
    else:  # for velocity
        levs = np.arange(0, 101, 2)# np.round(levs)
        cblevs = np.arange(0, 101, 10)
        colormap = 'nipy_spectral'#'cool'
        extend = 'max'

    # Create figure
    fig, ax = plt.subplots(figsize = (10, 5.625))
    
    # Background array allows for the figure background color to be customized
    background = np.zeros([len(heights_array),len(times_array)])

    # Plot field filled contours and Uwind and Vwind vectors
    # Background color
    ax.contourf(datetimes_array,heights_array/1000.,background, colors = 'aliceblue')
    color_plot = ax.contourf(datetimes_array, heights_array/1000., field_array, levels = levs,
                             extend=extend, cmap=colormap)
    # Use every fourth wind value; length is length of barb in points
    ax.barbs(datetimes_array[::4],heights_array[::4]/1000.,Uwind[::4, ::4],Vwind[::4, ::4],length = 7)
    # Only plots black contour lines for vertical velocity or CNR data
    if field == 'w' or field == 'cnr':
        # Use every fourth level value
        contour = ax.contour(datetimes_array,heights_array/1000.,field_array,levels=levs[::4],colors='black')
        # Add labels to contours
        plt.clabel(contour,fmt='%1.1f',colors='white')

    # Plot colorbar
    cb = plt.colorbar(color_plot)
    cb.set_ticks(cblevs)
    cb.set_ticklabels(cblevs)
    cb.ax.tick_params(labelsize=14)

    # Label colorbar and get info for plot title
    # W and velocity have the same units
    if field == 'cnr':
        cb.set_label('CNR (dB)',fontsize=16)  # 20 in field
        field_title = 'Carrier-to-Noise Ratio'
        save_name = field
    elif field == 'w': 
        cb.set_label('m s$^{-1}$',fontsize=16)  # 20 in field
        field_title = 'Vertical Velocity'
        save_name = 'vert_wspd'
    elif field == 'velocity':
        cb.set_label('kt',fontsize=16)  # 20 in field
        field_title = 'Horizontal Velocity'
        save_name = 'horz_wspd'
    else:  # SINCE WE ALREADY CHECKED FOR VALID FIELD THIS MIGHT NOT BE NECESSARY
        cb.set_label('dB',fontsize = 16)  # 20 in field
        field_title = field.upper()
        save_name = field
        
    # Set title & subtitle of plot
    #ax.set_title('{} ({}) Lidar {}\n{} - {}'.format(station_name, station, field_title, graphtimestamp_start,
    #                                                            graphtimestamp_end), fontsize = 24)
    if field == 'cnr':
        plt.suptitle ('{} ({}) Lidar {}'.format(station_name, station,field_title), x = 0.47, fontsize = 22)
    elif field == 'w':
        plt.suptitle ('{} ({}) Lidar {}'.format(station_name, station,field_title), x = 0.465, fontsize = 22)
    elif field == 'velocity':
        plt.suptitle ('{} ({}) Lidar {}'.format(station_name, station,field_title), x = 0.465, fontsize = 22)        
    plt.title('{} - {}'.format(graphtimestamp_start, graphtimestamp_end), ha = 'center', fontsize = 18)

    # Set Y-axis height ticks  
    height_ticks = np.array([0.1,0.5,1,1.5,2,2.5,3])
    ax.set_yticks(height_ticks)
    ax.set_yticklabels(height_ticks, fontsize = 14)  # 16 in field
    ax.set_ylim(0.1,3)
    ax.set_ylabel('Height (km)', fontsize = 16)  # 20 in field
    
    # Set X-axis time ticks
    ax.set_xlabel('Time (UTC)', fontsize=16)  # 20 in field
    # DO WE NEED NEXT LINE?
    ax.tick_params(axis='x',which='both',bottom='on',top='off')                  #add ticks at labeled times

    ax.tick_params(axis='both', which='major', length=8)
    ax.tick_params(axis='both', which='minor', length=4)
    ax.yaxis.grid(linestyle = '--')
    ax.set_xlim(curr_dt-timedelta(hours=24), datetimes_array[-1])
    # One date written per day
    ax.xaxis.set_major_locator( DayLocator(interval = 1), )
    # Show date, written as 'Jul-12'
    ax.xaxis.set_major_formatter( DateFormatter('%b-%d'))
    # Hour labels every 2 hours
    ax.xaxis.set_minor_locator( HourLocator(byhour=range(2,24,2),interval = 1) )
    # Show hour labels
    ax.xaxis.set_minor_formatter( DateFormatter('%H'))
    ax.get_yaxis().set_label_coords(-0.08,0.5)
    ax.xaxis.get_major_ticks()[0].label.set_fontsize(14)  # 16 in field
    cb.ax.tick_params(labelsize=14)  # 16 in field
    for tick in ax.xaxis.get_minor_ticks():
        tick.label.set_fontsize(14)  # 16 in field
    
    # Add mesonet logo
    fig.subplots_adjust(bottom=0.1,left=.05,right=1.1)
    # Read image from file to array
    im = plt.imread(get_sample_data(logo_path))
    # Add axes, plot image and remove x and y axes
    #new_ax = fig.add_axes([0.95, -0.02, 0.1, 0.1])
    new_ax = fig.add_axes([0.93, -0.02, 0.1, 0.1])
    new_ax.imshow(im)
    new_ax.axis('off')
    
    # Save plot
    plot_path = plot_parent_dir + 'nys_lidar_' + save_name + '_qc/' + today_date
    if not os.path.exists(plot_path):
        os.mkdir(plot_path)
    plt.savefig(plot_path + '/ops.nys_lidar_' + save_name + '.' + curr_dt_string + '.' + station.lower() + '.png',
                bbox_inches='tight')
    print('  Plotted ' + field + ' Lidar' + ' for ' + station)

    # Clear current figure and axes, and close window - this is probably redundant:-)
    plt.clf, plt.cla(), plt.close()