5. Tips and Examples > 1. Plotting
Plotting Examples
Author: Daniel Brown
This notebook is to give some examples of the builtin plotting. This can be very useful for just producing simple plots quickly. There is some flexibility provided to customise the plot. But if something more specific is required you'll have to resort to using Matplotlib yourself.
Firstly we'll run a simple cavity simulation to get some data to actually plot with:
import pykat
pykat.init_pykat_plotting()
from pykat import finesse
code = """
l l1 1 0 0 n1
s s1 10 1 n1 n2
m m1 0.9 0.1 0 n2 n3
s s2 10 1 n3 n4
m m2 0.91 0.09 0 n4 n5
ad refl 0 n2
ad circ 0 n4
ad tran 0 n5
pd pd_cav n3
noplot refl
xaxis m2 phi lin 0 360 720
yaxis abs:deg
"""
kat = finesse.kat()
kat.parse(code)
out = kat.run()
The plotting is all done via the output object returned after running the simulation. The best source of information on what options are available is the plot function help.
help(out.plot)
This is the simplest option, you just call out.plot()
and it will plot everything that has been outputted.
# Plot everything using default settings of the Finesse file
fig = out.plot()
By default the plot function will use the yaxis
set in the kat.yaxis
object property. You can quickly change this (if you forgot to set it to a log scale, for example) then you can use the yaxis argument:
fig = out.plot(yaxis="log abs")
The yaxis value should be the same as used in a Finesse script (See http://www.gwoptics.org/finesse/reference/). You can also specify to see the phase information if available:
fig = out.plot(yaxis="log abs:deg")
If you just want to see certain detector outputs from your simulation you can easily just list which outputs you want to see:
fig = out.plot(detectors=["refl","tran"],
yaxis="log abs:deg")
You change the style of each of the detector outputs by providing a dictionary of styles. The keys of this dictionary are the names of the output, the value is the usual matplotlib style string:
fig = out.plot(styles={"refl":'k:', 'circ':'go'},
yaxis="log abs:deg")
fig = out.plot(title="Run with some setting blah",
yaxis="log abs:deg",
ylabel="My ylabel",
xlabel="My xlabel",
y2label="My y2label",
x2label="My x2label",
legend=False)
You can save the final output to a file by specifying a filename and correct extension:
fig = out.plot(filename="my_plot.png",
show = False,
title="Run with some setting blah",
yaxis="log abs:deg",
ylabel="My ylabel",
xlabel="My xlabel",
y2label="My y2label",
x2label="My x2label",
legend=False)
Here is the plot shown in HTML using <img src="./my_plot.png"/>
: