Ask Your Question
1

Plot Legend for Dyntools

asked 2016-01-21 19:05:08 -0600

IanMelbourne93 gravatar image

updated 2016-01-21 19:06:20 -0600

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)
edit retag flag offensive close merge delete

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 ( 2016-02-14 16:27:26 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2016-01-29 02:16:49 -0600

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()
edit flag offensive delete link more

Comments

hi. how can I xtract channel 1 data?

Karma1 gravatar imageKarma1 ( 2017-01-17 12:57:40 -0600 )edit
1

answered 2017-09-05 11:14:10 -0600

jconto gravatar image

updated 2017-09-05 11:15:24 -0600

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)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

Stats

Asked: 2016-01-21 19:05:08 -0600

Seen: 3,501 times

Last updated: Sep 05 '17