arunreddy
7/25/2017 - 3:35 PM

power_plots.py

power_plots.py

# ---------------------------------------------------------
# library imports.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.dates import DateFormatter, DayLocator, HourLocator

# *********************************************************



# ---------------------------------------------------------
# Functions.

def datetime(x):
  return np.array(x, dtype = np.datetime64)
#

def plot_graph(df, title = '',zone_name=''):
  fig, ax = plt.subplots(figsize = (15, 5))
  fig.subplots_adjust(right = 0.75)
  #
  # # # Twin the x-axis twice to make independent y-axes.
  axes = [ax, ax.twinx()]
  # # Move the last y-axis spine over to the right by 20% of the width of the axes
  axes[1].spines['right'].set_position(('axes', 1.0))

  axes[0].plot(datetime(df[X_FIELD]), df[YA1_FIELD], label = YA1_LABEL, c = 'red')
  axes[0].plot(datetime(df[X_FIELD]), df[YA2_FIELD], label = YA2_LABEL, c = 'blue')
  axes[0].set_ylabel(YA_AXIS_LABEL)
  axes[0].set_xlabel(X_AXIS_LABEL)
  
  # Graph 02 -- all the axes[1] variables refer to graph 2
  axes[1].plot(datetime(df[X_FIELD]), df[YB_FIELD], label = YB_LABEL, c = 'green')
  axes[1].set_ylabel(YB_AXIS_LABEL)

  axes[0].legend(loc = 'upper left')
  axes[1].legend(loc = 'upper right')


  date_formatter = '%d'
  xfmt = DateFormatter(date_formatter)
  # xaxis set up
  axes[0].xaxis.set_major_locator(DayLocator())
  axes[0].xaxis.set_major_formatter(xfmt)
  axes[0].xaxis.set_minor_locator(HourLocator())

  plt.xlabel(X_AXIS_LABEL)
  plt.legend()
  plt.grid(linestyle='dotted')
  plt.title(title)
  plt.tight_layout()
  plt.savefig('%s_%s.png'%(OUT_FILE_NAME,zone_name),dpi=300)


def plot_all_graphs():
  # Read the file into the code.
  df = pd.read_csv(FILE_NAME, parse_dates = [X_FIELD])

  if ALL_ZONES:
    for zone_name in df[ZONE_FIELD].unique():
      dfz = df[df[ZONE_FIELD] == zone_name]
      title = '%s for %s' % (TITLE, zone_name)
      start_day = dfz[X_FIELD].min().date()
      end_day = dfz[X_FIELD].max().date()

      mask = (dfz['BINDING_TIMESTAMP'] >= start_day) & (dfz['BINDING_TIMESTAMP'] < end_day)
      _dfz = dfz[mask]
      
      plot_graph(_dfz, title,zone_name)
  
  else:
    plot_graph(dfz, TITLE,'')


# *********************************************************




# ---------------------------------------------------------
# MAIN
if __name__ == '__main__':
  # Set configuration here.
  X_FIELD = 'BINDING_TIMESTAMP'
  YA1_FIELD = 'RTD_DNI'
  YA2_FIELD = 'RTC15_DNI'
  YB_FIELD = 'RTC_RTD_DIV'
  
  TITLE = ''
  X_AXIS_LABEL = 'Binding Timestamp'
  YA_AXIS_LABEL = 'DNI (MW)'
  
  YA1_LABEL = 'RTD_DNI'
  YA2_LABEL = 'RTC15_DNI'
  
  YB_AXIS_LABEL = ''
  YB_LABEL = 'LBMPdiv($/min)'
  
  ALL_ZONES = True
  ZONE_FIELD = 'ZONE_NAME'
  
  FILE_NAME = 'RTC_RTD_DNI_LBMP_EXT.csv'
  FORMATTER = 'daily'
  
  OUT_FILE_NAME = 'RTC_RTD_DNI_LBMP_EXT'
  
  plot_all_graphs()

# *********************************************************