51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
|
|
""" Task3: implementation """
|
|
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
def plot_discrete_function(csvfile, delimiter, comments, pngfile):
|
|
"""
|
|
Reads a csv-file containing discretized value of a function (f), its derivative (df) and antiderivative (F)
|
|
and plots all three functions over the discrete value of the interval (x).
|
|
|
|
Expected csv-column layout:
|
|
x, f, F, df
|
|
|
|
Requirements for the plot:
|
|
- axis labels
|
|
- a legend for the plotted data records
|
|
|
|
Implementation hints:
|
|
- use numpy.loadtxt(...) for loading the data from the csvfile
|
|
- use Matplotlib for plotting
|
|
|
|
Parameters
|
|
----------
|
|
csvfile : string
|
|
Name of the csv-file containing the discrete function
|
|
delimiter: character
|
|
Charater used to delimit individual values in the rows
|
|
comments: character
|
|
Charater (when used as first character in a row) denoting comment lines
|
|
pngfile : string
|
|
Name of the file where the plot is saved
|
|
"""
|
|
# load data from csv file
|
|
data = np.loadtxt(csvfile, delimiter=delimiter, comments=comments)
|
|
x = data[:, 0]
|
|
f = data[:, 1]
|
|
F = data[:, 2]
|
|
df = data[:, 3]
|
|
|
|
# plot the data
|
|
plt.plot(x, f, label='f')
|
|
plt.plot(x, F, label='F')
|
|
plt.plot(x, df, label='df')
|
|
plt.xlabel('x')
|
|
plt.ylabel('y')
|
|
plt.legend()
|
|
plt.savefig(pngfile)
|
|
plt.close('all')
|