Python for Power Systems

A blog for power systems engineers to learn Python.

Matplotlib and PSSE

Graphics, when used correctly, can be the difference between an outstanding report and simply another piece of paper going around the office. The only thing more professional than a nice graphic, is a graphic that is automatically generated.

Exporting plots from the PSSE graphical interface can be a laborious task, with little flexibility in the final result. If you are running PSSE from Python, you have the option of looking beyond PSSE for tools to render your data.

matplotlib is a Python plotting tool which, given a little bit of effort, can make your life easier by automating figure generation; spend the time to get it right once, then generate the monthly figures with the press of a button.

matplotlib’s syntax is closely aligned with Matlab’s, which is fortunate if you are familiar with Matlab. If you are not familiar with them, don’t worry, the matplotlib community has provide an excellent tutorial and gallery displaying the immense possibilities of matplotlib (all examples come with code included for you to use as a starting point).

I’ll use matplotlib to create this chart:

It is the QV curve at bus 20001 (my favourite bus as it happens) and was plotted from data kept in a CSV file (qv.csv).

Here is the code we used to create that chart.

QV curve plot with Matplotlib - plotqv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from __future__ import with_statement
import csv

from pylab import plot, title, show, grid, xlabel, ylabel

# CSV file in format: V, Q
QV_CSVFILENAME = 'qv.csv'

def main():
  with open(QV_CSVFILENAME) as qvcsvfile:
      reader = csv.reader(qvcsvfile)
      # skip header row.
      header = reader.next()
      voltage_and_reactive = list(reader)

  # now convert the voltages and power values to floats.
  voltages, reactive = [], []
  for voltage, q in voltage_and_reactive:
      voltages.append(float(voltage))
      reactive.append(float(q))

  # now make the chart.
  plot(voltages, reactive, '-o')
  title("QV curve for bus number 20001")
  ylabel("Reactive Power [MVAr]")
  xlabel("Voltage [pu]")
  grid()
  show()

if __name__ == '__main__':
  main()

I’m using the pylab module of matplotlib. The pylab module most closely resembles Matlab code so its a really familiar place to start learning how to use matplotlib.

The first two sections open up the CSV file and convert it into a list of voltage and reactive floating point values.

Now, I’ll pull a section of that code out so that we can look closely:

creating the chart
1
2
3
4
5
6
7
   # now make the chart.
  plot(voltages, reactive, '-o')
  title("QV curve for bus number 20001")
  ylabel("Reactive Power [MVAr]")
  xlabel("Voltage [pu]")
  grid()
  show()

Thats the code that actually turns the lists of voltages and reactive values into a chart. The functions describe themselves pretty well. At the end of the routine show will pop up an interactive graph.

With the interactive graph you can:

  • zoom in and out;
  • pan around; and
  • export the chart to an image file.

If you haven’t ever used matplotlib go ahead and try this example out. Before you do run the Python code here, just make sure you have both matplotlib and numpy installed. Install numpy first because matplotlib definitely needs it and will not install without it.

You’ve seen how easy it can be to start making your own charts with matplotlib. The quality of the resulting images is really exceptional. We’ve used them throughout many industry published documents. If you have any questions about making your own charts, jump onto our forum for power systems engineers we’ll help you out.

If you made it this far, you must be pretty keen on Python. We are too! Let us update you when we have something new to say about Python and Power Systems (semi regularly). Get Python Tips (subscribe on the right) →