I'm using .imshow()
to show a 2D array from Z(X, Y), and I would like the axes to reflect the original x and y values used for the function, instead of the array indices.
But I would like Matplotlib to use it's tick marking algorithm to places ticks at "nice" numbers. Below I show what those might look like.
One way to do this would be to "extract" the tick mars from the two 1D plots and "implant" them on the imshow, but I don't know how to do that.
I've tried some searching but I can't even think of good matplotlib search terms to describe what I need.
def Z(X, Y):
Rsq = X**2 + Y**2 - 0.025*X**4
return np.exp(-0.2*Rsq)*np.cos(2.*Rsq)
import numpy as np
import matplotlib.pyplot as plt
halfpi, pi, threehalfpi, twopi = [f*np.pi for f in (0.5, 1, 1.5, 2)]
x = np.linspace(-twopi, pi, 450)
y = np.linspace(-threehalfpi, pi, 350)
X, Y = np.meshgrid(x, y, indexing='xy')
z1 = Z(X, Y)
z2 = Z(x, 0)
z3 = Z(0, y)
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 2)
ax1.imshow(z1, origin='lower')
ax1.xaxis.tick_top()
ax1.yaxis.tick_right()
plt.tick_params(axis='both', which='major', labelsize=14)
ax2 = fig.add_subplot(2, 2, 4)
ax2.plot(x, z2)
ax2.set_xlim(x.min(), x.max())
ax2.xaxis.tick_top()
ax2.yaxis.tick_right()
plt.tick_params(axis='both', which='major', labelsize=14)
ax3 = fig.add_subplot(2, 2, 1)
ax3.plot(z3, y)
ax3.set_ylim(y.min(), y.max())
ax3.yaxis.tick_right()
plt.tick_params(axis='both', which='major', labelsize=14)
plt.show()
print 'x: ', x.min(), x.max()
print 'y: ', y.min(), y.max()