Skip to content
Snippets Groups Projects
Commit 7103b534 authored by Frank Sauerburger's avatar Frank Sauerburger
Browse files

New section about data points

Adding a new section explain how to plot data points on top of the expected
cropped parabola.
parent fc2aac4b
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -202,3 +202,75 @@ cropped_parabola.png
```
-->
# Plotting Data Points
A typical task in the advanced laboratory might be to compare measured data
points to an expected function. Lets assume the expected function is the cropped
parabola $`f(x)`$ from the previous examples. We will use random data points in
this example, since we haven't actually measured real data, which is expected to
follow $`f(x)`$.
This example is based on the code from the previous example. Copy the file from
the previous examples to `data_plot.py`, such that we an append to it and keep
the function plotting code. Please add the code snippets in this section to the
file `data_plot.py`.
<!-- console
```bash
$ cp func_plot.py data_plot.py
```
-->
We generate the pseudo data points by adding random deviations to the expected
y-values. Lets assume we measured data points for all half-integer x-values.
<!-- append data_plot.py -->
```python
x_data = np.array([-2.5, -2, -1.5, -1, -.5, 0, .5, 1, 1.5, 2, 2.5, 3])
```
We evaluate the function $`f(x)`$ again for the `x_data` values. The random
deviations are drawn from a centered normal distribution with a standard
deviation of 0.3. The third argument of `numpy.random.normal` specifies, how
many random samples we want to draw. We use `len(x_data)`, since we want to draw
a random deviation for each x-value.
<!-- append data_plot.py -->
```python
y_data = x_data**2
y_data[x_data >= 2] = 4
y_data += np.random.normal(0, 0.3, len(y_data))
```
Finally we can add this to our plot. Since our data points are subject to
statistical fluctuations, we would like to use matplotlib's `errorbar` method,
which draw our data points with errorbars.
<!-- append data_plot.py -->
```python
plt.errorbar(x_data, y_data, 0.3, fmt="ko", capsize=0)
plt.savefig("measurement.eps")
```
The character `k` in the format parameter `fmt` sets the color to blac*k*, the
`o` in `fmt` changes the style to *large dots*. The optional parameter `capsize`
modifies the style of the error bars. You can play with these options and see
what happens or have a look at the
[documentation](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.errorbar.html).
<!-- append data_plot.py
```python
# We need also a png version of the plot to embed it into the README.md.
plt.savefig("measurement.png")
```
-->
<!-- console_output
```
$ python3 data_plot.py
$ ls measurement.eps
measurement.eps
$ ls measurement.png
measurement.png
```
-->
After running `data_plot.py` you should have a plot similar to this.
![Plot of cropped parabola with data points](https://srv.sauerburger.com/esel/FP-python-examples/-/jobs/artifacts/master/raw/measurement.png?job=doxec_test)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment