3. Plotting - Spinmob/spinmob GitHub Wiki

Importing Spinmob

import spinmob as s

Plot types

Under the s.plot you will find several types of plots that can be quickly generated:

s.plot.complex
s.plot.image
s.plot.magphase
s.plot.parametric
s.plot.realimag
s.plot.xy

(The options style and tweaks are for modifying how things are plotted.) By far the most common plot is xy, which, similar to pylab.plot, plots y-data versus x-data.

If you select one of these (xy, for example) and type another ., you'll see a list of plot methods: data(), databoxes(), files(), and function(). The method data is the lowest level; you supply arrays of data and it makes the plot for you. There are LOTS of optional arguments in this function, so its worth entering the command

help(s.plot.xy.data)

and taking a moment to see everything that's possible (especially common is clear=False so the previous plot remains). The data method is used by all of the other plotters (except those in the image group):

  • databoxes() takes a list of databoxes and scripts for generating the x, y, and error data for each (see the "Data Handling" section).
  • files() takes a list of files (or prompts the user to select files!) and scripts for generating x, y, and error data.
  • function() takes a string function (e.g. 'sin(x)'), a range over which to plot, and the number of plotted points.

All of these higher-level functions share the same optional arguments as the data() method (in addition to their own options), and all of these functions share optional arguments with the underlying pylab plotters (e.g. marker='o' or linestyle='').

To test that spinmob is working, I'd recommend trying a few things like

s.plot.xy.data([1,2,3], [1,2,1])
s.plot.xy.data([1,2,3],[2,3,4](/Spinmob/spinmob/wiki/1,2,3],[2,3,4), [1,2,1],[1,3,1](/Spinmob/spinmob/wiki/1,2,1],[1,3,1))
s.plot.xy.data([1,2,3], [1,2,1],[1,3,1](/Spinmob/spinmob/wiki/1,2,1],[1,3,1), marker='o', linestyle='')

and making sure the output is what you expect. You can also optionally specify y and/or x error bars, e.g.:

s.plot.xy.data([1,2,3], [1,2,1],[2,3,0](/Spinmob/spinmob/wiki/1,2,1],[2,3,0), [0.7,0.1,2], 0.2, marker='o', linestyle='', label=['Fake Data 1', 'Fake Data 2'], xlabel='Fake x (Hz)', ylabel='Fake y (V)')

Note the format of these quick plots is geared toward lab notebooks, not publication. In particular, the title provdes the date of creation, along with the command (or path to the script) used to create the plot. It is right-justified with a reasonably small font so that long commands or paths (that might be clipped) will show the most relevant information. Also, the legend is outset, to guarantee it will not overlap the data, and has a small enough font to support many curves. If you wish to convert it to a more publishable format, you can try s.plot.tweaks.ubertidy(keep_legend=False) (which has many options):

or just use pylab directly for more low-level control. Another useful one is function(), e.g.

s.plot.magphase.function(['sin(1j+x*x)**2*x', 'erf(1j+x)'], -5, 5)

and many of these functionalities exist for image plots as well, e.g.,

s.plot.image.function('sin(x*y**2)',-5,5,-2,2, 200,200)

produces

with interactive sliders to update the color map.

At this point it's best to start exploring on your own and reading the documentation of each plot type using the help() interface.

Plotting Data Files

Spinmob has some high-level functions to help it plot things quickly with pylab (i.e. replacing the same 10 lines of code I kept writing over and over again). I'll emphasize that underneath these functions are just plain old pylab commands, so all of your pylab knowledge can be used in parallel or as a replacement. You can also access all of it by typing s.plot. and s.tweaks or s.plot.tweaks (same thing). All of pylab is also available via s.pylab.

For example, if you want to quickly load a bunch of data files and plot column 0 versus column 2 with a per-file offset of 10,

s.plot.xy.files(0,2,yshift=10)

Notice how this is geared toward lab notebooks: the title shows the directory of the data files (right-justified; if it's a very long path, the most important directories will be visible), and the legend shows the file associated with each curve. The x and y axes labels show what script you supplied by default. If you want to quickly load a bunch of data files and plot some scripted combination of columns and header elements, you can, using something like

s.plot.xy.files('d[0]*1000', 'd[2]*cos(d[0])+h("Iterations")', xlabel='Time (ms)')

This will work so long as the code is valid python that could be called with eval(), columns 0 through 2 exist and are the same length, and the header element "Iterations" is a number or array. Within the plot scripts, d is the databox loaded for each file, c() is a shortcut to d.c(), h() is a shortcut to d.h(). The script interpreter also knows about all numpy and scipy special functions, e.g. cos(), sqrt(), and erf().

It is also possible to plot several scripted curves for each file using lists, similar to s.plot.xy.data() above. Alternatively, if you have a bunch of files and want to plot one data point per file, this is also possible, with scripts like

s.plot.xy.files('mean(d[1][1:30])','max(d[2])+h("Iterations")', marker='o')

Notice the legend shows the range of files covered by the data set. Check out help(s.plot.xy.files) to learn about the various options. In fact, use help() for everything. This is where the bulk of the documentation lives. A good IDE will also provide a help function that formats the help text for easier reading.

Tweaking Existing Plots

Once you have data plotted, there are a whole mess of functions to tweak these plots (most apply to 1d plots) found in

s.tweaks

for short. This includes

  • s.tweaks.coarsen_all_traces() to average groups of data points into single points.
  • s.tweaks.ginput() (same as s.pylab.ginput()) to turn mouse clicks into x,y values.
  • s.tweaks.integrate_shown_data() to plot the running integral of the shown data.
  • s.tweaks.set_all_line_attributes() to set a matplotlib attribute for all lines.
  • s.tweaks.shift() to shift the lines.
  • s.tweaks.ubertidy() to automatically configure the plot with a variety of options.

and a whole lot more! Use the code suggestions and help features of your python environment to explore.

Dark Theme

Work in a dark lab and / or want to reduce eye strain? Spinmob now offers a dark theme for figures. To enable this, run the following command once:

s.settings['dark_theme_figures'] = True

Restart the console and your plots will start looking more like this:

Note you can also put the rest of the GUI (Qt) into a dark theme with the following command:

s.settings['dark_theme_qt'] = True