Skip to content

Instantly share code, notes, and snippets.

@mttmantovani
Created February 10, 2019 16:45
Show Gist options
  • Save mttmantovani/7a1d14290359e03a41d3c0f15b8c7dd9 to your computer and use it in GitHub Desktop.
Save mttmantovani/7a1d14290359e03a41d3c0f15b8c7dd9 to your computer and use it in GitHub Desktop.
2dplot example - Python
import numpy as np
import matplotlib.pyplot as plt
# Generate fake data
x = np.linspace(-5, 5, 151);
y = np.linspace(-3, 3, 251);
X, Y = np.meshgrid(x, y)
Z1 = np.sin(X*Y)**2 - np.cos(X*Y)**2 - X - Y
# Save data
coords = np.vstack((X.flatten(), Y.flatten())).T
data = np.vstack((coords.T, Z1.flatten())).T
np.savetxt('example.dat', data)
# Visualize
plt.figure()
plt.title('Z1')
plt.imshow(Z1, extent=[x.min(), x.max(), y.min(), y.max()],
origin='lower', aspect='auto',
interpolation='lanczos')
plt.colorbar()
plt.xlabel('x')
plt.ylabel('y')
# Reload data and replot
data = np.loadtxt('example.dat')
x = np.unique(data[:,0])
y = np.unique(data[:,1])
Z2 = data[:,2].reshape(len(y), len(x))
plt.figure()
plt.title('Z2')
plt.imshow(Z2, extent=[x.min(), x.max(), y.min(), y.max()],
origin='lower', aspect='auto',
interpolation='lanczos')
plt.colorbar()
plt.xlabel('x')
plt.ylabel('y')
# Show
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment