In [ ]:
import numpy as np
A simple function with dependence on only one independent variable might be written to a file like this:
In [ ]:
x1 = np.linspace(0, 10, 9)
y1 = x1**2
np.savetxt('test_data_001.txt', (x1,y1))
or like this:
In [ ]:
x2 = np.linspace(0, 1, 100)
y2 = np.exp(x)
np.savetxt('test_data_002.txt', (x2,y2))
Note the use of np.linspace to give you uniform grid spacing for your data points.
Making two dimensional data is a bit more involved. In the case below we create a set of $(x,y)$ pairs and then use np.meshgrid to facilitate vectorizing the calculation of the value stored in z3. For this function we use the discrete values of X and Y to compute the quantity:
We store this in z3 and write to a file.
In [ ]:
x3 = np.linspace(0, 10, 100)
y3 = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x3, y3)
z3 = np.sin(X)**2*np.cos(Y)**2
np.savetxt('test_data_003.txt', (z3))
The benefit in using the np.savetxt function is that you can use the inverse np.loadtxt easily. Here we load the test data file back into a variable testZ and pass that array to an image.
In [ ]:
%matplotlib notebook
import matplotlib.pyplot as plt
from matplotlib import cm
testZ = np.loadtxt('test_data_003.txt')
# Make plot with vertical (default) colorbar
fig, ax = plt.subplots()
cax = ax.imshow(testZ, interpolation='nearest', cmap=cm.coolwarm)
ax.set_title('Scalar Field')
plt.show()
When I refer to the idea of "creating your own data" for use with numerical routines, the above is what I'm thinking.