First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!

Ask Your Question
1

Plot Legend for Dyntools

asked Jan 22 '16

IanMelbourne93 gravatar image

updated Jan 22 '16

tl;dr - How do I change the legend in dyntools plots?

At the moment, I am trying to compare two .out files which are both saved with the same file name in different folders. The .out files are of the same case with slight differences and therefore have the exact same channels which is the point of the code, to compare the channels with slight differences. My problem is that I cannot seem to find any option to change the legend of the plots. I can plot the files fine, its just that the legend has the same channel data with either an A or a B next to it. There is references at the top of the file as to which folder they came from, however I would like to make this a lot clearer and put it in the legend of the graph itself. I also find, that occasionally the plots are backward with the usual blue plot being printed in red and vice versa. Image of current plot cannot be uploaded due to low karma.

code for those interested (although probably unnecessary:

def plot_out_file(folder=[], outfile='', chans=None):
"""  Plot specific channels from the out file specified and save as subplots on one image.

:param folder: Folder location that the outfile can be found
:param outfile: The file name of the out file to be plotted
:param chans: The channels of the out file to be plotted
:return:
"""

# if there are no channels, assume all channels
if chans is None:
    chans = []
chnfobj, optnchn, optnppg, optnfmt = [], [], [], []
# Load the outfile and extract the data

outlist = []
if isinstance(folder, list):
    for fol in folder:
        outlist.append(os.path.join(fol, outfile))
    chnfobj = (dyntools.CHNF(outlist))

else:
    chnfobj = dyntools.CHNF(os.path.join(folder, outfile))

sh_ttl, ch_id, ch_data = chnfobj.get_data()

# Determine the structure of the subplots (rows and columns based on the number of channels)
count = len(chans)
if (count == 1):
    nRows = 1
    nCols = 1
elif (count == 2):
    nRows = 2
    nCols = 1
elif (count == 3):
    nRows = 3
    nCols = 1
elif (count == 4):
    nRows = 2
    nCols = 2
elif (count in [5, 6]):
    nRows = 4
    nCols = 2
elif (count in [7, 8, 9]):
    nRows = 4
    nCols = 2
elif (count in [10, 11, 12]):
    nRows = 4
    nCols = 3
else:
    nRows = 4
    nCols = 2

# Determine the options for the subplots
optnchn = dict()
optnppg = {'size': 'A3', 'orientation': 'Portrait'}  # 'Letter', 'Legal', 'A3', 'A4',     #'Portrait', 'Landscape'
optnfmt = {'rows': nRows, 'columns': nCols, 'dpi': 300, 'showttl': True}

plotID = 1
for i in range(0, len(chans)):
    ch = dict()
    for fol in folder:
        ch[os.path.join(fol, outfile)] = chans[i]

    if isinstance(chans[i], list):
        id = ch_id[chans[i][0]]
    else:
        id = ch_id[chans[i]]
    optnchn[plotID] = {'chns': ch,
                       'title': id,
                       'xscale': [0, 15]}
    plotID = plotID + 1
time = str(datetime.datetime.now().strftime("%Y%m%d%H%M"))
# print os.path.join(folder, outfile)
# Plot the out file and save with the same file name with a *.png attachment
file_str = os.path.join(folder[0], outfile).replace('.out', '.png')
file_str = file_str ...
(more)

Comments

Matplotlib is much more powerful then dyntools. Use dyntools to extract data from the out files and then plot with matplotlib

IanMelbourne93 gravatar imageIanMelbourne93 (Feb 14 '16)

2 answers

Sort by » oldest newest most voted
1

answered Jan 29 '16

SqFKYo gravatar image

Have you considered using the matplotlib library bundled with the PSS/E? Sample code after you have your time, and channel data read into memory:

from matplotlib import pyplot as plt
plt.plot(time_data, channel_one_data, label='channel_one')
plt.plot(time_data, channel_two_data, label='channel_two')
plt.legend()
plt.show()
link

Comments

hi. how can I xtract channel 1 data?

Karma1 gravatar imageKarma1 (Jan 17 '17)
1

answered Sep 5 '17

jconto gravatar image

updated Sep 5 '17

The dyntools module is a basic, 'hardcoded' implementation of plotting with matplotlib, where customization (positioning of legends, etc.) is not allowed. BTW, the above code did not work in my pc, so I made a few mods to it:

import os,sys
#---User vars   -----------------
psseversion = 33
folder  = r'C:\Users\xx\Documents\Code\Python\pti\EXAMPLE33'
outfile = r"bus154_fault.out"
chans   = [41,42]
#--------------------------------
if os.environ.has_key('PSSEVERSION'):       #STR returned!
   psseversion = int(os.environ['PSSEVERSION'])
if psseversion== 34 or psseversion== 33:
   exec('import psse%s'%psseversion)
else:
   pssepath = r"C:\Program Files (x86)\PTI\PSSE%s\PSSBIN"%psseversion
   if pssepath not in sys.path:
      sys.path.append(pssepath)
#--------------------------------
import psspy
#psspy.psseinit(15000)
#--------------------------------
import dyntools
import matplotlib.pyplot as plt
import time, datetime

def pltarray(chans):
  # Determine the structure of the subplots (rows and columns based on the number of channels)
  if not len(chans)>0:
     return 1, 1
  count = len(chans)
  if (count == 1):
      nRows = 1
      nCols = 1
  elif (count == 2):
      nRows = 2
      nCols = 1
  elif (count == 3):
      nRows = 3
      nCols = 1
  elif (count == 4):
      nRows = 2
      nCols = 2
  elif (count in [5, 6]):
      nRows = 4
      nCols = 2
  elif (count in [7, 8, 9]):
      nRows = 4
      nCols = 2
  elif (count in [10, 11, 12]):
      nRows = 4
      nCols = 3
  else:
      nRows = 4
      nCols = 2
  return nRows, nCols

#--------------------------------
def plot_out_file(folder=[], outfile='', chans=None):
  """  Plot specific channels from the out file specified and save as subplots on one image.
      :param folder: Folder location that the outfile can be found
      :param outfile: The file name of the out file to be plotted
      :param chans: The channels of the out file to be plotted
      :return: outfile
  """

  # if there are no channels, assume all channels
  if chans is None:
     chans = []
  chnfobj, optnchn, optnppg, optnfmt = [], [], [], []
  # Load the outfile and extract the data 
  outlist = []
  if not isinstance(folder, list):
     folder = [folder,]
  print ' 0 folder=', folder
  for fol in folder:
      outlist.append(os.path.join(fol, outfile))
  chnfobj = dyntools.CHNF(outlist)
  sh_ttl, ch_id, ch_data = chnfobj.get_data()

  nRows, nCols = pltarray(chans)

  # Determine the options for the subplots
  optnchn = dict()
  #optnppg = {'size': 'A3', 'orientation': 'Portrait'}  # 'Letter', 'Legal', 'A3', 'A4',     #'Portrait', 'Landscape'
  optnfmt = {'rows': nRows, 'columns': nCols, 'dpi': 300, 'showttl': True}

  plotID = 1
  for i in range(0, len(chans)):
      ch = dict()
      for fol in folder:
          ch[os.path.join(fol, outfile)] = chans[i]

      if isinstance(chans[i], list):
          id = ch_id[chans[i][0]]
      else:
          id = ch_id[chans[i]]
      optnchn[plotID] = {'chns': ch,
                         'title': id,
                         'xscale': [0, 15]}
      plotID = plotID + 1
  time = str(datetime.datetime.now().strftime("%Y%m%d%H%M"))
  # print os.path.join(folder, outfile)
  # Plot the out file and save with the same file name with a *.png attachment
  print 'folder=', folder
  file_str = os.path.join(folder[0], outfile).replace('.out', '.png')
  file_str = file_str.split(".png", 1)
  file_str = file_str[0] + "_" + str(datetime.datetime.now().strftime("%Y%m%d_%H%M%S")) + ".png"
  # file_str = file_str[0] + ".png"
  figfiles = chnfobj.xyplots(optnchn, optnfmt, file_str)
  if figfiles:
      print 'Plot fils saved:'
      for f in figfiles:
          print '    ', f
  chnfobj ...
(more)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.
Want to format code in your answer? Here is a one minute demo on Youtube

Add Answer

[hide preview]

Question Tools

Stats

Asked: Jan 22 '16

Seen: 3,509 times

Last updated: Sep 05 '17