I have some python code that applies a 2-D function over a range of x and y values, and uses matplotlib to plot the results as a colormap. However, the axes on this plot show the integer indexes of the output array. I would instead like these axes to show the range of the x and y values, from -1.0
to 1.0
, like a typical graphing application would look.
How do I set the range of the axes?
import matplotlib.pyplot as plt
# let x, y values be in range of (-1, 1)
x, y = np.mgrid[-1:1:.05, -1:1:.05]
# Apply the function to the values
z = x * y
# Get matplotlib figure and axis
fig, ax = plt.subplots(figsize=(3, 3), ncols=1)
# Plot the colormap
pos_neg_clipped = ax.imshow(z, cmap='RdBu', interpolation='none')
# Display the image
plt.show()
Output: