I have a 900X900 2-d numpy array that I plotted using the matshow() method, the result is great but I don't want to show the position of the pixel on the axis, rather, I want to label the x-axis and y-axis with different strings at specified locations(eg. x=50, x=200, x=700 ,y=50, y=200, y=700). I also want to only show x and y ticks at specified locations(eg.x=100, x=400, and x=900). So, how to do it? This is my sample code
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
two_d_array_900 = np.random.rand(900,900)
print(two_d_array_900)
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111)
color_map = plt.cm.get_cmap('PiYG')
max_value = np.nanmax(two_d_array_900)
min_value = np.nanmin(two_d_array_900)
middle_value = (max_value+min_value)/2
norm_thresholded = cm.colors.DivergingNorm(vmax = max_value,
vcenter=middle_value,vmin=min_value)
divider = make_axes_locatable(ax)
cax_for_colbar= divider.append_axes("right", size="5%", pad=0.05)
im = ax.matshow(two_d_array_900, cmap=color_map, norm=norm_thresholded)
fig.colorbar(im, cax=cax_for_colbar)
ax.set_title("test", fontsize=15, y=1, pad=10)
This is the original figure
This is the desired figure
Read more here: https://stackoverflow.com/questions/66271092/axis-label-with-different-string-with-different-xor-y-range
Content Attribution
This content was originally published by Jiang Xu at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.