Plotting the Data¶
There are several possibilities to plot data. Most important is to include error bars for measured data. The plotting used here is based on matplotlib. In you analysis you typically plot raw data, or calculated values and error bars together with the result of a fit. If you analyze an experiment that involves counting of radom events, histogramming is frequently used together with its plotting capabilities. Some of the most frequently used functions included in LT/LT_Fit are described in the following.
Plot Columns of a Data File (pdfile
)¶
Very often you would like to just quickly have a look at the
data in your file. For this the function dplot_exp()
is very useful.
You can plot the values of the various columns in your datafile versus
each other. As an example I plot the time versus position of the data
in mf
(my_exp_1.data) including the errors. Remember time was
called time
, position was called dist
and its error was called
d_err
. You can always get the names of all the variables in you
pdfile
object by doing :
In [21]: mf.show_keys()
In order or get a quick look at the data in data file you can use the following plotting command:
In [22]: B.dplot_exp(mf, 'time', 'dist','d_err')
You can even combine the loading of the file and the plotting into a single line (this is useful if you would like to have a quick look at data while you are taking them:
In [22]: B.dplot_exp(B.get_file('me_exp_1.data'), 'time', 'dist','d_err')
In this case we replace the mf
by the call B.get_file('me_exp_1.data')
.
If you do not have errors or do not want to plot them just leave them off. If you cannot see a plot enter the command:
In [22]: show() # or
In [22]: B.pl.show()
This assures that the plot is shown and updated. It is especially important to issue this command in a script (see Python Scripts)
g*Example:* clear a previous figure and plot some values in a data file with a one-line command:
In [22]: clf(); B.dplot_exp( B.get_file('my_exp_1.data'), 'time', 'dist','d_err'); show()
This is a simple way to take a quick look at data stored in a datafile.
Plotting Arrays of Data¶
Since you often have the data as a numpy.array()
you need
be able to plot them. For experimental data with error bars you should use the function plot_exp()
.
This will be your most frequently used function to plot data.
As an example we use the arrays from the previous example namely t
, dexp
and
derr
. To plot those you do:
In [25]: B.pl.clf() # clear the figure
In [26]: B.plot_exp(t, dexp, derr); B.pl.show() # show the new plot wit error bars
If you have additional errors in the independent variable (x
) you can add them with
the statement xerr = sigma_x
where sigma_x
stands for the array
containing your x
errors.
To make a plot where you join the data points with a line you use the function plot_line()
.
This function is generally used when you plot a function representing a theoretical result or a fit result.
A simple example is show below:
In [26]: B.plot_line(t, dexp); B.pl.show() # join the data points with a line
Normally one does not join experimental data with lines, unless you really want to highlight a trend and/or has very many data points.
Note that this time the title and the labels are None. You can add the
labels as shown later. The plot looks the same as before. You
can leave off the errors if you leave off derr
in the arguments to
B.plot_exp
. There are many so-called keyword arguments to further
control the appearance. You can get more information on
plot_exp()
or plot_line()
and
even more on matplotlib.pyplot.plot()
since this is the function
that the LT.plotting
functions are based upon. You should also
familiarize yourself with how keywords are used.
Using a log-scale¶
If you need a log scale enter:
In [22]: B.pl.yscale('log'); show() # show() on the same line
This is also an example where two commands are given on one line. One
can enter several commands on one line if they are separated by a
;
.
To switch back to a linear scale enter:
In [23]: B.pl.yscale('linear'); show()
Labeling the Plot¶
Every plot needs to be properly labeled in order that the viewer knows what is shown. At the least the x-axis and y-axis need labels but it is often good practice to add a title to the plot. You might also want to change the range of the x- or y-axis in order to show the viewer the important part of the plot with greater detail. This can be achieved by changing the axis limits. The commands for these tasks are shown below (make a not of these as you will use them often):
to change |
do this |
---|---|
x-axis label |
B.pl.xlabel( ’ a new x axis label’ ) |
y-axis label |
B.pl.ylabel( ’ a new y axis label’ ) |
plot title |
B.pl.title(’A new Plot title’) |
x-axis limits |
B.pl.xlim ( (xmin, xmax ) ) |
y-axis limits |
B.pl.ylim( (ymin, ymax) ) |
Note that the two parenthesis are needed since the limits are entered as
so-called tuples. These are basically as sequence of objects (numbers,
strings etc.) which cannot be changed. If you enter the command
B.pl.xlim()
you will get back the current limits. If you want to know
more about tuples have a look at the Python documentation.
Finally, below is the final plot including all the commands necessary:

Save the plot¶
You can save the plot as a pdf file by giving the command:
In [24]: B.pl.savefig('my_plod.pdf')
Note that all the commands that adjust the plot start with B.pl
. The
reason is that the LT.box
module itself imports the matplotlib
plotting module and gives it the name B.pl
. Frequently (especially if you are using Spyder)
matplotlib.pyplot
and numpy
have already been
imported. However when you run a script (Python Scripts) you need to import them
explicitly and it is therefore a better practice to use B.pl
in
front of the commands (assuming you did import LT.box as B
) to get
used to it.