Examples of how to change imshow axis values (labels) in matplotlib:
Let's consider a simple figure using matplotlib imshow
import numpy as np
import matplotlib.pyplot as plt
def f(x,y):
return (x+y)*np.exp(-5.0*(x**2+y**2))
x,y = np.mgrid[-1:1:100j, -1:1:100j]
z = f(x,y)
plt.imshow(z)
plt.colorbar()
plt.title('How to change imshow axis values with matplotlib ?', fontsize=8)
plt.savefig("imshow_change_values_on_axis_01.png", bbox_inches='tight')
plt.close()
By default, the x and y values corresponds to the indexes of the array used as an input in the imshow function:
Change imshow axis values using the option extent
To change the axis values, a solution is to use the extent option:
extent = [x_min , x_max, y_min , y_max]
for example
plt.imshow(z,extent=[-1,1,-1,1])
Customize the axis values using set_xticks() and set_yticks()
Another solution is to use the matplotlib functions set_xticks() and set_yticks(). Fo example, with the option extent set up to [-1,1,-1,1], it is possible to replace the values [-0.75,-0.25,0.25,0.75] by ['A2', 'B2', 'C2', 'D2']:
fig, ax = plt.subplots(1,1)
img = ax.imshow(z,extent=[-1,1,-1,1])
x_label_list = ['A2', 'B2', 'C2', 'D2']
ax.set_xticks([-0.75,-0.25,0.25,0.75])
ax.set_xticklabels(x_label_list)
fig.colorbar(img)
Without using the option extent, it is necessary to use the array indexes to specify where to replace the values:
fig, ax = plt.subplots(1,1)
img = ax.imshow(z)
x_label_list = ['A1', 'B1', 'C1', 'D1']
ax.set_xticks([20,40,60,80])
ax.set_xticklabels(x_label_list)
fig.colorbar(img)
Another example using set_xticks() and set_yticks():
fig, ax = plt.subplots(1,1)
img = ax.imshow(z,extent=[-1,1,-1,1])
x_label_list = ['x1', 'x2', 'x3', 'x4']
y_label_list = ['y1', 'y2', 'y3', 'y4']
ax.set_xticks([-0.75,-0.25,0.25,0.75])
ax.set_yticks([-0.75,-0.25,0.25,0.75])
ax.set_xticklabels(x_label_list)
ax.set_yticklabels(y_label_list)
fig.colorbar(img)
Code python to test imshow axis values (labels) in matplotlib
import numpy as np
import matplotlib.pyplot as plt
def f(x,y):
return (x+y)*np.exp(-5.0*(x**2+y**2))
x,y = np.mgrid[-1:1:100j, -1:1:100j]
z = f(x,y)
plt.imshow(z)
plt.colorbar()
plt.title('How to change imshow axis values with matplotlib ?', fontsize=8)
plt.savefig("imshow_change_values_on_axis_01.png", bbox_inches='tight')
plt.close()
#----------------------------------------------------------------------------------------#
plt.imshow(z,extent=[-1,1,-1,1])
plt.colorbar()
plt.title('How to change imshow axis values with matplotlib ?', fontsize=8)
plt.savefig("imshow_change_values_on_axis_02.png", bbox_inches='tight')
plt.close()
#----------------------------------------------------------------------------------------#
fig, ax = plt.subplots(1,1)
img = ax.imshow(z)
x_label_list = ['A1', 'B1', 'C1', 'D1']
ax.set_xticks([20,40,60,80])
ax.set_xticklabels(x_label_list)
fig.colorbar(img)
plt.title('How to change imshow axis values with matplotlib ?', fontsize=8)
plt.savefig("imshow_change_values_on_axis_03.png", bbox_inches='tight')
plt.close()
#----------------------------------------------------------------------------------------#
fig, ax = plt.subplots(1,1)
img = ax.imshow(z,extent=[-1,1,-1,1])
x_label_list = ['A2', 'B2', 'C2', 'D2']
ax.set_xticks([-0.75,-0.25,0.25,0.75])
ax.set_xticklabels(x_label_list)
fig.colorbar(img)
plt.title('How to change imshow axis values with matplotlib ?', fontsize=8)
plt.savefig("imshow_change_values_on_axis_04.png", bbox_inches='tight')
plt.close()
#----------------------------------------------------------------------------------------#
fig, ax = plt.subplots(1,1)
img = ax.imshow(z,extent=[-1,1,-1,1])
x_label_list = ['x1', 'x2', 'x3', 'x4']
y_label_list = ['y1', 'y2', 'y3', 'y4']
ax.set_xticks([-0.75,-0.25,0.25,0.75])
ax.set_yticks([-0.75,-0.25,0.25,0.75])
ax.set_xticklabels(x_label_list)
ax.set_yticklabels(y_label_list)
fig.colorbar(img)
plt.title('How to change imshow axis values with matplotlib ?', fontsize=8)
plt.savefig("imshow_change_values_on_axis_05.png", bbox_inches='tight')
plt.close()
References
Links | Site |
---|---|
Change values on matplotlib imshow() graph axis | stackoverflow |
Manually-defined axis labels for Matplotlib imshow() | stackoverflow |
Matplotlib: personalize imshow axis | stackoverflow |
label matplotlib imshow axes with strings | stackoverflow |
matplotlib.axes.Axes.imshow | matplotlib.org |
how to use 'extent' in matplotlib.pyplot.imshow | stackoverflow |
origin and extent in imshow | matplotlib.org |
AttributeError while adding colorbar in matplotlib | stackoverflow |